简体   繁体   中英

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:

package com.exemple.spring.springboot.model;

public class Employee {
   ...
}

And in the pom.xml I have this:

<?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:

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:

<?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. I've imported it manually like this import com.exemple.spring.springboot.model.Employee ;, but Intellij show me an error: Cannot resolve symbol '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. 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.

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.
  • You have your JAR in the classpath of the second app. You can see all of them in "External libraries" in idea. If not, reimport maven project or use auto-import option

Also you can use different maven project structure eg :

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

I think it should work better for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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