1. Introduction

For many years students (and members of staff) have been asking me how to set the Java classpath environmental variable on Apple machines. If all you want to do is create and use your own Java packages the answer is "don't bother". The following is intended to demonstarte how to create and use user defined Java packages withput fiddeling about with the classpath environmental variable. The examples are intended for Apple users, however theyare equally applicable to UNIX users (don't ask me about Windows, I have no idea).

Tp start with create a directory in which you wish your package to be contained. In the examples I am assumimg a directory PackExample. Write your source code on some other directory, I am using a directory called SourceCode. I will also be using a directory called Application (for my application classes). The relative realtionship between yhese directories is as shown in Table 1.

                     frans
                       |
                  JavaProgs
                       |
                 Experiments
                       |
                    Packages
                       |
    +------------------+------------------+
    |                  |                  |
PackEample        SourceCode         Application

Table 1: Directory structure used in the examples

2. Package Source Code

In the source code for a class to be included in a package include the key word package followed by the name of the package at the top of the class (first line excluding comments). I give a "Hello World" example in Table 2.


// HELLO WORLD
// Frans Coenen
// Department of Computer Science, The University of Liverpool
// Wednesday 21 April 2010

package PackExample;

public class HeloWorld {

    /* ------ FIELDS ------ */
    
    // None
    
    /* ------ METHODSS ------ */

    public void output1() {
         System.out.printf("Helo World\n");
         }
    }
	

Table 2: HelloWord source code

Now compile this using:

javac -d ~frans/JavaProgs/Experiments/Packages/ HelloWorld.java

(called from within the directory in which the HelloWorld.java source code is held). Note that I have specified the absolute directory path (relative paths, such as ../Packages/, do not seem to wiork; no idea why). The -d flag will cause a directory PackExample to be created (if it does not already exist). In this directory you will now find a HelloWorld class. When recompilling the compilation will cause any existing class of the same name to be overwritten.

You can carry out the above using the (standard):

javac HelloWorld.java

which will create a HelloWorld class in the current directory which you can then cut-and-paste into the Packages directory. However I would recommend the first approach (less work).

3. Application Code

Having created our package we now we want to incoporate it into applications. Table 3 gives some appropriate source code for an application class, HelloWorldApp.


// HELLO WORLD APPLICATION CLASS
// Frans Coenen
// Department of Computer Science, The University of Liverpool
// Wednesday 21 April 2010

import PackExample.*;

public class HelloWorldApp {

    /* ------ FIELDS ------ */
    
    // None
    
    /* ------ METHODSS ------ */
    
    /* Main method. */
    
    public static void main(String[] args) {
        // Create instance of class HelloWorld
        HelloWorld newHelloWorld = new HelloWorld();
        
        // Output
        newHelloWorld.output1();
        }
    }
	

Table 3: HelloWorldApp source code

To compile the code in Table 3 use:

javac -classpath ~frans/JavaProgs/Experiments/Packages/ HelloWorldApp.java

(called from within the directory in which the HelloWorldApp.java source code is held). Again note the use of an absolute directory path. The -classpath flag tells the compiler where to look for the componemts required by JellowWprdApp.java. Any number of locations can be included as an argument each seperated by a : (colon) as demonstrated in the following section. Note again that I have used abosolute class paths (why do relative class paths, using the .. notation, not work?).

4. Running The Application

To actual run the application use:

java -classpath ~frans/JavaProgs/Experiments/Packages/:. HelloWorldApp

(againn called from within the directory in which the HelloWorldApp.java source code is held). Note that in the class path we need to include the current directory, indicated by . (dot), because this is where the class HelloWorldApp is held. As a result we will get some output of the form shown in Table 4.


Helo World
	

Table 4: Output from HelloWorld application

The varuious compile and run commands gievn above are quite long winded and thus prone to typing errors, I would therefore recommend creating shell scripts (especially if you are developping code and repeatedly invoking a compile-run-debug cycle).

5. Making Things More Complicated

Suposing we want to add another class to our PackExample package that uses the HelloWorld class we have already created. The ExtendedHelloWorld.java source code given in Table 5 extends the HelloWorld class created previously. Note that this class inherits the method output1() from its parent class HelloWorld. To compile the code in Table 5 use:

javac -d ~frans/JavaProgs/Experiments/Packages/ -classpath ~frans/JavaProgs/Experiments/Packages/ ExtendedHelloWorld.java

Note that becausewe were using an existing class from the PackExample packagec we had to tell the compliler where to find it using the -classpath flag (otherwise the compiler would not be able to locate the method output1()). Thus, we now have a second class in our PackExample package.


// EXTENDED HELLO WORLD
// Frans Coenen
// Department of Computer Science, The University of Liverpool
// Wednesday 21 April 2010

package PackExample;

public class ExtendedHelloWorld extends HelloWorld {

    /* ------ FIELDS ------ */
    
    // None
    
    /* ------ METHODSS ------ */

    public void output2() {
         System.out.printf("**********\n");
         output1();
         System.out.printf("**********\n");
         }
    }
	

Table 5: ExtendedHelloWorld source code

We now need another aplication class, to use the ExtendedHelloWorld class, such as that given in Table 6.


// EXTENDED HELLO WORLD APPLICATION
// Frans Coenen
// Department of Computer Science, The University of Liverpool
// Wednesday 21 April 2010

import PackExample.*;

public class ExtendedHelloWorldApp {

    /* ------ FIELDS ------ */
    
    // None
    
    /* ------ METHODSS ------ */
    
    /* Main method. */
    
    public static void main(String[] args) {
        // Create instance of class HelloWorld
        ExtendedHelloWorld newHelloWorld = new ExtendedHelloWorld();
        
        // Output
        newHelloWorld.output1();
        System.out.println("---------------");
        newHelloWorld.output2();
        }
    }
	

Table 6: ExtendedHelloWorld source code

We compile this in the same way as before:

javac -classpath ~frans/JavaProgs/Experiments/Packages/:. ExtendedHelloWorldApp.java

To run we then use (as before):

java -classpath ~frans/JavaProgs/Experiments/Packages/:. ExtendedHelloWorldApp

which will give output of the form given in Table 7.


Hello World
---------------
**********
Hello World
**********
	

Table 7: Output from ExtendedHelloWorld application

I hope all this is of some help!