简体   繁体   English

如何在另一个应用程序中的一个应用程序中使用Java类?

[英]How to use a Java class from an application in another application?

I have a class in an application (it is a Spring Boot project), for example: 我在应用程序中有一个类(它是一个Spring Boot项目),例如:

package com.exemple.spring.springboot.model;

public class Employee {
   ...
}

And in the pom.xml I have this: 在pom.xml中,我有这个:

<?xml version="1.0" encoding="UTF-8"?>
<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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.exemple.spring</groupId>
    <artifactId>spring-boot</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    ...

Now I want to use this class Employee in another application like this: 现在,我想在另一个这样的应用程序中使用此类Employee:

package com.exe.spring.controller;

import org.springframework.stereotype.Controller;
import com.exemple.spring.springboot.model.Employee;


@Controller
public class EmployeeController {

    public Employee getAnEmployee() {
       ...
    }
}

I've build the JAR with mvn clean install for the first app and I've added it in the pom.xml of the second app like this: 我已经为第一个应用程序使用mvn clean install构建了JAR,并将其添加到第二个应用程序的pom.xml中,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<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.exe.spring</groupId>
    <artifactId>rest-client</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>rest-client</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>com.exemple.spring</groupId>
            <artifactId>spring-boot</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

    </dependencies>

And the problem is that I cannot import the Employee class in the EmployeeController class. 问题是我无法在EmployeeController类中导入Employee类。 I've imported it manually like this import com.exemple.spring.springboot.model.Employee ;, but Intellij show me an error: Cannot resolve symbol 'springboot'. 我已经像import com.exemple.spring.springboot.model.Employee ;一样手动导入了它,但是Intellij向我显示了一个错误:无法解析符号“ springboot”。 I see that the dependency is presented in the local repository. 我看到依赖性在本地存储库中提供。 It is very weird because Eclipse doens't see anything to import for Employee, and Intellij see the class but when I try to click on "Import class" it doesn't import anything. 这很奇怪,因为Eclipse没有看到要为Employee导入的任何内容,而Intellij却看到了该类,但是当我尝试单击“导入类”时,它什么也没有导入。 What can I do to import it correctly? 我该怎么做才能正确导入它? Thank you! 谢谢!

You can not directly add it as dependency, it has to be present in one of certral/ local / remote repository. 您不能直接将其添加为依赖项,它必须存在于certral / local / remote仓库之一中。

You need to add the jar to repo, eg local repoository, 您需要将罐子添加到仓库中,例如本地仓库,

mvn install:install-file -DlocalRepositoryPath=repo -DcreateChecksum=true -Dpackaging=jar -Dfile=[your-jar] -DgroupId=[...] -DartifactId=[...] -Dversion=[...]

And then add repository as, 然后将存储库添加为

<repository>
    <id>repo</id>
    <url>file://${project.basedir}/repo</url>
</repository>

Check other optios here 在这里检查其他选项

Check follow: 检查如下:

  • You've used mvn clean install to build and install the first JAR. 您已经使用mvn clean install来构建和安装第一个JAR。
  • You have your JAR in the classpath of the second app. 您的第二个应用程序的类路径中有JAR。 You can see all of them in "External libraries" in idea. 您可以在“外部库”中看到所有这些内容。 If not, reimport maven project or use auto-import option 如果不是,请重新导入Maven项目或使用自动导入选项

Also you can use different maven project structure eg : 您也可以使用不同的Maven项目结构,例如:

Common parent
 |
 |-------> first jar 
 |
 |-------> second jar (with first one as a dependency)

I think it should work better for you. 我认为它应该对您更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM