Compiling/Running Java Projects with Multiple Classes

This documentation answers a question raised in class. It’s not yet a required part of the course and I suspect it will only interest a few people at this time. Read as far as you are interested.

There are several ways to bundle java code.
Compile w/o packages
The first, and simplest, is not to use a package and have all your class files in the same directory. Let’s illustrate with ObjectSenderV2 which depends on the external classes Building, Occupation and Person

To compile:
javac ObjectSender.java Building.java Occupation.java Person.java

To run the main program:

java ObjectSender

Bundling the Code in a jar file
A .jar file stands for Java Archive file. It’s based on the Unix .ar or .tar archive file and commands, with the addition of a Manifest file which can be used to specify which class is the main class of your program. Because these commands are very old they do not use a dash to preceded the flags and flags such as cfv are the first argument.
To bundle your classes into a jar file:
jar cvf myCode.jar ObjectSender.java Building.java Occupation.java Person.java
c stands for create. v stands for verbose. And f is used name the .jar file.

To list the files inside the jar file:
jar tvf myCode.jar
Now modify the manifest so that ObjectSenderV2 is designated as the main program.
jar uvfe myCode.jar ObjectSenderV2
u stands for update, v again for verbose and e means that we are defining and entry point into the package. One can define multiple entry points. It’s also possible to define a main program as the default entry point - see the jar tutorial for instructions.
To run the code in the jar file:
java -jar myCode.jar ObjectSenderV2
Working with packages
A package is a collection of programs from the same project. At the top of each java file add the following line, substituting the name of your package.
package MyPackage;

Now create a directory MyPackage and place all of your .java files in it. From the directory above it compile:
javac MyPackage/*.java

If you compile any files with a dependency on another class, make sure you compile the dependent classes either previously or at the same time. To run your code:
java MyPackage.yourMainClass

This, by the way, is the origin of the hierarchy of classes used in the import statement. A class java.net.Socket would have been developed in the file Socket.java in the directory java/net. The directory hierachy symbol / simply gets replaced by a period.

Working with packages in a jar file
You can collect your packaged program in a jar file as well.

jar cvf MyCode2.jar MyPackage/
Then modify the Manifest file to indicate your entry points:
jar uvfe MyCode2.jar MyPackage.ObjectSenderV2
To run the code:
java -jar MyCode2.jar MyPackage.ObjectSender V2

Lastly, Extracting From and Updating a jar file.
To update a jar file:
jar uvf MyCode.jar xyz.class MyPackage/abc.class
The example shows you how to update the jar either with a package element or a non-package element. You can also add your source code and documentation into the jar file as well.
To list items in the jar file:
jar vtf MyCode.jar
You can then use these listed names to extract an item to the current directory. Be careful about overwriting files – you may want to move the .jar file to a different directory for safety.

jar xvf MyCode.jar xyz.class MyPackage/abc.class

Finally, to remove an item from the archive –there is no such option in the jar command. However a jar file is just a zip file with a Manifest. You can use the zip command:
zip -d MyCode.jar xyz.class
If you program had a main and you designated in the Manifest as an entry point, use the M option to remove the entry
jar xvfM MyCode.jar xyz.class

One More Thing… Working with NetBeans

If you designate a main class in your project, Netbeans creates a .jar file under the distdirectory of your project. You can move this to a different system and use jar to look inside it and java to run it as well.