when I run mvn clean install the .jar is generated but when I launch it java -jar target/test-1.0.0-BUILD-SNAPSHOT.jar
I have an error
Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/boot/SpringApplication at com.acme.test.Application.main(Application.java:12) Caused by: java.lang.ClassNotFoundException: org.springframework.boot.SpringApplication at java.net.URLClassLoader$1.run(URLClassLoader.java:366) at java.net.URLClassLoader$1.run(URLClassLoader.java:355) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:354) at java.lang.ClassLoader.loadClass(ClassLoader.java:425) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) at java.lang.ClassLoader.loadClass(ClassLoader.java:358) ... 1 more
I don't understand, I have no errors in eclipse, I think the .jar is in maven depences
回答1:
Your jar does not contain the dependencies such as spring-boot which it is looking for during runtime. You can build a consolidated jar with all the dependencies as outlined in this question.
回答2:
If you would like to stay in spring boot space just set the pom packaging to jar
jar
and add the spring-boot-maven-plugin to you build properties in the pom.xml file:
org.springframework.bootspring-boot-maven-plugin
then a simple mvn package command will create a complete executable jar file.
See the very good spring reference doc for more details (doing it gradle style also) spring reference doc
回答3:
Here the packaging is jar type, hence you need to use manifest plugin, in order to add dependencies into the Manifest.mf
The problem here is that maven could find the dependencies in pom file and compile the source code and create the output jar. But when executing the jar, manifest.mf file contains no details of dependencies. Hence you got this error. This is a case of classpath errors.
Another option is to use the Apache Maven Shade Plugin: This plugin provides the capability to package the artifact in an uber-jar, including its dependencies and to shade - i.e. rename - the packages of some of the dependencies.
add this to your build plugins section
org.apache.maven.pluginsmaven-shade-plugin
回答5:
Mine was caused by a corrupt Maven repository.
I deleted everything under C:\Users\\.m2\repository.
Then did an Eclipse Maven Update, and it worked first time.
So it was simply spring-boot.jar got corrupted.
回答6:
The Answer to the Above Question is none of the Above. When you Download new STS it won't support the OLD Spring Boot Parent Version. Just update parent version with latest comes with STS it will work.
If you have problem getting the latest , Just Create a new Spring Starter Project. Goto File->New -> Spring Start Project And Create a demo project you will get the latest parent version , change your version with that all will work. I do this everytime i change STS. This work for me and is the fast fix. Anybody have Other idea please share.
回答7:
Clean your maven cache and rerun:
mvn dependency:purge-local-repository
回答8:
I had the same problem and tried most of the solutions suggested above, but none worked for me. Eventually, I rebuild my entire com.springframework (maven) repository (by simply deleting .m2/org/springworkframework directory).