Practical: Edit-Compile-Run Cycle for Java Programs

When developing programs with command-line tools, you typically iterate the following steps:

  1. Edit: Use an editor to develop the code in your programming language of choice. Result: a text file with the source code of the program (e.g. Sample1.java)

  2. Compile: Use a compiler to translate the source code into a version understood by the machine. Result: an executable file containing byte code of the virtual machine, in this case the Java Virtual Machine (JVM), e.g. Sample1.class

  3. Run: Execute the program on the machine. Result: you see the result of executing your program (e.g. messages printed by the program)

Similar to the previous the Section called Practical: Edit-Compile-Run Cycle for C Programs, this section is a practical on how to use common Linux tools to develop, compile and run simple Java programs from the command-line.

Editing

There are a lot of good editors for Linux out there, and some of them are discussed in the Section called Editors. In this practical we will used emacs as editor, which by default has a programming language mode for Java including highlighting etc.

To get started, download the program called Sample1.java executing the following command from the commandline (or using this URL):


  $ wget http://www.macs.hw.ac.uk/~hwloidl/Courses/LinuxIntro/Sample1.java
  
You now have the file in your current directory. You can confirm this by typing

  $ ls -ltr
  
and the last line displayed should show the filen Sample1.java.

Compiling

Before we can run the program we need to compile it. This means, we need to run a program that takes the Java program as input, and produces an executable file, i.e. a file in a format that is understood by the machine. In an IDE the steps of compiling and executing are often conflated by just clicking at a run button. However, conceptually it is important to separate the two steps, because they do very different things.

Now, to compile our Sample1.java program, we execute the following line:


  $ javac Sample1.java
  
This will translate the Java program in Sample1.java into byte code of the Java Virtual Machine (JVM), stored in Sample1.class. You can think of this representation of the program as portable machine code, which can be executed by another application called java.

Running

As mentioned above, we use the java to execute the byte code (class file) that we have generated:


    $ java Sample1
Enter an integer: 1
...
Enter an integer: 10
Sum = 55
You should see the string Enter an integer: and a prompt to enter a number and to press return. Enter 10 integers in total, and you will see at the end the calculated sum of all numbers, which in this case is 55 if you entered the numbers from 1 to 10.

Further Exercises

You now know how to edit, compile and run a Java program from the command line. As another exercise, go back to the compilation step, and modify the program so that it also computes to product of all numbers that have been entered. Print the product, as well as the sum, at the end of the program.