代码之家  ›  专栏  ›  技术社区  ›  Rahul Raj

Spring启动应用程序失败-错误Java包不存在

  •  0
  • Rahul Raj  · 技术社区  · 6 年前

    我有一个Spring引导项目,它引用一个外部模块来执行结果。

    我已经在模块依赖项下添加了外部模块:

    但是,当我运行Spring引导应用程序时,它会失败,并出现以下错误: <代码>错误:(3, 16)Java:包实例不存在

    customerLossPrediction in the below source referencing to external module and call to this module fails even thing I added the module to project workspace and dependencies are added correctly.请帮忙!

    package com.example.demo.service;
    
    import examples.customerLossPrediction;
    导入org.nd4j.linalg.api.ndarray.indarray;
    导入org.springframework.web.multipart.multipartfile;
    
    导入java.io.file;
    导入java.io.ioexception;
    
    @服务
    公共类dl4jserviceimpl实现dl4jservice{
    @重写
    公共字符串fetchprediction(multipartfile文件)引发IOException、InterruptedException{
    file convfile=新文件(file.getOriginalFileName());
    文件.transferto(convfile);
    indarray array=new customerlossprediction().generateoutput(convfile);
    返回array.toString();
    }
    }
    < /代码> 
    
    

    以下是pom.xmlif needed:。

    <?xml version=“1.0”encoding=“utf-8”?gt;
    <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。示例</groupid>
    <artifactid>演示</artifactid>
    <version>0.0.1-快照</version>
    <packaging>罐子</packaging>
    
    <name>演示</name>
    <description>Spring Boot演示项目</description>
    
    <父级>
    <groupid>org.springframework.boot</groupid>
    <artifactid>Spring引导启动程序父级</artifactid>
    <version>2.0.3.版本</version>
    <相对路径/><!--从存储库中查找父级-->
    </parent>
    
    <属性>
    <project.build.sourcencoding>utf-8</project.build.sourcencoding>
    <project.reporting.outputencoding>utf-8</project.reporting.outputencoding>
    Java.Valp& Gt;1.8 & lt;/java.版本& gt;
    </properties>
    
    <相关性>
    &!--<相关性>
    <groupid>org.springframework.boot</groupid>
    <artifactid>Spring引导启动程序数据jpa</artifactid>
    </dependency>
    <相关性>
    <groupid>org.springframework.boot</groupid>
    <artifactid>Spring启动程序数据mongodb</artifactid>
    </dependency>-->
    <相关性>
    <groupid>org.springframework.boot</groupid>
    <artifactid>春季启动启动程序thymeleaf</artifactid>
    </dependency>
    <相关性>
    <groupid>org.springframework.boot</groupid>
    <artifactid>Spring Boot Starter Web</artifactid>
    </dependency>
    
    <相关性>
    <groupid>org.springframework.boot</groupid>
    <artifactid>春季启动测试</artifactid>
    <scope>测试</scope>
    </dependency>
    <相关性>
    <groupid>org.springframework</groupid>
    <artifactid>Spring核心</artifactid>
    </dependency>
    <相关性>
    <groupid>组织.nd4j</groupid>
    <artifactid>nd4j api</artifactid>
    <版本>0.8.0</version>
    </dependency>
    </dependencies>
    
    <构建>
    <插件>
    <插件>
    <groupid>org.springframework.boot</groupid>
    <artifactid>Spring引导Maven插件</artifactid>
    <配置>
    <classifier>执行</classifier>
    </configuration>
    </plugin>
    </plugins>
    </build>
    
    
    </project>
    < /代码> 
    
    

    我认为发生这种情况是因为我试图从某种方式引用外部模块,或者另一个模块无法从弹簧引导模块引用?如果有人能澄清这一谜团,我将不胜感激:)

    我已经在模块依赖项下添加了外部模块: enter image description here

    但是,当我运行Spring引导应用程序时,它会失败,并出现以下错误: Error:(3, 16) java: package examples does not exist enter image description here

    CustomerLossPrediction在下面提到外部模块的源代码中,即使我将模块添加到项目工作区,对该模块的调用也会失败,并且依赖项也会正确添加。请帮忙!

    package com.example.demo.service;
    
    import examples.CustomerLossPrediction;
    import org.nd4j.linalg.api.ndarray.INDArray;
    import org.springframework.web.multipart.MultipartFile;
    
    import java.io.File;
    import java.io.IOException;
    
    @Service
    public class DL4JServiceImpl implements DL4JService {
        @Override
        public String fetchPrediction(MultipartFile file) throws IOException, InterruptedException {
            File convFile = new File( file.getOriginalFilename());
            file.transferTo(convFile);
            INDArray array = new CustomerLossPrediction().generateOutput(convFile);
            return array.toString();
        }
    }
    

    这里是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.example</groupId>
        <artifactId>demo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>jar</packaging>
    
        <name>demo</name>
        <description>Demo project for Spring Boot</description>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.0.3.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
    <!--        <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-jpa</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-mongodb</artifactId>
            </dependency>-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-thymeleaf</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
            </dependency>
            <dependency>
                <groupId>org.nd4j</groupId>
                <artifactId>nd4j-api</artifactId>
                <version>0.8.0</version>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <configuration>
                        <classifier>exec</classifier>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    
    
    </project>
    

    我认为发生这种情况是因为我试图从某种方式引用外部模块,或者另一个模块无法从弹簧引导模块引用?如果有人能澄清这个秘密,我将不胜感激:)

    1 回复  |  直到 6 年前
        1
  •  2
  •   Antoniossss    6 年前

    您必须将该外部模块作为maven依赖项添加。如果您未能做到这一点,即使您设法从IDE运行它,它也将无法与Maven一起构建。

    这里您已经解释了如何将JAR安装为maven工件。当你这样做的时候,把它作为依赖项包含在pom中。

    https://maven.apache.org/guides/mini/guide-3rd-party-jars-local.html