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

Java技术问询:同一机器下如何在一个项目中引用另一项目的类

Hey there! I get it—you're working on a Java project and need to use a class that lives in another project on the same machine. Let's walk through the most practical ways to make this happen, depending on your setup:

Most modern Java projects use build tools like Maven or Gradle, which make dependency management a breeze. Here's how to do it for each:

Maven

  • First, navigate to the project that contains the class you need. Run this command in its root directory to package it and install it into your local Maven repository (usually at ~/.m2/repository):
    mvn clean install
    
  • Next, open your current project's pom.xml file and add a dependency entry pointing to the target project. Replace the placeholders with the actual groupId, artifactId, and version from the target project's pom.xml:
    <dependency>
        <groupId>com.yourcompany</groupId>
        <artifactId>target-project-name</artifactId>
        <version>1.0.0</version>
    </dependency>
    
  • Refresh your Maven dependencies (in your IDE, this is usually a "Reload All Maven Projects" option), and you'll be able to import and use the target class just like any other class in your project.

Gradle

  • For a Gradle-based target project, run this command to publish it to your local Maven repository:
    ./gradlew publishToMavenLocal
    
  • Then, in your current project's build.gradle (or build.gradle.kts for Kotlin DSL), add the dependency to the dependencies block:
    dependencies {
        implementation 'com.yourcompany:target-project-name:1.0.0'
    }
    
  • If you don't want to publish to the local repo, you can also reference the target project's built JAR directly:
    dependencies {
        implementation files('/full/path/to/target-project/build/libs/target-project-1.0.0.jar')
    }
    
  • Sync your Gradle project, and you're good to go.
2. Manually Add the Target JAR to Your Classpath

If you're not using a build tool, you can manually handle the classpath yourself:

  • First, compile and package the target project into a JAR file. For example, if your target class is in the com.example package, compile it with:
    javac com/example/TargetClass.java
    
    Then package it into a JAR:
    jar cvf target-project.jar com/example/TargetClass.class
    
  • When compiling your current project's classes, include the target JAR in the classpath:
    javac -cp /path/to/target-project.jar YourCurrentClass.java
    
  • To run your compiled class, also include the JAR (and your current class's directory) in the classpath:
    # On Linux/macOS
    java -cp .:/path/to/target-project.jar YourCurrentClass
    
    # On Windows
    java -cp .;C:\path\to\target-project.jar YourCurrentClass
    

If you're using an IDE like IntelliJ IDEA or Eclipse, you can link the two projects directly so the IDE handles dependencies automatically:

IntelliJ IDEA

  • Open your current project, then go to File > Project Structure > Modules.
  • Click the + button in the top-left corner and select Import Module. Choose the root directory of the target project.
  • Once imported, go back to your current project's module settings, navigate to Dependencies, click +, and select Module Dependency. Pick the target project's module from the list.

Eclipse

  • Import both your current project and the target project into your Eclipse workspace.
  • Right-click your current project, select Build Path > Add Projects....
  • Check the box next to the target project and click OK.

Quick Note

Don't forget: The class you want to reference must be declared as public! Non-public classes can only be accessed within their own package, so they won't be visible to your other project.

内容的提问来源于stack exchange,提问作者Abhi

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 10:04:16