具有 maven 依赖项的 Java ClassNotFoundException

标签 java maven noclassdeffounderror classnotfoundexception

当我尝试使用 maven 定义的依赖项运行我的应用程序时,我收到 ClassNotFoundExceptionNoClassDefFoundError 异常。

我将我的 Maven 依赖项添加到我的 pom.xml 文件中,声明如下:

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

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

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

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

相关文章:

java - 适用于 oracle 和 HSQL 的查询

spring-boot - 为什么我们需要在 Spring Boot 中为 log42j 添加额外的依赖?

android - 如何解决 NoClassDefFoundError : Failed resolution of: Lcom/google/android/gms/common/R$string on Android 的问题

Java SE 1.7 ClassDefNotFound

android - Android 上带有 GoogleAccountCredential.newChooseAccountIntent() 的 java.lang.NoClassDefFoundError

Java Swing JTextField - 只有数字

java - SWT下的Swing组件可以加速吗?

java - 启动Docker容器时获取java.lang.ClassNotFoundException

java - org.springframework.beans.factory.BeanInitializationException : Could not load properties

java - Android布局边距使宽度为0