Java NoSuchMethodError: com.google.common.util.concurrent.MoreExecutors.directExecutor 在 Elastic Search jar 上冲突
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35157642/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NoSuchMethodError: com.google.common.util.concurrent.MoreExecutors.directExecutor conflits on Elastic Search jar
提问by André Alves
While creating Elasticsearch Client, I'm getting the exception java.lang.NoSuchMethodError: com.google.common.util.concurrent.MoreExecutors.directExecutor()Ljava/util/concurrent/Executor; After some lookup, seams like the Guava-18 is being overwrite by an older version at runtime, and Guava-18 only works during compile task.
在创建 Elasticsearch Client 时,我收到异常 java.lang.NoSuchMethodError: com.google.common.util.concurrent.MoreExecutors.directExecutor()Ljava/util/concurrent/Executor; 经过一番查找,Guava-18 之类的接缝在运行时被旧版本覆盖,而 Guava-18 仅在编译任务期间有效。
My Maven configuration is the follow:
我的Maven配置如下:
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
How can i force the Guava-18 version at execution time?
如何在执行时强制使用 Guava-18 版本?
采纳答案by devlearn
You should try to find where the "old" version of guava is taken from and to exclude it once for all.
您应该尝试找到“旧”版本番石榴的来源,并一劳永逸地排除它。
Find the dependency :
找到依赖:
mvn dependency:tree | grep guava
mvn dependency:tree | grep guava
Exclude it :
排除它:
<dependency>
<groupId>org.whatever</groupId>
<artifactId>the_lib_that_includes_guava</artifactId>
<version>0.97</version>
<exclusions>
<exclusion>
<artifactId>com.google</artifactId>
<groupId>guava</groupId>
</exclusion>
</exclusions>
</dependency>
See https://maven.apache.org/guides/introduction/introduction-to-optional-and-excludes-dependencies.htmlfor more info on the dependency exclusion.
有关依赖项排除的更多信息,请参阅https://maven.apache.org/guides/introduction/introduction-to-optional-and-excludes-dependencies.html。
回答by Gary Gauh
Add a dependencyManagement
block solves this problem:
添加dependencyManagement
块解决了这个问题:
<dependencyManagement>
<!-- enforce dependency guava version 20.0 -->
<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>20.0</version>
</dependency>
</dependencies>
</dependencyManagement>
Reference:
参考:
http://techidiocy.com/maven-dependency-version-conflict-problem-and-resolution/
http://techidiocy.com/maven-dependency-version-conflict-problem-and-resolution/
回答by Robin Wang
I add the correct dependency of elasticsearch resolve the problem
我添加了elasticsearch的正确依赖解决了问题
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>18.0</version>
</dependency>
回答by MaverickZHN
I had a similar problem. I created a .jarfile (Java source), then I wanted to load that file into the Spark Shell. It turns out that Spark Shell loads jars from something similar to this spark-[version]-bin-hadoop[version]/jars/".
我有一个类似的问题。我创建了一个.jar文件(Java 源代码),然后我想将该文件加载到Spark Shell 中。事实证明,Spark Shell 从类似于spark-[version]-bin-hadoop[version]/jars/" 的东西加载 jars 。
That directory had an older version of the guava which causes the error. I had the correct version in my pom.xml. I even added exclusions and all suggested responses. In conclusion, it is indeed a wrong version of guava. I copied the version that matchesmy pom.xml file. Hope this helps. Regards.
该目录具有导致错误的较旧版本的番石榴。我的pom.xml 中有正确的版本。我什至添加了排除项和所有建议的回复。综上所述,确实是番石榴的错误版本。我复制了与我的 pom.xml 文件匹配的版本。希望这可以帮助。问候。
回答by Himadri
SOLVED: I updated the Guava dependency to latest version and it solved the
已解决:我将 Guava 依赖项更新到最新版本,它解决了
<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>latest</version>
</dependency>
回答by mjj1409
I was also seeing the error message mentioned by the OP when creating an Elasticsearch Client instance. In my case it was occurring in a Spring Boot app at application startup. Spring Boot was attempting to auto-configure the Elasticsearch Client via dependencies brought in by spring-boot-starter-data-elasticsearch
. The underlying guava version being brought in was:
在创建 Elasticsearch Client 实例时,我还看到了 OP 提到的错误消息。在我的情况下,它发生在应用程序启动时的 Spring Boot 应用程序中。Spring Boot 试图通过spring-boot-starter-data-elasticsearch
. 引入的基本番石榴版本是:
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>18.0</version>
</dependency>
This was all working fine until I introduced the following google-api-client
dependency...
这一切都很好,直到我引入了以下google-api-client
依赖项......
<dependency>
<groupId>com.google.api-client</groupId>
<artifactId>google-api-client</artifactId>
<version>1.23.0</version>
</dependency>
...which depends onfollowing guava dependency:
...这取决于以下番石榴依赖:
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava-jdk5</artifactId>
<version>17.0</version>
</dependency>
This caused a class path collision and the fix was to exclude the older guava version from the google-api-client
like so:
这导致了类路径冲突,修复方法是从google-api-client
类似中排除旧的番石榴版本:
<dependency>
<groupId>com.google.api-client</groupId>
<artifactId>google-api-client</artifactId>
<version>1.23.0</version>
<exclusions>
<exclusion>
<groupId>com.google.guava</groupId>
<artifactId>guava-jdk5</artifactId>
</exclusion>
</exclusions>
</dependency>
回答by Garima Thakur
I was struggling with this issue from past 2 months and finally found the solution.
过去 2 个月我一直在努力解决这个问题,终于找到了解决方案。
I added too many external jars in my Project Structure which actually created some jars in Library Root resulting in conflicts whenever something is added in pom.xml.
我在我的项目结构中添加了太多外部 jar,这实际上在 Library Root 中创建了一些 jar,导致在pom.xml 中添加某些内容时发生冲突。
So what needs to be done is delete all external jar files from your project and keep only the ones which are from maven like Maven:org...
所以需要做的是从你的项目中删除所有外部 jar 文件,只保留那些来自 Maven 的文件,比如 Maven:org ...
My project structure:
我的项目结构:
回答by Rajiv Singh
For SBT solution:
对于 SBT 解决方案:
Use shading the library in build.sbt
在 build.sbt 中使用着色库
// Shading com.google.**
// We need com.google.guava above 18 version but spark uses version 14 and in that we don't have directExecutor() method
// as spark give preference to spark used libraries, our code was failing
assemblyShadeRules in assembly := Seq(
ShadeRule.rename("com.google.**" -> "shadeio.@1").inAll
)
回答by tariq abughofa
The best soltion is to use the shade plugin for maven. Adding this to your pom.xml should fix it:
最好的解决方案是使用 maven 的 shade 插件。将此添加到您的 pom.xml 应该修复它:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<relocations>
<relocation>
<pattern>com.google.common</pattern>
<shadedPattern>shaded.com.google.common</shadedPattern>
</relocation>
</relocations>
<artifactSet>
<includes>
<include>com.google.guava:guava</include>
</includes>
</artifactSet>
</configuration>
</execution>
</executions>
</plugin>
This will create an upper jar with the same name including only a shaded guava inside.
这将创建一个同名的上层罐子,里面只包含一个带阴影的番石榴。