3. A Larger Example

Return to one of the Unix/Linux machines, this time connecting via X or NX.

Copy the file RunAnywhere.java into a convenient directory. If you are using the same directory as earlier, delete any .class files already in that directory.

Like almost any compiler, javac has a number of options that can be given on the command line. Some common ones are:

Compilation Flags
-cp pathlist Add the directories and jar files named in the pathlist (multiple items may be separated by ':') to the list of places searched when locating other Java source or object code.
-g Include debugging information in compiled code (required if you want to be able to run the gdb debugger.
-depend Check each class used in the code being compiled to see if those mentioned classes' source code has been changed since they were last compiled. If so, automatically recompile those classes.
-deprecation Check the code for features that used to be legal in Java, but are expected to become illegal in the near future.
-O Optimize the compiled code (produces smaller, faster programs but takes longer to compile)

Of these, you would probably use -g almost all the time (unless you are one of those mythical programmers who never needs to debug their code).

So, compile this program with

javac -g RunAnywhere.java

then run the program with

java RunAnywhere

You should see window pop up shortly. Try clicking on the word "run".

Like the compiler, the java execution program allows for some command-line options. The most commonly used are:

Execution Flags
-cp pathlist Add the directories and jar files named in the pathlist (multiple items may be separated by ':') to the list of places searched when locating other Java object code.
-jar Instead of looking for a compiled class file, execute the "default" class in a .jar file.
-XmxN Sets the amount of memory to be reserved for the heap in the JVM emulator. N is a memory size consisting of an integer followed by a units indicator, usually M for megabytes, e.g., -Xmx512M

To see the effect of the -cp option, try doing this:

mkdir folder
cd folder
java RunAnywhere
java -cp .. RunAnywhere

The first attempt to execute the program fails because, after we have moved into a different directory, the java program cannot find our compiled class. The -cp option in the second command tells it where to look for that class and so is able to run the program.

Now cd back into your working directory and do an

ls

Notice that you have more than one .class file, even though you compiled only one .java file. That's a fairly common occurrence.

Of course, compiling a larger project consisting of several separate .java files is also likely to result in several .class files. It could be awkward if, every time you wanted to transfer a compiled Java program to another platform, you had to transfer an arbitrary number of different .class files. This awkwardness can be relieved by packaging the compiled class files into a single Java Archive (jar).

A jar file is actually a conventional "zip" compressed archive file with a little bit of extra directory information written into a special file (called the manifest) included in the archive.[1] Create a jar file like this:

jar -cf allInOne.jar *.class 

or

jar -cef allInOne.jar RunAnywhere *.class 

The -c option indicates that we want to create a new jar archive. The e option in the second form states that we are going to give a default entrypoint (i.e., a default program to execute). Use the first form if you are simply packing a group of "utility" classes, none of which is a full executable application, or if you are choosing to package together several different programs, none of which is more deserving of a "default" status than any other.

For now, use the second form to create allInOne.jar. Then delete the .class files and execute the program:

rm *.class
java -cp allInOne.jar RunAnywhere
java -jar allInOne.jar

The first java command uses the -cp option to tell where to look for compiled code (in the jar file) and explicitly names the program (RunAnywhere) that we want to look for. The second simply states that we want to execute the default program in that jar file. Both should work.

Finally, transfer the file allInOne.jar to a Windows PC. You should be able to launch the program from a cmd window by cd'ing to the appropriate directory and giving either of the java commands above. Alternatively, use the Windows GUI to open the folder where you placed that jar and double-click on the jar file to launch its default program.



[1] You can use a convectional zip archive program to verify this, if you wish. For example, after you create the allInOne.jar file in the next step, try

unzip -l allInOne.jar

In the Forum:

(no threads at this time)