具有 Maven 依赖的 Java ClassNotFoundException

标签 java maven noclassdeffounderror classnotfoundexception

当我尝试使用 Maven 定义的依赖项运行应用程序时,出现 ClassNotFoundExceptionNoClassDefFoundError 异常。

我使用以下声明将相关 jar 的 Maven 依赖项添加到我的 pom.xml 文件中:

<dependency>
    <groupId>spy</groupId>
    <artifactId>spymemcached</artifactId>
    <version>2.8.4</version>
    <scope>provided</scope>
</dependency>

这会将相关的 JAR 文件添加到 Eclipse 中的 Maven 依赖项文件夹中。我可以访问代码中的类,但是一旦运行应用程序,我就会遇到上述异常。

该 jar 在我的 Java 构建路径中的 Maven 依赖项下引用:

Java build path

我的本​​地 Maven 存储库已添加到我的类路径中:

screenshot

当我尝试运行该应用程序时,出现以下两个异常:

java.lang.NoClassDefFoundError: Lnet/spy/memcached/MemcachedClient;

java.lang.ClassNotFoundException: net.spy.memcached.MemcachedClient

有人能指出我正确的方向吗?

最佳答案

provided更改为compile

已提供

This is much like compile, but indicates you expect the JDK or a container to provide the dependency at runtime. For example, when building a web application for the Java Enterprise Edition, you would set the dependency on the Servlet API and related Java EE APIs to scope provided because the web container provides those classes. This scope is only available on the compilation and test classpath, and is not transitive.

关于具有 Maven 依赖的 Java ClassNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42132517/

相关文章:

java - 使用 OutputStreamWriter 写入 : line by line vs all at once

java - java中JSON转换结果失败(JSONObject无法转换为JSONArray)

maven - 在 pom.xml 中包含 vaadin-cdi 依赖项足以使 WAR 无法部署。为什么?

java.lang.NoClassDefFoundError : android. support.v7.app.AppCompatDelegateImplV14 在Android Studio

java - Osgi 中的 NoClassDefFoundError w/Felix, Ant

java - 我们可以将 Atomikos 事务管理器与 Tomcat JDBC XA 连接池一起使用吗

java - Android Studio导入库项目后如何生成R.java?

java - 模拟在 Spock 框架中不起作用

java - 多模块项目中,sonar分析绑定(bind)maven生命周期,如何让SonarQube模块只分析一次项目?

java - org/apache/poi/ss/usermodel/Sheet 的 NoClassDefFoundError Jar 错误