Home > Java > javaTutorial > body text

How to Include JAR Files in Java Compilation via Command Prompt?

Barbara Streisand
Release: 2024-11-09 06:27:02
Original
170 people have browsed it

How to Include JAR Files in Java Compilation via Command Prompt?

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
Copy after login

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;
Copy after login

Linux/macOS:

export CLASSPATH=.:/home/path/mail.jar:/home/path/servlet.jar:
Copy after login

Further Resources:

For more detailed information on setting the CLASSPATH environment variable, refer to the following blog:

  • [How Classpath Work in Java](http://javarevisited.blogspot.com/2011/01/how-classpath-work-in-java.html)

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template