Menu

packages

Billy Keyes

Packages and Projects in Eclipse

Java, like many programming languages, provides support for organizing code into namespaces. In Java, namespaces are known as packages. In any project with more than two or three files and more than one or two developers, packages are a good way to make project structure understandable.

In addition, proper packaging ensures that namespace conflict are minimized. Two classes with the same name can be distinguished only if they are in different packages. Using no package, i.e. the default package, can be disruptive, as most Java tools expect classes to have fully-qualified names.

Packages for floctrl

The floctrl project uses the following package scheme as of 3/12/2012.

  1. All code is placed in a subpackage of edu.cmu.cs.floctrl. The use of the domain name structure follows standard Java conventions.
  2. The subpackage name is a shortened version of the project or component name. For example, the publish/subscribe code is the the pubsub subpackage, giving full path of edu.cmu.cs.floctrl.
  3. You can use additional subpackages as needed. For instance, you might find it convenient to put all code for your GUI in a package (edu.cmu.cs.floctrl.conductor.gui).
  4. If you have test code, place it in the package edu.cmu.cs.floctrl.test. If you have multiple test files, you can create subpackages of the test package.

Packages in Eclipse

Eclipse has good support for dealing with packages. You can create new packages in the same way you create new classes, using either File -> New -> Package, the button in the toolbar next to the "New Java Class" button, or right-clicking in the Package Explorer panel and choosing New -> Package.

Aside: If you do not have the Package Explorer open, go to Window -> Show View -> Package Explorer

Eclipse should be able to find the correct paths for any classes you want to use and automatically add import statements. Ctrl + Shift + O is a handy shortcut for organizing and correcting import statements.

Subprojects in Eclipse

Eclipse can only manage import statements if it knows where all the necessary code is located. If your code depends on code that someone else wrote in a different project, you'll have to add that project as a dependency of your own project. Here's how:

  1. Right-click on your project in the Package Explorer
  2. Click on Build Path -> Configure Build Path...
  3. Click on the Projects tab
  4. Click the Add... button
  5. Select the required projects and click OK

Note that you have to have the projects open in your workspace to add them as dependencies. Use the File -> Import menu to add the projects to your workspace.

Adding new Subprojects

Because of the way our git repository is setup, adding new subprojects is simple.

  1. Click on File -> New -> Java Project (or use the toolbar icon)
  2. In the New Java Project dialog, uncheck Use default location.
  3. Click the Browse button and select the directory that was created when you checked out the repository. This directory should have all the other project folders inside.
  4. Finish creating your new project and add/commit it when you are ready.

Directory Structure

Because all the project folders are at the top-level of the git repository, there should be no path problems, because everyone with a copy of the repository should have the same structure.

If you do have path problems, make sure you haven't moved any projects out of the git repository.

Migration Issues

The package structure was added by Billy Keyes on 3/12/2012. If you have any problems with the new structure, please email him.


Related

Wiki: Home

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.