Compiling and running Java code that uses packages on Solaris Unix

refer to pg 336-340

In order to test the compilation and execution of packages, try the

following steps:

** Don't forget to alter your path, using the set_path script**

1. Create a directory in your home directory called jdk1.2

2. Create a subdirectory under jdk1.2 called jre

3. Create a subdirectory under jre called classes

4. Get a copy of the code for example 8.3 (Time1.java and Time1Test.java)

and put it in a directory to hold your source code.

5. From your source code directory, compile the example code from

figure 8.3 as follows:

> javac -d $HOME/jdk1.2/jre/classes Time1.java

- this will place the package (Time1.class) in your home directory under

$HOME/jdk1.2/jre/classes/com/deitel/jhtp3/ch08

(it will create all the subdirectories under the classes directory for you)

6. Now compile the TimeTest.java program. This program imports the package.

> javac -classpath $HOME/jdk1.2/jre/classes TimeTest.java

- this will place the compiled program in your source code directory

7. Run the program, but now you need to specify the location of your class

directory that contains the packages. This is done with the -classpath

switch as follows:

> java -classpath $HOME/jdk1.2/jre/classes:. TimeTest

We are telling the interpreter to search 2 directories, with the directories

separated by a colon ( : ).

The first directory is:

$HOME/jdk1.2/jre/classes (where home is your home directory)

The second directory is:

. (a single dot, which means LOOK IN THE CURRENT DIRECTORY).

 

***You can avoid having to specify the -classpath variable during execution by

defining a environmental variable at the command prompt as follows:

> setenv CLASSPATH $HOME/jdk1.2/jre/classes:.

This works for execution time, but not during compilation, as you need to specify

the -d switch in order to get the .class file to appear in the package location.