I tried to change the Spring Boot class loader.. but it is not working, only print this message:
Error: Could not find or load main class com.example.MyMainClass
I have tried this solution: https://github.com/dkublik/spring-boot-loader-play
I tried to change the Spring Boot class loader.. but it is not working, only print this message:
Error: Could not find or load main class com.example.MyMainClass
I have tried this solution: https://github.com/dkublik/spring-boot-loader-play
If you are using Spring Boot 2.1.5.RELEASE, the following should work:
java -cp "yourfile.jar" org.springframework.boot.loader.JarLauncher
Please, note that the spring-boot-maven-plugin
repackages the original jar file at maven's package
build phase, which results in a jar with a different internal file structure. In this new/repackaged jar, you can find the classes from your project inside BOOT-INF/classes/
, which differs from the original jar, where you find them in the first level of the file structure. The plugin keeps the original jar file as yourfile.jar.original
inside the target
folder.
Also, i suggest to take a look at the file META-INF/MANIFEST.MF
inside the repackaged jar, which reflects the description above. If you open this file you should see something similar to the following:
Manifest-Version: 1.0
Implementation-Title: yourproject
Implementation-Version: 1.0.0-SNAPSHOT
Start-Class: com.example.MyMainClass
Spring-Boot-Classes: BOOT-INF/classes/
Spring-Boot-Lib: BOOT-INF/lib/
Build-Jdk-Spec: 1.8
Spring-Boot-Version: 2.1.5.RELEASE
Created-By: Maven Archiver 3.4.0
Main-Class: org.springframework.boot.loader.JarLauncher
Please, note that the value of the Main-Class
header from repackaged jars will always be org.springframework.boot.loader.JarLauncher
, defining this as the entry point of your jar. Later on, Spring will look for the Start-Class
header, thus loading your project through IoC (Inversion of Control).
For more info:
fat jar
. This way you can run your application with
java -cp example.jar
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
<configuration>
<finalName>example</finalName>
</configuration>
</plugin>