Ubuntu18.04.2+Java11环境中,下载的.jar文件存放何处可免-cp编译?
-cp When Compiling with Third-Party JARs (Ubuntu 18.04 + Java 11) Hey Shivam! Great question—let's get that MongoDB driver JAR set up so you can compile your code without typing the -cp flag every single time. Since you're on Ubuntu 18.04 using Java 11, here are three solid approaches tailored to your setup:
1. System-Wide Extension Directory (Quick Fix)
Java has a legacy extension directory that still works reliably in Java 11 (even though it’s marked as deprecated for newer versions). Placing your JAR here makes it available to all Java projects on your system.
First, find your JDK installation path. Run this command to list available Java versions:
update-alternatives --list javaYou’ll get a path like
/usr/lib/jvm/java-11-openjdk-amd64/bin/java—strip off the/bin/javapart to get your JDK home (e.g.,/usr/lib/jvm/java-11-openjdk-amd64/).Copy the MongoDB JAR to the
lib/extsubdirectory of your JDK home. You’ll need sudo permissions since this is a system folder:sudo cp /home/shivam/Downloads/mongo-java-driver-3.10.2.jar /usr/lib/jvm/java-11-openjdk-amd64/lib/ext/That’s it! Now you can run
javac HelloWorld.javadirectly, and the compiler will automatically pick up the MongoDB classes.
Note: This affects all users on your machine. If you only want the JAR available to your user, skip to the next option.
2. User-Specific CLASSPATH Environment Variable
This method lets you set a classpath that only applies to your user, so you don’t need sudo access.
First, create a dedicated folder to store your third-party JARs (keeps things organized):
mkdir -p ~/java-librariesCopy the MongoDB JAR into this folder:
cp /home/shivam/Downloads/mongo-java-driver-3.10.2.jar ~/java-libraries/Edit your shell profile file to set the
CLASSPATHvariable. If you use Bash (the default on Ubuntu), open~/.bashrcwith a text editor like nano:nano ~/.bashrcAdd this line at the end of the file (it adds your JAR folder to the existing classpath):
export CLASSPATH="$CLASSPATH:$HOME/java-libraries/*"The
*means all JARs in the folder will be included—handy if you add more libraries later.Save the file (Ctrl+O, then Enter in nano) and exit (Ctrl+X). Then reload the profile to apply changes immediately:
source ~/.bashrc
Now any new terminal window will use this classpath, and you can compile without -cp.
3. Use a Build Tool (Recommended for Larger Projects)
If you’re planning to work on bigger Java projects, tools like Maven or Gradle are the industry standard for managing dependencies. They handle downloading JARs and setting classpaths automatically, so you never have to mess with -cp again.
For example, with Maven:
Create a
pom.xmlfile in your project folder with this dependency snippet for the MongoDB driver:<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.yourname</groupId> <artifactId>your-project</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.mongodb</groupId> <artifactId>mongo-java-driver</artifactId> <version>3.10.2</version> </dependency> </dependencies> </project>Run
mvn compileto build your project—Maven will download the driver JAR automatically and handle the classpath for you.
This approach is scalable, keeps your projects clean, and is what most professional Java developers use.
内容的提问来源于stack exchange,提问作者Shivam Kumar

