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.

FrVaBe
  • 47,963
  • 16
  • 124
  • 157

2 Answers2

-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

Community
  • 1
  • 1
Shawn Clark
  • 3,330
  • 2
  • 18
  • 30
  • 1
    That does not work with the [Spring Boot executable jar](http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#getting-started-first-application-executable-jar) format – FrVaBe Jul 28 '16 at 08:58
  • 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: https://github.com/ShawnTuatara/stack-overflow-spring-multiple-main-classes – Shawn Clark Jul 28 '16 at 09:18
  • 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. – FrVaBe Jul 28 '16 at 09:29
  • I tried to make my "speciality" clearer in the question. – FrVaBe Jul 28 '16 at 09:36
  • 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. http://one-jar.sourceforge.net/ – Shawn Clark Jul 28 '16 at 09:49
-1

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>

rual27
  • 1