4

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?

xuanzhui
  • 1,300
  • 4
  • 12
  • 30
  • Correct alternative can be found here https://stackoverflow.com/a/61601687/3805988 – Ram Jul 29 '20 at 23:58

1 Answers1

1

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.

Yogesh Badke
  • 4,249
  • 2
  • 15
  • 23