07 – Packages

We’re nearly ready to write some code on our own now. The last thing we must understand before doing so is how packages work in Java. Packages are simply a way to organize your classes in a hierarchical structure, grouped however you want, but hopefully with the intention of making management of your source code easier.

To place a class into a package you must first make a directory structure that looks like the package hierarchy you wish to employ. Typically you would encode information about who you are and the application you are coding for within the hierarchy at the same time. For example if you were writing an email agent for your non-profit you might choose a base structure like org.myorg.email From there you would create subdirectories depending upon your programs requirements, like org.myorg.email.app, org.myorg.email.gui, etc.

Then as you create a class within a package, the first line of the class file must define where the package is in the hierarchy. For example, in our app package for our eGate monitor the main class might look like this:


package org.myorg.email.app;
public class MainApp {
...

As long as you remember that the package definition must be the first line in the class file you’re well on your way to creating your own Java classes.

Now we can finally create some Java code on our own. First we must put our directory structure in place. Make a folder in your home drive called javatut. Inside that make a directory with your initials, like ~/javatut/abc/ or c:\\javatut\abc\ on windows. Under the yourinitials directory make a directory there called app. We will put the java103 directory in our classpath when we compile and run our java code so our base package will be that yourinitials directory. Anything we put under the app directory (which is where we’ll put all our executable code) will be in the yourinitials.app package.
Beneath the app directory create a .java file called TestApp.java. Edit the file so that it looks like this:


package yourinitials.app;

public class TestApp {
  public static void main(String[] args) {
    System.out.println(“Hi”);
  }
}

Obviously, change yourintials to whatever you entered for your initials. Next we open a command prompt and compile and run the program with the following lines, for windows:


C:\>cd javatut
C:\\javatut>javac -cp . yourinitials\app\TestApp.java
C:\\javatut>java -cp . yourinitials.app.TestApp

Or in Unix:


~ >cd javatut
~/javatut > javac -cp . yourinitials/app/TestApp.java
~/javatut > java -cp . yourinitials/app/TestApp

Since I use Unix most often, a shortcut that I used to compile and run in one step is to use the && shell operator to do both if the first succeeds:


~/javatut > javac -cp . yourinitials/app/TestApp.java && java -cp . yourinitials/app/TestApp

Then using the command line history, you can quickly recompile and retest. Later on we’ll learn how to use ant to automate building and running.

Compiling the Java files produces files with a .class extension. These files contain the bytecode that the Java virtual machine (JVM) executes.

If for some reason these lines fail, please make sure you have a Java 2 version 1.4 or higher SDK installed. You might also need to update your Path information or fully qualify the path to the javac and java commands. These commands could also fail if there was a typo either in the command or the code you entered above. Please double-check these and once the program is running it will simply display “Hi” as its output.

Packages Exercise

Now with packages under our belts, so to speak, we can actually test the code we have written. Put your code for the Methods Exercise to the test, by saving it to your app package location and placing an appropriate package declaration at the top. Then compile your .java file and execute it.