You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ubuntu18.04.2+Java11环境中,下载的.jar文件存放何处可免-cp编译?

How to Avoid -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 java
    

    You’ll get a path like /usr/lib/jvm/java-11-openjdk-amd64/bin/java—strip off the /bin/java part to get your JDK home (e.g., /usr/lib/jvm/java-11-openjdk-amd64/).

  • Copy the MongoDB JAR to the lib/ext subdirectory 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.java directly, 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-libraries
    
  • Copy 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 CLASSPATH variable. If you use Bash (the default on Ubuntu), open ~/.bashrc with a text editor like nano:

    nano ~/.bashrc
    
  • Add 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.

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.xml file 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 compile to 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 07:59:46