简体   繁体   English

使用命令行使用maven在Java + spring helloworld应用中出现NoClassDefFoundError

[英]NoClassDefFoundError in java+spring helloworld app with maven using commandline

I have been stuck for 12 hours straight now on this error, tried every possible way . 我已经连续12个小时被这个错误困扰,尝试了所有可能的方法。 My dependencies are right 我的依赖是对的

<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.hello</groupId>
  <artifactId>helloworld</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>helloworld</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <!-- Spring  dependencies -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.3.9.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.9.RELEASE</version>
        </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

SpringBeans.xml file SpringBeans.xml文件

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">

    <bean id="helloBean" class="com.hello.HelloWorld">
        <property name="name" value="world" />
    </bean>

But I get this ----> 但是我明白了->

 Exception in thread "main" java.lang.NoClassDefFoundError:
 org/springframework/beans/factory/BeanFactory
         at java.lang.Class.getDeclaredMethods0(Native Method)
         at java.lang.Class.privateGetDeclaredMethods(Class.java:2625)
         at java.lang.Class.getMethod0(Class.java:2866)
         at java.lang.Class.getMethod(Class.java:1676)
         at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.java:494)
         at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:486)
 Caused by: java.lang.ClassNotFoundException:
 org.springframework.beans.factory.BeanFactory
         at java.net.URLClassLoader$1.run(URLClassLoader.java:359)
         at java.net.URLClassLoader$1.run(URLClassLoader.java:348)
         at java.security.AccessController.doPrivileged(Native Method)
         at java.net.URLClassLoader.findClass(URLClassLoader.java:347)
         at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
         at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:312)
         at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
         ... 6 more

I compile with maven and it builds fine but when I run it with java and necessary classpaths I get the above error. 我使用maven进行编译,并且编译良好,但是当我使用java和必要的类路径运行它时,会出现上述错误。 No clue what could be going wrong, tried googling for hours but no luck. 不知道可能出什么问题,尝试了数小时的谷歌搜索,但没有运气。

This is the command line I am using to run : 这是我用来运行的命令行:

java -cp ".:target/helloworld-1.0-SNAPSHOT.jar:lib/*" com.hello.App

where lib directory has spring-context and spring-core jar files 其中lib目录包含spring-context和spring-core jar文件

You need spring-beans jar in your runtime class path. 您需要在运行时类路径中使用spring-beans jar。 org.springframework.beans.factory.BeanFactory class is in this jar. org.springframework.beans.factory.BeanFactory类在此jar中。 I suggest handling runtime dependencies by creating fatJar or using maven dependency plugin to collect dependencies to a folder and adding this folder to runtime class path. 我建议通过创建fatJar或使用maven依赖插件将依赖项收集到文件夹并将此文件夹添加到运行时类路径来处理运行时依赖项。

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
    <version>4.3.9.RELEASE</version>
</dependency>

How can I create an executable JAR with dependencies using Maven? 如何使用Maven创建具有依赖项的可执行JAR?

https://maven.apache.org/plugins/maven-dependency-plugin/examples/copying-project-dependencies.html https://maven.apache.org/plugins/maven-dependency-plugin/examples/copying-project-dependencies.html

When using Maven, it will automatically download transitive dependencies of all the libraries you explicitly specify in your POM. 使用Maven时,它将自动下载您在POM中显式指定的所有库的可传递依赖项。 This means the list of actual dependencies may (probably will) be quite a bit larger than what your POM contains, and all of these dependencies are required to run your application. 这意味着实际依赖项列表可能会(可能会)比POM包含的列表大很多,并且所有这些依赖项都是运行应用程序所必需的。

If you want to list the complete set of dependencies use the command 如果要列出完整的依赖项集,请使用以下命令

mvn dependency:tree

However, you shouldn't need to worry about any of this if you stay completely within Maven. 但是,如果您完全呆在Maven中,则不必担心任何这些。 For example, to run your compiled project (assuming it can be run from the command line), just use mvn exec:java . 例如,要运行已编译的项目(假设可以从命令行运行),只需使用mvn exec:java If you need to package a .jar with dependencies there are options to do that as well. 如果您需要打包带有依赖项的.jar ,也可以选择这样做。

You need the list of dependencies only if you're going to try to run your code outside of Maven, such as directly from the command line with the java command. 仅当您要尝试在Maven外部运行代码时才需要依赖项列表,例如直接从命令行使用java命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM