Including JAR Files in Java Compilation via Command Prompt
When developing Java applications that rely on external libraries, it becomes necessary to include the corresponding JAR files during compilation. Here's how to accomplish this using the command prompt:
Including JARs with "javac"
To include JAR files directly in the compilation command, use the "-cp" (classpath) option:
javac -cp ".:/home/path/mail.jar:/home/path/servlet.jar;" MyJavaFile.java
Replace the ".": (current directory) with the path to your JAR files, and separate the paths with colons. Alternatively, you can use "-classpath" instead of "-cp".
Setting the CLASSPATH Environment Variable
To avoid having to specify JAR paths every time, you can set the CLASSPATH environment variable. This stores the paths to the required JARs and classes, eliminating the need to include them individually:
Windows:
set CLASSPATH=.;/home/path/mail.jar;/home/path/servlet.jar;
Linux/macOS:
export CLASSPATH=.:/home/path/mail.jar:/home/path/servlet.jar:
Further Resources:
For more detailed information on setting the CLASSPATH environment variable, refer to the following blog:
The above is the detailed content of How to Include JAR Files in Java Compilation via Command Prompt?. For more information, please follow other related articles on the PHP Chinese website!