1

I'm having a Spring Boot project referring to an external module to execute the results.

enter image description here

I have already added the external module under module dependencies:

enter image description here

However when I run the Spring Boot application, it fails with this error: Error:(3, 16) java: package examples does not exist

enter image description here

CustomerLossPrediction in the below source referring to external module and call to this module fails even though I added the module to project workspace and dependencies are added correctly.

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();
    }
}

Here is the pom.xml if needed:

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

I'm thinking it happens because I tried to refer an external module that is some way or the other cannot refer from Spring Boot module?

3
  • Have you specified the bean in the component annotation tag ? Commented Jul 24, 2018 at 5:41
  • CustomerLossPrediction isn't a spring bean, it's a plain java class on external module. Commented Jul 24, 2018 at 5:43
  • Can you run main method through IDE? Like opening the DemoApplication.java file in IntelliJ and click the green button appearing on the left bar Commented Jul 24, 2018 at 5:53

1 Answer 1

3

You have to add that external module as maven dependency. If you fail to do so, even if you manage to run it from IDE, it will fail to build with Maven.

Here you have explained how to install JAR as Maven artifact. When you do that, include it as dependency in POM

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

Sign up to request clarification or add additional context in comments.

12 Comments

Thanks for the great info. Quick question, is it possible to add a directory dependency or something like that without needing to create a jar out of external module?
I don't know that, never needed it. JAR is natural distribution package, so what you would try to do would be unusual in entire ecosystem. Either add sources to your main project, or create JAR of dependency and add via maven.
Just followed your steps. Created a jar from external module. Selected the only class (main) where all implementation+ main method is present. Here is the command: mvn install:install-file -Dfile=dl4j.jar -DgroupId=dl4j -DartifactId=dl4j -Dversion=1.0.0 -Dpackaging=jar. Then jar file dependency is added in the pom.xml file. But now when I execute the spring boot application, I get this error: java.lang.ClassNotFoundException: examples.CustomerLossPrediction
Did you add it to POM ?
Yes, I added :(
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.