具有 maven 依赖性的 Java ClassNotFoundException [英] Java ClassNotFoundException with maven dependency

查看:42
本文介绍了具有 maven 依赖性的 Java ClassNotFoundException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am getting ClassNotFoundException and NoClassDefFoundError exceptions when I attempt to run my application using a maven defined dependency.

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

I added my maven dependency for the jar in question to my pom.xml file with the following declaration:

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

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

This added the relevant JAR file to my Maven Dependencies folder in Eclipse. I can access the classes in code but I get the mentioned exceptions once I run the application.

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

The jar is referenced in my Java build path under Maven dependencies:

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

My local maven repository is added to my classpath:

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

When I attempt to run the application, I get the following two exceptions:

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

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

谁能指出我正确的方向?

Can anyone point me in the right direction?

推荐答案

provided改为compile

提供

这很像编译,但表明您希望 JDK 或容器在运行时提供依赖项.例如,在为 Java Enterprise Edition 构建 Web 应用程序时,您可以将 Servlet API 和相关 Java EE API 的依赖项设置为提供的范围,因为 Web 容器提供这些类.此范围仅在编译和测试类路径上可用,不可传递.

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆