8.5 Java

8.5 Java

Although some application programmers love Java, Unix systems programmers avoid it, and you may not even need it on your system. However, you should still have an idea of how it works on a typical Linux system.

There are two kinds of Java compilers: native compilers for producing machine code for your system (like a C compiler), and bytecode compilers for use by a bytecode interpreter (usually called a virtual machine, even though it's more of an abstract machine). You will invariably encounter bytecode on Linux.

Bytecode files end in .class. The Java runtime environment (JRE) contains all of the programs you need to run Java bytecode. To run a bytecode file, use this command:

java file.class

You may also encounter bytecode files that end in .jar, which are collections of archived .class files. To run a .jar file, use this syntax instead:

java -jar file.jar

Sometimes you need to set the JAVA_HOME environment variable to your Java installation prefix. If you're really unlucky, you may need to use CLASSPATH to include any directories containing classes that your program expects. This is a colon-delimited set of directories like the regular PATH variable for executables.

If you need to compile a .java file into bytecode, you need the Java Development Kit (JDK). You can run the javac compiler from JDK to create some .class files:

javac file.java

JDK also comes with jar, a program that can create and pick apart .jar files. It works like tar.