java.lang.ClassNotFoundException: org.springframework.boot.SpringApplication Maven

匿名 (未验证) 提交于 2019-12-03 01:12:01

问题:

I'm trying to create a spring mvc application. This is my pom.xml

4.0.0com.acmetestspringmvcjar1.0.0-BUILD-SNAPSHOT3.1.1.RELEASE1.6.101.6.6spring-milestonehttp://repo.spring.io/libs-milestonefalsespring-milestonehttp://repo.spring.io/libs-milestonefalseorg.springframeworkspring-context${org.springframework-version}commons-loggingcommons-loggingorg.springframeworkspring-webmvc${org.springframework-version}org.aspectjaspectjrt${org.aspectj-version}org.slf4jslf4j-api${org.slf4j-version}org.slf4jjcl-over-slf4j${org.slf4j-version}runtimeorg.slf4jslf4j-log4j12${org.slf4j-version}runtimelog4jlog4j1.2.15javax.mailmailjavax.jmsjmscom.sun.jdmkjmxtoolscom.sun.jmxjmxriruntimejavax.injectjavax.inject1javax.servletservlet-api2.5providedjavax.servlet.jspjsp-api2.1providedjavax.servletjstl1.2junitjunit4.7testorg.springframework.bootspring-boot-starter-web0.5.0.M6org.springframework.bootspring-boot-starter-tomcatorg.springframework.bootspring-boot-starter-jetty0.5.0.M6org.springframework.bootspring-boot-starter-actuator0.5.0.M6maven-eclipse-plugin2.9org.springframework.ide.eclipse.core.springnatureorg.springframework.ide.eclipse.core.springbuildertruetrueorg.apache.maven.pluginsmaven-compiler-plugin2.5.11.61.6-Xlint:alltruetrueorg.apache.maven.pluginsmaven-jar-plugin2.4com.ilppa.cocktail.Applicationorg.codehaus.mojoexec-maven-plugin1.2.1org.test.int1.Mainorg.springframework.bootspring-boot-maven-plugin

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.

Here you can find the details on how to do it.



回答4:

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.

org.springframework.bootspring-boot-starter-parent1.5.8.RELEASE

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).

It worked for me.



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!