1

I want to start my spring-boot application in docker an i got an error in docker run

My Main-Class:

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

My 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>com.emo.test</groupId>
        <artifactId>test</artifactId>
        <version>0.1-SNAPSHOT</version>
    </parent>

    <artifactId>test-core</artifactId>
    <version>${parent.version}</version>
    <packaging>jar</packaging>

    <name>test-core</name>
    <url>http://maven.apache.org</url>

    <dependencies>
        ....
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <!-- Build an executable JAR -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>com.test.Application</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

My manifest:

Manifest-Version: 1.0
Built-By: emo.leumassi
Build-Jdk: 11.0.7
Main-Class: com.test.Application
Created-By: Apache Maven

For some reasons, i don't want to use the spring-boot-maven-plugin

My Dockerfile

FROM openjdk:8-jdk-alpine
ARG JAR_FILE=test-*.jar
COPY ${JAR_FILE} test.jar
ENTRYPOINT ["java", "-jar", "test.jar"]

I created the container with this script:

docker build -t test .

docker run -p 8080:8080 test

I get this error:

Sending build context to Docker daemon  18.94kB
Step 1/4 : FROM openjdk:8-jdk-alpine
 ---> a3562aa0b991
Step 2/4 : ARG JAR_FILE=test-*.jar
 ---> Running in 739ab5566b9f
Removing intermediate container 739ab5566b9f
 ---> 9d2e5a894070
Step 3/4 : COPY ${JAR_FILE} test.jar
 ---> 2746354c80a5
Step 4/4 : ENTRYPOINT ["java", "-jar", "test.jar"]
 ---> Running in 9850a06d6686
Removing intermediate container 9850a06d6686
 ---> e5ec33eac8a9
Successfully built e5ec33eac8a9
Successfully tagged kazi-core:latest
Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/boot/SpringApplication
        at com.test.Application.main(Application.java:10)
Caused by: java.lang.ClassNotFoundException: org.springframework.boot.SpringApplication
        at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        ... 1 more

emoleumassi
  • 4,881
  • 13
  • 67
  • 93
  • You're not getting dependencies from Maven. Instead you've them locally. Did you correctly mount those files in `lib` into docker. – Piyush Patel Jun 04 '20 at 18:34
  • If this is really a spring boot app you should not try to use maven-jar-plugin yourself. Use the spring-boot-maven-plugin. Have you tested to start your spring-boot app without docker first to check if it's correctly starting up? – khmarbaise Jun 04 '20 at 19:50
  • @khmarbaise I remove maven-jar-plugin, and I got no main manifest attribute, in /app.jar error when docker run . And I run the jar file local successfully. – alan9uo Mar 12 '21 at 03:18
  • What is this: `${parent.version}` ? Yes because the manifest main entry is done by spring-boot-maven-plugin and there is s a bootstrap within Spring Boot jar file ... if you done correctly you can start the app via `java -jar xyz.jar` if not there is something missing/wrong... I can't see everything you configured so I can't say what exactly is wrong... – khmarbaise Mar 12 '21 at 08:00
  • 1
    @emuleumassi did you find what was the problem eventually ? I have a similar issue – thahgr Jan 28 '22 at 08:56
  • @thahgr see my answer – emoleumassi Jan 29 '22 at 07:46
  • The plugin config mentioned in the answer here would help- https://stackoverflow.com/questions/54867295/springboot-no-main-manifest-attribute-maven – Caffeine Coder Apr 16 '22 at 15:37

2 Answers2

0

add maven-assembly-plugin to your pom.xml

eg:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
    </configuration>
    <executions>
        <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>
0

with spring boot we can use spring-boot-maven-plugin

 <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                        <configuration>
                            <classifier>exec</classifier>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
emoleumassi
  • 4,881
  • 13
  • 67
  • 93