System Interface Guide

The Java Programming Environment

Programming in Java is supported in Solaris JavaVM by your favorite text editor, make(1S), and by:

javac

Java compiler. Translates Java source code files (name.java) into byte code files (name.class) that can be processed by the interpreter (java(1)). Both Java applications and Java applets are compiled.

javald

Wrapper generator. Creates a wrapper that captures the environment needed to compile and run a Java application. Since the specified paths are not bound until the wrapper is invoked, the wrapper allows for relocation of the JAVA_HOME and CLASSPATH paths.

java

Java interpreter. May be invoked as a command to execute a Java application or from a browser via html code to execute an applet. 

appletviewer

Java applet viewer. This command displays specified document(s) or resource(s) and runs each applet referred to by the document(s). 

javap

Java class file disassembler. Disassembles a javac compiled byte code class file and prints the result to stdout.

javah

C header and stub file generator. For each class specified, creates a header file, named classname.h, and places it in the current directory. Also, optionally, produces C source stub files.

(For more information on using make(1S) see the chapter "make Utility" in the Programming Utilities Guide.)

The normal Java environment variables are:

Variable 

Description 

JAVA_HOME

Path of the base directory of the Java software. For example, javac, java, appletviewer, javap, and javah are all contained in $JAVA_HOME/bin. Does not need to be set to use Solaris JavaVM.

CLASSPATH

A colon (:) separated list of paths to directories containing compiled *.class files for use with applications and applets. Used by javac, java, javap, and javah. If not set, all Solaris JavaVM executables default to /usr/java/lib/classes.zip. Does not need to be set to use Solaris JavaVM.

PATH

The normal executable search list can contain $JAVA_HOME/bin.


Note -

The JavaVM tools are installed in /usr/java/bin and symbolic links to each executable are stored in /usr/bin. This means that nothing needs to be added to a user's PATH variable to use the newly installed JavaVM package. Also, all Solaris JavaVM executables default to the path /usr/java/lib/classes.zip to find the standard Java class library.


The base Java programming environment provides no debugger. A debugger is included in the optional Java Workshop package.

Java Programs

Java programs are written in two forms: applications and applets.

Java applications are run by invoking the Java interpreter from the command line and specifying the file containing the compiled application.

Java applets are invoked from a browser. The HTML code interpreted by the browser names a file containing the compiled applet. This causes the browser to invoke the Java interpreter which loads and runs the applet.

An Application

Example 2-1 is the source of an application that simply displays "Hello World" on stdout. The method accepts arguments in the invocation, but does nothing with them.


Example 2-1 A Java Application

//
// HelloWorld Application
//
class HelloWorldApp{
	public static void main (String args[]) {
		System.out.println ("Hello World");
	}
}

Note that, like C, the method, or function, to be initially executed is identified as main. The keyword public lets the method be run by anyone; static makes main refer to the class HelloWorldApp and no other instance of the class; void says that main returns nothing; and args[] declares an array of type String.

The application is compiled by

$ javac HelloWorldApp.java

It is run by

$ java HelloWorldApp arg1 arg2 ...

An Applet

Example 2-2 is the source of the applet which is equivalent to the application in Example 2-1.


Example 2-2 A Java Applet

//
// HelloWorld Applet
//
import java.awt.Graphics;
import java.applet.Applet;

public class HelloWorld extends Applet {
	public void paint (Graphics g) {
		g.drawstring ("Hello World", 25, 25);
	}
}

In an applet, all referenced classes must be explicitly imported. The keywords public and void mean the same as in the application; extends says that the class HelloWorld inherits from the class Applet.

The applet is compiled by

$ javac HelloWorld.java

The applet is invoked in a browser by HTML code. A minimum HTML page to run the applet is:


<title>Test</title>
<hr>
<applet code="HelloWorld.class" width=100 height=50>
</applet>
<hr>

javald and Relocatable Applications

Correct execution of many Java applications depends on the values of the JAVA_HOME, CLASSPATH, and LD_LIBRARY_PATH environment variables. Because the values of these environment variables are controlled by each individual user, they can be set to arbitrary paths, with either path being unusual. Further, it is common for an application to require an unique value in the CLASSPATH variable.

javald(1) is a command that generates wrappers for Java applications. The wrapper can specify the correct paths for any or all of the JAVA_HOME, CLASSPATH, and LD_LIBRARY_PATH environment variables. It does so, with no effect on the user's values of these environment variables. And it overrides the user's values for these environment variables during execution of the Java application. Further, the wrapper ensures that the specified paths are not bound until the Java application is actually executed, which maximizes relocatability of applications.