0

I'm just getting to know Eclipse, and I'm trying to familiarize myself with how I should handle file organization and importing classes. This is a screenshot of the Package Explorer for reference:

Project Explorer

I've got two projects: "CashierSimulation" is a simulation of a cashier (go figure), and "BeanJuice" is a text-based game about coffee. Each of these use some or all of the generic classes that I made, which are located in the "GenericClasses" project file.

Originally, all of the files in all three project files were located in the same project file "BeanJuice", but I decided to separate the simulation files from the game files, and I moved the generic classes to their own project file because they aren't specific to the game.

I've figured out how to import the generic classes to the different project files as needed by referencing this post:

Import a custom class in Java

In addition to importing the classes using:

import package.myclass;

each project's properties file had to be updated to include "GenericClasses" in the build path.

Doing all of this worked and got my programs running, but I'm wondering if this is the "correct" way of doing all of this.

  • Is there an easier way that this could be done?
  • Should generic classes be stored in their own project file like this, or is there another way that it is more commonly done?
  • Should the packages inside each project file be renamed, or is this only necessary when you're planning on importing something from that package?

Any tips or tricks would be appreciated.

devjoco
  • 425
  • 3
  • 15

2 Answers2

1
  1. You may also want to have a look at maven and how to create "maven" modules in eclipse (or other IDEs); especially if you are authoring some sort of libraries for general consumption.
  2. There isn't any general rule on putting generic classes in their own project. They simply need to be logically grouped - into their own packages and/or projects.
  3. Also, instead of manually renaming the packages etc..., you may use 'refactor' (move etc...) - IDE will do the rest.
HarryBee
  • 26
  • 4
0

Do the following for your both projects, CashierSimulation and BeanJuice:

  1. Right-click the project folder: Build Path > Configure Build Path...
  2. In Projects tab click Add... and choose your GenericClasses project
  3. Click Apply and Close

Result: All classes of GenericClasses are also available in CashierSimulation and BeanJuice.

howlger
  • 31,050
  • 11
  • 59
  • 99
  • When you say "All classes of _GenericClasses_ are also available in _CashierSimulation_ and _BeanJuice_," do you mean the classes in _GenericClasses_ are now useable or importable by the other two projects? – devjoco Aug 22 '17 at 15:33
  • The classes of _GenericClasses_ can be imported in the other two projects and the compiled classes are on the build path, so you can run your _CashierSimulation_ directly from inside of Eclipse. – howlger Aug 22 '17 at 15:50