How to run main class packed in springboot jar

My project uses springboot, after pack it to jar file, I can execute java -jar my-project.jar to run the instance.

I also have a script which is a main class, when I execute java -cp my-project.jar com.test.MyScript, it says Could not find or load main class.

How can this happen? By the way, when I execute java -cp my-project.jar com.test.MyApplication, the same error occurs. MyApplication is the SpringBootApplication class.

Why can't I manually run the main class?

1 answer

  • answered 2018-04-17 05:43 Yogesh Badke

    That is because spring boot doesn't use the SpringBootApplication as the main entry point. It uses org.springframework.boot.loader.Launcher.

    Excerpt from the spring docs:

    The org.springframework.boot.loader.Launcher class is a special bootstrap class that is used as an executable jar’s main entry point. It is the actual Main-Class in your jar file, and it is used to setup an appropriate URLClassLoader and ultimately call your main() method.