1

Is it possible to start a different Java class (with a main method) in a Spring Boot executable jar than the declared mainClass?

Speciality: The class that should be started is located in a library that is embedded in the executable jar (inside the lib folder).

Background information:

The executable jar contains a library with a class that I would like to call to gracefully shutdown the application. This library is embedded inside the executable jars lib folder and not accessible by the default Java classpath parameter.

3 Answers 3

0

java -cp your-spring-boot.jar -Dloader.main=<Main Class Name in embedded Library> org.springframework.boot.loader.PropertiesLauncher <Pprovide any parameters required for Main Class>

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

1 Comment

Please explain what your code does and how it does it.
0

The solution from rual27 worked for me! I only had to change the spring class as it has been relocated in a different package.

So I ended up using:

java -cp my-spring-boot-executable-jar.jar  -Dloader.main=org.acme.ClassIWantToExeceute org.springframework.boot.loader.launch.PropertiesLauncher

This command launch the PropertiesLauncher class og springboot loader (which is directlye accessible as it is in the root of the jar) and use the loader.main to specify the actual class to call, which will be resolved by the launcher by looking into the nested jar in BOOT-INF.

Comments

-1

See this reply: https://stackoverflow.com/a/2023544/1499549. You just need to specify the class after defining your jar file on the classpath.

java -cp myapp.jar com.example.Main1

5 Comments

That does not work with the Spring Boot executable jar format
I just tested it and it does work. Important thing to note is you are not using the "-jar" option. Instead you are using "-cp" to just use the jar on the classpath. Sample project: github.com/ShawnTuatara/…
I think the difference is that you are calling a Main class inside the project and I want to call a Main class that is included in a dependent library. The libraries are included as jar files in the lib sub folder of the executable jar and not accessible via the -cp option.
I tried to make my "speciality" clearer in the question.
I see... the other problem with this though is that all the other jar's in the lib folder won't be loaded in the classpath unless you use the packaged entry point (executable jar). This is a similar issue to OneJar which is what the spring-boot artifact was modelled after. one-jar.sourceforge.net

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.