views:

407

answers:

4

I've got a very small set of classes built up in a custom package hierarchy with one console app class that employs them. Everything works fine from JCreator and from a command prompt.

I'd like to build a second console app that re-uses that same package.

As a Java newbie, what is the quickest, dirtiest method to do that?

My main concern was avoiding copying the package directories over to the new console app's directory.

Using JCreator, I didn't have any problems adding the package directory to the project and compiling and running. But when I tried to run the console app from the command line, it couldn't find the classes in the package hierarchy.

In Visual Studio, you just add a reference...

+5  A: 

What you want to do for both apps is create a jar file with a Main-class definition in the var manifest. There's a good bit of information on this in the Java Tutorials, but the gist of it is just that you'll create a jar file with the jar tool, and then make a little wrapper to run it as

java -jar myfile.jar
Charlie Martin
+1: Executable JAR -- quick and *not* dirty.
S.Lott
Here's the full tutorial: http://java.sun.com/docs/books/tutorial/deployment/jar/appman.html
toddk
toddk, that's the exact same tutorial I linked.
Charlie Martin
A: 

If you do not wish to copy your class files from your first application, then you need to set up the classpath used when you run java from the command line to include the location of those files.

Make sure you also include the location of your newly created class files.

Stephen Denne
A: 

Using a tool like process exlporer, you should be able to see the command line which JCreator used to launch your application.

(Typically IDEs also include lots of command line parameters for being able to connect to your application and debug it, etc.)

Stephen Denne
A: 

If I read this correctly, you have a class I'll call A that references packages in package pkg and now you wish to create a new class B that also uses the classes in pkg.

One option is to create a pkg.jar file that contains all the classes in pkg and then create separate jar files to hold A and B. In the manifest files for A.jar and B.jar you can include a Class-Path element to include pkg.jar

Then so long as pkg.jar is sent along with A.jar or B.jar, they would each reference the pkg.jar without having to worry about specifying the classpath on the command line.

Some details here: http://java.sun.com/docs/books/tutorial/deployment/jar/downman.html

Peter Richards