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.xmlfile and add a dependency entry pointing to the target project. Replace the placeholders with the actualgroupId,artifactId, andversionfrom the target project'spom.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(orbuild.gradle.ktsfor Kotlin DSL), add the dependency to thedependenciesblock: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.
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.examplepackage, compile it with:
Then package it into a JAR:javac com/example/TargetClass.javajar 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 selectImport 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 selectModule 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

