Working with Packages in Java

It is sometimes convenient to bundle several related classes into a Java package. Such packages can then be imported into other applications or applets that you write. Each package must be in a separate subdirectory that has the same name as the package. If Kawa is set up properly, it will create the subdirectory for you and put the class files into it.

In Kawa, choose Customize and then Project Root Options. You should see the screen below.

Check the top box (Compilation output directory [-d]), and then click on the double arrow following the box right below it. Type a single period into the box shown in the screen below. It is hard to see it on this screen but it is there. This will make the current directory the place where package sub-directories are distributed.

Next click on Update and Close. You should see the following screen.

 

Choose OK to finish the setup. Now when you compile a file that starts with the term package, Kawa will create a subdirectory with that name and store all the .class files in it.

An example of such a file is shown next. The complete code for it is included at the end of the document.

When you compile this file, Reader.class will be stored in a subdirectory called simpleIO.

You can now import the class in this file into other programs. A test program is displayed on the next screen. When you import the class, use the full name of the class such as import simpleIO.Reader; rather than simpleIO.*;. When you compile this file, it will look in the package subdirectory in order to find the class file(s) that it needs to finish compiling.