user201788
user201788

Reputation:

Compiling without having to put project files under classpath

I want someone to be able to compile the code without then having to set the CLASSPATH to the project directory. Is this possible?

Upvotes: 0

Views: 215

Answers (4)

Ryan Stewart
Ryan Stewart

Reputation: 128879

Use a build tool, like Ant or Maven. They use a project descriptor of one sort or another to manage the classpath for you.

Upvotes: 1

Michael Borgwardt
Michael Borgwardt

Reputation: 346347

The classpath needs to be set somehow, though using the environment variable is not recommended these days. Typically, projects that people are supposed to compile for themselves are distributed with either an Apache Ant build script or a Maven POM descriptor. Both of these build automation systems can do much more than just compilation, but both require some time to learn if you haven't used them before. However, it is time well spent, as they can save you a lot of time on all kinds of repetitive tasks, and both are very commonly used in Java projects.

Upvotes: 2

antlersoft
antlersoft

Reputation: 14786

Sounds like you want to create a .jar

The jar tool in the JDK puts your .class files in an archive. Then you can just reference the archive on the classpath or with the -jar flag for the java executable.

If you create a manifest for the jar, you can make the .jar runnable with a click from Windows and most other desktop environments.

Upvotes: 0

brabster
brabster

Reputation: 43580

You can set the classpath to be used for the compilation as a parameter passed to the compiler instead of setting an environment variable. The parameter is -classpath or -cp.

Upvotes: 4

Related Questions