Executing an Arbitrary Main Class from a Spring Boot JAR: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
No edit summary
No edit summary
 
Line 6: Line 6:


A Spring Boot JAR comes with all an application needs to function, and the application can be started as simply as:
A Spring Boot JAR comes with all an application needs to function, and the application can be started as simply as:
 
<font size=-1>
  java -jar my-app.jar
  java -jar my-app.jar
 
</font>
This works because there is a well defined entry point that bootstraps the Spring Boot runtime. The application bytecode is maintained under a BOOT-INF directory inside the JAR. However, this interferes with an attempt to run an arbitrary main class as follows:
This works because there is a well defined entry point that bootstraps the Spring Boot runtime. The application bytecode is maintained under a BOOT-INF directory inside the JAR. However, this interferes with an attempt to run an arbitrary main class as follows:
 
<font size=-1>
  java -cp my-app.jar com.example.Main
  java -cp my-app.jar com.example.Main
 
</font>
If "com.example.Main" is present under BOOT-INFO, it can be executed as follows:
If "com.example.Main" is present under BOOT-INFO, it can be executed as follows:
 
<font size=-1>
  java -cp my-app.jar -Dloader.main=com.example.Main org.springframework.boot.loader.PropertiesLauncher ''arg0'', ''arg1'', ....
  java -cp my-app.jar -Dloader.main=com.example.Main org.springframework.boot.loader.PropertiesLauncher ''arg0'', ''arg1'', ....
</font>

Latest revision as of 00:36, 7 February 2022

Internal

Overview

A Spring Boot JAR comes with all an application needs to function, and the application can be started as simply as:

java -jar my-app.jar

This works because there is a well defined entry point that bootstraps the Spring Boot runtime. The application bytecode is maintained under a BOOT-INF directory inside the JAR. However, this interferes with an attempt to run an arbitrary main class as follows:

java -cp my-app.jar com.example.Main

If "com.example.Main" is present under BOOT-INFO, it can be executed as follows:

java -cp my-app.jar -Dloader.main=com.example.Main org.springframework.boot.loader.PropertiesLauncher arg0, arg1, ....