java - 使用命令行使用 maven 在 java+spring hello world 应用程序中出现 NoClassDefFoundError

标签 java spring maven

我已经被这个错误困住了 12 个小时,尝试了各种可能的方法。 我的依赖是正确的

<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 文件

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

但我明白了 ---->

 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

我用maven编译,它构建得很好,但是当我用java和必要的类路径运行它时,我得到了上面的错误。不知道可能出了什么问题,尝试谷歌搜索几个小时,但没有运气。

这是我用来运行的命令行:

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

其中 lib 目录包含 spring-context 和 spring-core jar 文件

最佳答案

使用 Maven 时,它将自动下载您在 POM 中明确指定的所有库的传递依赖项。这意味着实际依赖项列表可能(可能会)比 POM 包含的内容大很多,并且运行应用程序需要所有这些依赖项。

如果您想列出完整的依赖项集,请使用命令

mvn dependency:tree

但是,如果您完全使用 Maven,则无需担心这些。例如,要运行已编译的项目(假设可以从命令行运行),只需使用mvn exec:java。如果您需要打包带有依赖项的 .jar ,也可以选择执行此操作。

仅当您要尝试在 Maven 外部运行代码时才需要依赖项列表,例如直接使用 java 命令从命令行运行。

关于java - 使用命令行使用 maven 在 java+spring hello world 应用程序中出现 NoClassDefFoundError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49469083/

相关文章:

java - 在 Android Maven 项目中使用纯 Java 库

java - Java 8 中的 interned 字符串是否适合垃圾回收?

java - 5+5+5+5+5 和 5*5 哪个更快?

java - 我可以关闭/重新打开 InputStream 来模拟不支持标记的输入流的标记/重置吗?

java - 不受 Spring 管理的域对象上的 AspectJ 切入点表达式

java - 配置spring缓存咖啡因

java - 多模块项目的 Maven 报告和站点生成

java - 无法读取 Maven 的setting.xml 文件

java - 为什么这个 if 语句不起作用?

java - Spring 数据存储库 StackOverflow