Donnerstag, 14. März 2019

Start another main class within a Spring Boot jar

When you build your Spring Boot application with Maven and package it as a jar file you will start it via java -jar yourApp.jar. This will start the application with the class which is annotated with @SpringBootApplication. But what if there is another main-class you sometimes want to start?


When you normally work with Java you would do something like this:
java -classpath "yourApp.jar" your.domain.AnotherMainClass
But this will not work and you get this output instead:
Error: Could not find or load main class your.domain.AnotherMainClass

So there is something different than in normal Java jars.


Having a look into the META-INF\MANIFEST.MF within the jar shows that org.springframework.boot.loader.JarLauncher is defined there as Main-Class. After searching a while for that problem and with the information of the JarLauncher I was able to find this page https://docs.spring.io/spring-boot/docs/current/reference/html/executable-jar.html which defines a PropertiesLauncher that is able to launch another main class when defining it with the property loader.main.

So finally this worked for me:
java -classpath "yourApp.jar" -Dloader.main=your.domain.AnotherMainClass org.springframework.boot.loader.PropertiesLauncher

Keine Kommentare:

Kommentar veröffentlichen