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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20740703/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 03:59:05  来源:igfitidea点击:

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

javaspringmaven

提问by Ajouve

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

我正在尝试创建一个 spring mvc 应用程序。这是我的 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.acme</groupId>
    <artifactId>test</artifactId>
    <name>springmvc</name>
    <packaging>jar</packaging>
    <version>1.0.0-BUILD-SNAPSHOT</version>
    <properties>
        <org.springframework-version>3.1.1.RELEASE</org.springframework-version>
        <org.aspectj-version>1.6.10</org.aspectj-version>
        <org.slf4j-version>1.6.6</org.slf4j-version>
    </properties>

    <repositories>
        <repository>
            <id>spring-milestone</id>
            <url>http://repo.spring.io/libs-milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>spring-milestone</id>
            <url>http://repo.spring.io/libs-milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>

    <dependencies>
        <!-- Spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${org.springframework-version}</version>
            <exclusions>
                <!-- Exclude Commons Logging in favor of SLF4j -->
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>

        <!-- AspectJ -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>${org.aspectj-version}</version>
        </dependency>

        <!-- Logging -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${org.slf4j-version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
            <version>${org.slf4j-version}</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>${org.slf4j-version}</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.15</version>
            <exclusions>
                <exclusion>
                    <groupId>javax.mail</groupId>
                    <artifactId>mail</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>javax.jms</groupId>
                    <artifactId>jms</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.sun.jdmk</groupId>
                    <artifactId>jmxtools</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.sun.jmx</groupId>
                    <artifactId>jmxri</artifactId>
                </exclusion>
            </exclusions>
            <scope>runtime</scope>
        </dependency>

        <!-- @Inject -->
        <dependency>
            <groupId>javax.inject</groupId>
            <artifactId>javax.inject</artifactId>
            <version>1</version>
        </dependency>

        <!-- Servlet -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

        <!-- Test -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.7</version>
            <scope>test</scope>
        </dependency>

        <!-- Boot -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>0.5.0.M6</version>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jetty</artifactId>
            <version>0.5.0.M6</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
            <version>0.5.0.M6</version>
        </dependency>


    </dependencies>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-eclipse-plugin</artifactId>
                <version>2.9</version>
                <configuration>
                    <additionalProjectnatures>
                        <projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
                    </additionalProjectnatures>
                    <additionalBuildcommands>
                        <buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand>
                    </additionalBuildcommands>
                    <downloadSources>true</downloadSources>
                    <downloadJavadocs>true</downloadJavadocs>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                    <compilerArgument>-Xlint:all</compilerArgument>
                    <showWarnings>true</showWarnings>
                    <showDeprecation>true</showDeprecation>
                </configuration>
            </plugin>
            <!-- com.ilppa.cocktail.Application -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>com.ilppa.cocktail.Application</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <configuration>
                    <mainClass>org.test.int1.Main</mainClass>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

when I run mvn clean installthe .jar is generated but when I launch it java -jar target/test-1.0.0-BUILD-SNAPSHOT.jar

当我运行mvn clean install.jar 时生成但是当我启动它时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.run(URLClassLoader.java:366)
    at java.net.URLClassLoader.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

我不明白,我在 eclipse 中没有错误,我认为 .jar 在 maven 依赖中

采纳答案by Saket

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.

您的 jar 不包含它在运行时寻找的依赖项,例如 spring-boot。您可以使用此问题中概述的所有依赖项构建一个整合的 jar 。

回答by Himanshu Bhardwaj

Here the packaging is jar type, hence you need to use manifest plugin, in order to add dependencies into the Manifest.mf

这里打包的是jar类型,所以需要使用manifest插件,以便在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.

这里的问题是 maven 可以在 pom 文件中找到依赖项并编译源代码并创建输出 jar。但是在执行 jar 时,manifest.mf 文件不包含依赖项的详细信息。因此你得到了这个错误。这是类路径错误的一种情况。

Hereyou can find the details on how to do it.

在这里您可以找到有关如何操作的详细信息。

回答by stoffer

If you would like to stay in spring boot space just set the pom packaging to jar

如果您想留在弹簧靴空间,只需将 pom 包装设置为 jar

<packaging>jar</packaging>

and add the spring-boot-maven-plugin to you build properties in the pom.xml file:

并将 spring-boot-maven-plugin 添加到 pom.xml 文件中的构建属性中:

<build>
  <plugins>
    <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
  </plugins>
</build>

then a simple mvn package command will create a complete executable jar file.

然后一个简单的 mvn package 命令将创建一个完整的可执行 jar 文件。

See the very good spring reference doc for more details (doing it gradle style also) spring reference doc

有关更多详细信息,请参阅非常好的 spring 参考文档(也可以使用 gradle 样式) spring 参考文档

回答by Francois

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.

另一种选择是使用Apache Maven Shade Plugin:该插件提供了将工件打包到 uber-jar 中的功能,包括其依赖项,以及对某些依赖项的包进行着色(即重命名)。

add this to your build plugins section

将此添加到您的构建插件部分

<plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-shade-plugin</artifactId>
</plugin>

回答by David Lavender

Mine was caused by a corrupt Maven repository.

我的是由损坏的 Maven 存储库引起的。

I deleted everything under C:\Users\<me>\.m2\repository.

我删除了C:\Users\<me>\.m2\repository.

Then did an Eclipse Maven Update, and it worked first time.

然后做了一个 Eclipse Maven 更新,它第一次工作。

So it was simply spring-boot.jargot corrupted.

所以它只是spring-boot.jar被损坏了。

回答by user3141256

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.

上述问题的答案是“以上都不是”。当您下载新的 STS 时,它将不支持旧的 Spring Boot 父版本。只需使用 STS 附带的最新版本更新父版本即可。

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.8.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

If you have problem getting the latest, just create a new Spring Starter Project. Go to 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 every time I change STS.

如果您在获取最新版本时遇到问题,只需创建一个新的 Spring Starter 项目。转到 File->New->Spring Start Project 并创建一个演示项目,您将获得最新的父版本,更改您的版本,一切都将起作用。我每次更改 STS 时都会这样做。

回答by Abdessattar NOISSI

Clean your maven cache and rerun:

清理您的 Maven 缓存并重新运行:

mvn dependency:purge-local-repository

回答by user1693207

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

我遇到了同样的问题并尝试了上面建议的大多数解决方案,但没有一个对我有用。最终,我重建了整个 com.springframework (maven) 存储库(只需删除 .m2/org/springworkframework 目录)。

It worked for me.

它对我有用。

回答by ankit

May be your dependencies not build correctly. Check compilation issue in project.

可能是您的依赖项未正确构建。检查项目中的编译问题。

Clean and rebuild project.

清理并重建项目。

For maven project: mvn clean install

对于 Maven 项目: mvn clean install

For gradle projects: gradle clean buildor gradlew clean build

对于 gradle 项目: gradle clean buildgradlew clean build

回答by russellhoff

In my case, I did remove maven nature manually from .projectfile while having the project opened in Eclipse. So what I'd to do was to add maven nature again using the contextual menu (roght click on the project > configuration > add maven nature). Afterwards, everything worked nice :D

就我而言,.project在 Eclipse 中打开项目时,我确实从文件中手动删除了 Maven 性质。所以我要做的是使用上下文菜单再次添加 Maven 性质(右击项目 > 配置 > 添加 Maven 性质)。之后,一切都很好:D