Java tips

This page contains tips on using Java.

Writing a Java program
You write a Java program by creating a SomeClass.java file which contains a public class (here, named SomeClass) with a public static void main(String[] args) method. (The .java file can contain other non-public classes too.) Once written, you then can either  compile it to bytecode with a Java compiler like Jikes or Sun's compiler (which is run with the javac SomeClass.java command, or else compile it to native machine code (a.k.a. ahead-of-time ("AOT") compile) using GCJ. 

Here is what a Hello World Java program would look like (it has to be named the same as the class that holds the main method, HelloWorld.java):

/*
 * Hello world program

public class HelloWorld {

public static void main(String[] args){ System.out.println("Hello, World!"); }

}

Running a Java program
Java programs are run using the java SomeClass command where SomeClass</tt> is the name of your public class containing a static main method. A common error is running it with the full class file name (e.g. SomeClass.class) instead of just the class name by itself. Run it like so: $ java SomeClass

You must use your class's fully-qualified name, so if it's in a package, you'll need to type something like: $ java org.foobar.SomeClass

Of course, if you ahead-of-time compiled your Java code, you can simply run it like any other binary on your system.

The problem that most people have with java on Linux is the fact that the Java Runtime Engine (which is needed to run java programs) is rarely in the default path of a user. This means that people get hideously confused when the java command cannot be found.

Running a Jar package
$ java -jar HelloWorld.jar Not all jar packages are executable. Some contain resources (like png's, or other binary or textual files) which cannot be run directly.

If you set your desktop environment's file associations to launch MIME type 'application/x-jar' using 'java -jar %f', executable Jar packages can be run from the desktop in the same way as native executables (i.e. by double-clicking them).

Classpath
Java searches for class file in it's Classpath. This normally is an enviroment variable called CLASSPATH</tt>. It's a list of directories (and paths to .jar files) similar to the common PATH</tt> variable. That means directories (or jar files) are seperated by ':'. The java command also accepts a -cp</tt> option to set the Classpath.

Packaging your Java programs as Jar files
The best way to distribute your Java program is by putting it into a nice all-in-one zip-compressed .jar</tt> file. The jar</tt> command works analogous to the tar</tt> command. Create your jar file like so: $ jar cvf MyApp.jar SomeFile.class Foo.class Bar.class coolpic.png

Since one of those .class files probably contains your main method, you'll want to extract out the manifest file $ jar xf MyApp.jar META-INF/MANIFEST.MF

edit it, $ vim META-INF/MANIFEST.MF

adding in a Main-Class: SomeClass

line to the end, then put it back, updating your jar's copy of that manifest $ jar ufm MyApp.jar META-INF/MANIFEST.MF

Now, when you later update your source, update the jar file like so: $ javac Foo.java $ jar uf Foo.jar Foo.class

= See also =


 * http://en.wikibooks.org/wiki/Java_Programming - get good info on this topic