Compiling and executing

To compile a program, you execute javac bob.java from the command line. This will produce bob.class. To execute bob.class, you type java bob. Notice, to compile its classname.java and to execute its just classname.

Compiling and executing with packages

To work with a package, you have to place it in a directory structure that mirrors its name. So, if you are working ~/java and the package is com.hp.spokane, you need to place all the cjava files in ~/java/com/hp/spokane. Then to compile, you go to ~/java and execute, javac com/hp/spokane/bob.java. To execute, you would type java com/hp/spokane/bob. Notice again, that we use the .java extension to compile and leave it off for execution.

Jar Files

A java archive file ( jar ) is used to distribute your code so that others may use it. Here are some sample instructions on how to create your own jar file.

  1. create bob.java with some reasonable main (ie: hello world)
  2. compile it to create bob.class
  3. under linux, there are two jar's, so alisas yours to be alias jar = /path/to/java/binaries/jar
  4. create a text file, ....call it "manifest.mf". It should look like the following....

    Manifest-version: 1.2
    Created-By: John
    Main-Class: bob
  5. jar cvfm test.jar manifest.mf bob.class. This creates the jar file. To add more classes to the jar, use the same command, but either add -C /directory/name or just list all the classes, bob.class, betty.class.
  6. to execute your new jar file, use the command... java -jar test.jar