To compile Java programs that rely on external libraries, these libraries must be included in the classpath. This article explores how to incorporate jars into the classpath using command-line options for javac or apt.
For javac, use the -cp option followed by a colon-separated list of directories and jar files. For example, to compile HelloImp.java with the jsr181-api.jar library:
javac -cp .:jsr181-api.jar HelloImp.java
apt also supports -cp to set the classpath. However, it recommends using the -module-path option for modular Java applications. For example:
apt -module-path .:jsr181-api jar HelloImp
If you prefer not to permanently modify your command-line arguments, you can use the java command with the -cp option:
java -cp .:jsr181-api.jar HelloImp
This will set the classpath temporarily for the duration of the command.
The manifest text file approach can be more versatile. Create a manifest file named MANIFEST.MF with the following content:
Manifest-Version: 1.0 Class-Path: jsr181-api.jar
Then, compile the program with:
javac HelloImp.java -m MANIFEST.MF
The above is the detailed content of How to Include JAR Files in the Classpath for Java Compilation (javac and apt)?. For more information, please follow other related articles on the PHP Chinese website!