1 For Dr Java
2 For Eclipse
3 Exporting Package to Others

How to Make Java Packages

Kathi Fisler

Credits: Thanks to Taymon Beal for the detailed instructions on doing this in Eclipse.

In Java, packages bundle related classes into a single unit that others can use. Whenever you use the import statement, you are using someone else’s Java package: so far, you have used the Tester and LinkedList packages, for example. These notes describe how to create your own packages, a standard component of working in Java.

Whether you are using Eclipse or DrJava, packages go along with projects. The instructions for creating projects differ across the two tools, but both share the conventional Java directory structure for packages. Let’s understand the package directory structure first, then discuss how to create projects in each tool.

Packages are aligned with the structure of files and directories that contain your classes. When you create a package, you want your code to end up in a directory structure like the following:

project-directory

  > src

    > package-name

      > ... your .java files

  > bin [all your .class files will end up in here]

  > test

    > package-name

      > Examples.java

      > Main.java

As a concrete example, assume I wanted to make a package called animals from our start-of-term code about boas and dillos. I would then want the following directory structure:

animals-project

  > src

    > animals

      > Dillo.java

      > Boa.java

      > IAnimal.java

  > bin

    > animals

      > Dillo.class

      > Boa.class

      > IAnimal.class

      > Examples.class

      > Main.class

  > test

    > animals

      > Examples.java

      > Main.java

In each of the .java files in the animals project (both src and test files), the first non-comment line would have to be package animals;

Note that the compiler creates the .class files as always – you just have to have the bin directory in which to put those files.

Once you have your project set up, you just need to be sure to put your .java files in the correct place (src or test) as you create and save them.

1 For DrJava

With DrJava, you set up the directories shown above manually, then collect them into a single project.

Set up the directory structure as shown above: make a directory for your work, create subdirectories called "src", "bin", and "test", and put subdirectories with the name of your package into each of "src" and "test". Then, follow these instructions for creating a DrJava project, using your project-directory directory as the "Project Root", your "bin" directory as the "Build Directory", and your project-directory as the "Working Directory".

The copy of Main.java in the test directory also needs the same "package ..." declaration at the top as all other files for the package,

Skip the instructions on the project-instructions page about "Testing the Project", as those instructions were not written for someone using the tester library.

Once you add files to your project, you should be able to compile the project and run it, getting the same tester output that you have all term. Should you get an error about Java not being able to find the tester library, go to the "Project" menu, then "Project Preferences" (at the bottom), then add the tester.jar file as an "Extra Classpath".

2 For Eclipse

With Eclipse, creating a new project sets up (most) of the directories shown above automatically.

Creating a new project in Eclipse will automatically create the "src" and "bin" directories . To have Eclipse create the test directory too, at the second screen ("Java Settings") of the New Java Project wizard, go to the Source tab (the first one), click "Create new source folder", and enter "test" for the folder name. While you’re at it, go to the Libraries tab (the third one) and add the tester library to your classpath.

When creating a new source file, the New Java Class and New Java Interface dialogs have a "Source folder" field (which should contain either "YourProjectName/src" or "YourProjectName/test") and a "Package" field. Eclipse will automatically generate the package declaration, create the package directory if it doesn’t already exist, and put the source file in it. To move something to a different package or source folder, right-click it in the Package Explorer pane on the left-hand side, go to "Refactor", and click "Move...". If the package you want to move it to doesn’t exist (or doesn’t exist in the source folder you want), click the "Create Package..." button to create it. Eclipse will move the source file, change the package declaration, and update any imports elsewhere in your project as appropriate. This window won’t let you create new source folders; you can do that from the New menu. Note that files copied into a project from outside sources do not automatically get package declarations; however, Eclipse will suggest fixing the resulting compile error with a package declaration, which it will generate if you ask it to.

You will have to take one of the following two approaches to get Eclipse to properly find your Examples class:

Once you add files to your project, you should be able to compile the project and run it, getting the same tester output that you have all term.

3 Exporting Package to Others

These instructions help you organize files into packages. If you wanted to share your package with someone else, you would ask DrJava or Eclipse to bundle the package up into a single .jar file. We are not asking you to do that for this assignment, but you should be familiar with how this is done.

In DrJava, you’ll find a "Create Jar File from Project" option under the "Project" menu.

In Eclipse, you go to the "Package" menu and use the "Export" option (you want to export the package, not the project).

In either case, if you enter <package-name>.jar for the name), you’ll get a single .jar file that you could then use in another program by saying import <package-name>.*;