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 use 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.

More editing

Now we want to modify the sample program in such a way, that we can specify the number of integer values, that should be read, on the command line. It is therefore a general example on how to get input into your program, without reading from file or interactively asking for input.

First we copy the file Sample1.java to Sample2.java, for our new program, and we start the editor on this new file:


  $ cp Sample1.java Sample2.java
  $ emacs Sample2.java
  

The Main method in the original program, has an array of strings as an argument (args). We haven't used this so far, but we can in order to pass a value from the command-line. In order to so, we check, just before the start of the loop, whether the length of args is 1, which is the expected case. If it is not, we exit with a usage message. If it is, we lookup the first argument in the array, and parse it as an integer value. The result is written to the new variable n, which specifies the number of integer values to read interactively. The rest of the program is unchanged. Below you can see a screenshot of the modified program, with the cursor at the end of the new code.

Again, compile the code like this:


  $ javac Sample2.java
  
and run the program like this, with one additional argument passed from the command-line

    $ java Sample2 2
Enter an integer: 2
Enter an integer: 4
Sum = 6
You should be asked for as many numbers as you have specified, and the sum will be computed as before.

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. You can find a sample solution here.