怎么查看java主类,Java运行时如何找到我的主类?

I am learning Spring Boot. I made a simple Spring Boot project that can output a hello world string at http://localhost:8080/welcome

I use Maven to build my project that would output a jar file.

To start up my spring boot app, I use the command as below

java -jar my-springboot-app.jar

My question is:

How is java smart enough to locate my main class and its main method (e.g. the application launcher)?

I checked the jar file and browsed those BOOT-INF & META-INF and could not find any clues.

Does the spring boot framework (@SpringBootApplication) or maven automatically do the magic for me?

解决方案

In case of spring boot jar the things are little bit more complicated than regular jar. Mainly because spring boot applicaton jar is not really a JAR (by jar I mean something that has manifest and compiled classes). Regular JARs can be "recognized" and processed by jvm, however in Spring Boot there are also packed dependencies (take a look at BOOT-INF/lib) so its jars inside JARs. How to read this?

It turns out that spring boot always has its own main class that is indeed referred to in MANIFEST.MF and this a real entry point of the packaged application.

The manifest file contains the following lines:

Main-Class: org.springframework.boot.loader.JarLauncher

Start-Class: com.example.demo.DemoApplication

Main-Class is a JVM entry point. This class, written by spring developers, basically does two things:

- Establishes a special class loader to deal with a "non-regular-jar" nature of spring boot application. Due to this special class loaders spring boot application that contains "jars" in BOOT-INF/lib can be processed, for example, regular java class loaders apparently cannot do this.

- Calls the main method of Start-Class value. The Start-Class is a something unique to spring boot applications and it denotes the class that contains a "main" method - the class you write and the class you think is an entry point :) But from the point of view of the spring boot infrastructure its just a class that has an "ordinary" main method - a method that can be called by reflection.

Now regarding the question "who builds the manifest":

This MANIFEST.MF is usually created automatically by plugins offered by Spring Developers for build systems like Maven or Gradle.

For example, the plugin looks like this:

org.springframework.boot

spring-boot-maven-plugin

During its work, this plugin identifies your main class (com.example.demo.DemoApplication in my example). This class is marked with @SpringBootApplication annotation and has a public static void main method.

However, if you put many classes like this the plugin probably won't recognize the correct class so you'll need to configure the plugin properties in POM.xml to specify the right class.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值