【问题标题】:java.lang.ClassNotFoundException: org.hamcrest.Matchers after adding dependency too pom.xmljava.lang.ClassNotFoundException: org.hamcrest.Matchers 添加依赖后 pom.xml
【发布时间】:2017-06-18 11:48:36
【问题描述】:

当我启动肥皂服务应用程序时,我从控制台收到此错误

java.lang.ClassNotFoundException: org.hamcrest.Matchers

经过研究,我在pom.xml中添加了这三个依赖,启动应用时仍然存在错误

<dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.12</version>
          <scope>test</scope>
        </dependency>
        <dependency>
          <groupId>org.hamcrest</groupId>
          <artifactId>hamcrest-library</artifactId>
          <version>1.3</version>
          <scope>test</scope>
        </dependency>
         <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-core</artifactId>
            <version>1.3</version>
            <scope>test</scope>
         </dependency>

感谢收到的任何帮助。

【问题讨论】:

  • 使用 hamcrest-all 代替 hamcrest-library 和 hamcrest-core。
  • 刚刚做了,重新启动后结果相同
  • 您在项目的哪个部分使用Matcher 类?由于 pom 仅在测试范围内提供 lib,因此您只能在单元测试中使用它(/src/test/java 中的 java 文件)。

标签: java xml spring soap


【解决方案1】:

你可以试试下面的:

<dependency>
    <groupId>org.hamcrest</groupId>
    <artifactId>hamcrest-all</artifactId>
    <version>1.3</version>        
</dependency>

【讨论】:

  • 如果这行得通,那么您在错误的范围内使用了 Matcher 类。单元测试应该在 maven 的 test 阶段运行。
【解决方案2】:

此错误表明 hamcrest 的库不存在于您应用的类路径中。

如果您在运行时使用 org.hamcrest.Matchers 类,如果您的中间件提供了 hamcrest 库,则有必要更改您的编译范围或提供。

测试范围仅在测试生命周期中使用该库。

您可以在maven documentation阅读更多内容

【讨论】:

    猜你喜欢
    • 2021-09-19
    • 2011-08-05
    • 2023-04-08
    • 2017-01-05
    • 2015-01-16
    • 2013-07-12
    • 2022-11-29
    • 2016-11-27
    • 2013-11-03
    相关资源
    最近更新 更多