Using JAVA


Java, the popular object-oriented language, is availableon UISACAD.   In this section we discuss how to set up your environment to use Java, how to use the JIT compiler, and how to know the current JDK version.
 

Setting up your environment

We assume your are using tcsh as your Unix shell.   In your home directory, you should find a file named .tcshrc.  Open that file with your favorite editor.  See if you already have the variable CLASSPATH already defined in that file.  If you already do, leave it as it is and move on to the next section of this document.  Otherwise, add the following towards the end of the file:

# Java section
setenv JAVA_HOME=/usr/local/jdk1.4
setenv CLASSPATH .
setenv  PATH ${PATH}:${JAVA_HOME}/bin
# end of Java section

Save the file, log out and log in again (or just type source .tcshrc from the shell prompt).  CLASSPATH is the variable used by the java compiler and interpreter to know where to look for compiled classes.  By setting this variable to ".", you are asking the interpreter and compiler to look into your current directory first. 
 

Knowing the JDK version

The first thing you want to do after you change your .cshrc is test whether you can access the Java interpreter.  Try the following from the shell prompt:

    java -version

The java interpreter will answer with the JDK version you will be using to compile and run Java programs.
 

Using the compiler (javac)

Try the Java compiler with a simple java program.   Type the following piece of code into a file named HelloWorld.java  (note the spelling - the uppercase letters are significant):
 
public class HelloWorld {
    public static void main(String args[]) {
        System.out.println("Hello World!");
    }
}
To compile this program, from the shell prompt, type:
 
javac HelloWorld.java


You should now have a HelloWorld.java and a HelloWorld.class in your current directory.  The first is, of course, the program you typed in.  The second is the output of the javac compiler.  However, contrary to the output of a C or C++ compiler, you may not directly execute this .class file.  This file will be interpreted by the Java interpreter.
 

Using the Interpreter (java)

To run the example program from the previous section, use the java interpreter.  At the shell prompt, jut type java followed by the name of the class from whose main() function you want the execution of the program start.  In this case, the name of the class is HelloWorld and you should write:
 
java HelloWorld


If all is well, the program should run and you should see the "Hello World!" string as output.
 

Using the JIT compiler

JIT is the "just in-time" compiler for java code.  Contrary to the "javac" compiler, the JIT compiler actually compiles the class into native machine code and runs the machine code.  Using the JIT compiler should speed up your program execution by an order of magnitude.   We have installed the Sun-Solaris version of the JIT compiler in both csc and copihue.  Here is what you need to do to use it:

First, update your LD_LIBRARY_PATH environment variable as follows (you may add this to the Java section of your .cshrc):

setenv LD_LIBRARY_PATH ${LD_LIBRARY_PATH}:/usr/local/java/lib

The define the following environment variable:

setenv JAVA_COMPILER sunwjit_opt

(WARNING:  once you set up this veriable, the JIT compiler will be invoked every time you run the java interpreter.  If you only want to test the JIT compiler, do not set the JAVA_COMPILER variable in your .cshrc file.)

That's it!  Now, every time you run a Java program with the java interpreter, the JIT compiler will dynamically compile the necessary .class files into Sun-Solaris' machine code and run that compiled code.  You do not need to use any special flag to invoque the JIT compiler.  See http://www.sun.com/solaris/jit for more details on how the JIT compile works.


Home | Information | Academics | People | Resources | Contact

The Department of Computer Science at the University of Illinois at Springfield
Copyright© 2002 University of Illinois at Springfield