views:

65

answers:

5

I've been a c++ programmer for 10 years, i'm used to just creating libraries and then linking to them from my existing project. However in java, i have 2 projects, one is my game engine, the other is the test environment that i would like to use, here is how it is structured:

com.logic.engine
com.logic.testapp

yet in my test app, i cannot do

import com.logic.engine.*;

it simply cannot find the reference.

How can I do this without having to copy and paste my engine into every program i write with it?

+5  A: 

Package your engine as a jar, and add the jar to the classpath of the projects that use it.

Nathan Hughes
While adding the engine project to the test apps build path will work, IMHO this solution gets you closer to how the app would likely be deployed. The only caveat I'm aware of is that changes to the engine won't necessarily be reflected in the jar the test app is using, unless you update it, or make your build rebuild the jar, etc.
Jake
A: 

You need to change the classpath to include the folder containing your engine.

java -cp /path/to/engine testapp

Rocket
Not helpful - this is an Eclipse question.
crazyscot
Sorry, I didn't realize. You can still change classpath in eclipse, though.
Rocket
+2  A: 

In eclipse, right click on your test project in the Project Explorer and select Properties. In the "Java Build Path" section, go to the Projects tab and add your engine project.

David
+2  A: 

In eclipse you can also declare that your test prgram depends on your game engine. The classes will then be available. Check in the build paths dialog available from the context menu in the project view.

Peter Tillemans
A: 

I just fired up Eclipse to check this out.

You should see a red wavy underline on import com.logic.engine.*; - mouse over this and it'll tell you that the import could not be resolved and offer you a number of so-called quick fixes. One of those should be Fix Project Setup - that will offer to fix things for you, in this case to add one project to the other's build path.

crazyscot