【问题标题】:Why I'm getting ClassNotFoundException while the package is listed as dependency in the pom.xml为什么我在 pom.xml 中将包列为依赖项时收到 ClassNotFoundException
【发布时间】:2017-12-19 20:40:14
【问题描述】:

我刚刚问过another question,因为我遇到了一些 ClassNotFoundException。

Caused by: java.lang.ClassNotFoundException: org.elasticsearch.plugins.NetworkPlugin
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[na:1.8.0_131]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_131]
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335) ~[na:1.8.0_131]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_131]
    ... 56 common frames omitted

有人向我指出,这个异常意味着在运行时在类路径中找不到所提到的类。这是我在 pom.xml 文件中有容器包的情况。

<dependency>
    <groupId>org.elasticsearch</groupId>
    <artifactId>elasticsearch</artifactId>
    <version>5.5.0</version>
</dependency>

所以现在我的问题是,如何将包命名为依赖项但其中的某些类在运行时丢失?而且,mvn dependency:tree -Dverbose -Dincludes=commons-collections 不会返回任何冲突。

这是我在调试模式下使用 IntelliJ Idea 运行的 Spring Boot 项目。我还在我的代码中以完全相同的方式使用了十几个其他包,它们都运行顺利。但是一旦我添加了 Elasticsearch,我就会遇到上面提到的异常。

【问题讨论】:

  • 发布所有堆栈跟踪。有人在这里使用魔法。

标签: java maven intellij-idea


【解决方案1】:

您可以尝试从您的计算机中删除下载的依赖项吗?

它位于~/.m2/repository/org/elasticsearch 假设您将 maven 依赖项保留在默认位置并在 Mac OS X 上运行

rm -rf ~/.m2/repository/org/elasticsearch/*

然后尝试使用重新下载依赖项

mvn dependency:resolve

【讨论】:

    【解决方案2】:

    您可以尝试使用一种波纹管版本的 elasticsearch` 依赖项。您可以从以下依赖项中尝试一种依赖项:-

    <dependency>
    <groupId>org.elasticsearch</groupId>
    <artifactId>elasticsearch</artifactId>
    <version>5.4.3</version>
    </dependency>
    

    <dependency>
    <groupId>org.elasticsearch</groupId>
    <artifactId>elasticsearch</artifactId>
    <version>6.0.0-alpha2</version>
    </dependency>
    

    【讨论】:

      【解决方案3】:

      您是否尝试过清理和构建?因为有时当您向 pom.xml 添加依赖项时,您会看到库正在下载,但如果您尝试在不构建的情况下执行项目,则会出现该错误。

      【讨论】:

        【解决方案4】:

        你错过了插件部分。

        <!-- https://mvnrepository.com/artifact/org.elasticsearch.plugin/plugins -->
        <dependency>
            <groupId>org.elasticsearch.plugin</groupId>
            <artifactId>plugins</artifactId>
            <version>2.4.5</version>
            <type>pom</type>
        </dependency>
        

        也许你还需要运输部分:

        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>transport</artifactId>
            <version>5.5.0</version>
        </dependency>
        

        看看这个链接是否有帮助:Elasticsearch TransportClient NetworkPlugin NoClassDefFoundError

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2016-08-02
          • 2018-02-10
          • 2019-12-22
          • 2019-04-16
          • 2017-05-20
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多