Issue
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
Solution
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>
Answered By - emoleumassi
Answer Checked By - Dawn Plyler (JavaFixing Volunteer)