0

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

4 Answers 4

4

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.

2
  • I have to send this code to someone. Should they be doing the same to compile or is there an even easier way?
    – user201788
    Jul 18, 2011 at 22:08
  • If you need to send the source code and they need to produce compiled classes from it, then they'll need to do this compilation step. Alternatively, if they do not need the source and want only the compiled classes, then you can create a jar file which can then be used with a java -jar [yourjar.jar] classname args... command to execute (described in @antlersoft's answer)
    – brabster
    Jul 18, 2011 at 22:11
2

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.

1

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

0

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.

1
  • If you are talking about CLASSPATH for compiling, Brabster's answer is appropriate.
    – antlersoft
    Jul 18, 2011 at 22:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.