Java Jar地狱运行时异常

时间:2016-08-02 05:19:23

标签: java elasticsearch integration-testing

我在运行单元测试时遇到了jar地狱的问题。

java.lang.RuntimeException: found jar hell in test classpath
    at org.elasticsearch.bootstrap.BootstrapForTesting.<clinit>(BootstrapForTesting.java:95)
    at org.elasticsearch.test.ESTestCase.<clinit>(ESTestCase.java:99)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:348)
    at com.carrotsearch.randomizedtesting.RandomizedRunner$2.run(RandomizedRunner.java:585)
Caused by: java.lang.IllegalStateException: jar hell!
class: org.apache.commons.io.CopyUtils
jar1: C:\Users\vvenkatasubbu\.gradle\caches\modules-2\files-2.1\commons-io\commons-io\1.3.2\b6dde38349ba9bb5e6ea6320531eae969985dae5\commons-io-1.3.2.jar
jar2: C:\Users\vvenkatasubbu\.gradle\caches\modules-2\files-2.1\org.apache.commons\commons-io\1.3.2\b6dde38349ba9bb5e6ea6320531eae969985dae5\commons-io-1.3.2.jar
    at org.elasticsearch.bootstrap.JarHell.checkClass(JarHell.java:282)
    at org.elasticsearch.bootstrap.JarHell.checkJarHell(JarHell.java:186)
    at org.elasticsearch.bootstrap.JarHell.checkJarHell(JarHell.java:87)
    at org.elasticsearch.bootstrap.BootstrapForTesting.<clinit>(BootstrapForTesting.java:93)
    ... 4 more

我得到了上面的错误,通过删除不必要的jar文件解决了这些错误。

但我面临的问题是以下两个jar,即tomcat-embed-core-8.0.36.jar和hibernate-jpa-2.1-api-1.0.0.Final.jar。这两个之间有一个共同的类我需要jar文件,任何人都可以解释我如何解决问题。我需要两个jar文件。

java.lang.RuntimeException: found jar hell in test classpath
        at org.elasticsearch.bootstrap.BootstrapForTesting.<clinit>(BootstrapForTesting.java:95)
        at org.elasticsearch.test.ESTestCase.<clinit>(ESTestCase.java:99)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:348)
        at com.carrotsearch.randomizedtesting.RandomizedRunner$2.run(RandomizedRunner.java:585)
    Caused by: java.lang.IllegalStateException: jar hell!
    class: javax.persistence.PersistenceProperty
    jar1: C:\Users\vvenkatasubbu\.gradle\caches\modules-2\files-2.1\org.apache.tomcat.embed\tomcat-embed-core\8.0.36\de87fa65671daad59773c8280eeabbd335f42c\tomcat-embed-core-8.0.36.jar
    jar2: C:\Users\vvenkatasubbu\.gradle\caches\modules-2\files-2.1\org.hibernate.javax.persistence\hibernate-jpa-2.1-api\1.0.0.Final\5e731d961297e5a07290bfaf3db1fbc8bbbf405a\hibernate-jpa-2.1-api-1.0.0.Final.jar
        at org.elasticsearch.bootstrap.JarHell.checkClass(JarHell.java:282)
        at org.elasticsearch.bootstrap.JarHell.checkJarHell(JarHell.java:186)
        at org.elasticsearch.bootstrap.JarHell.checkJarHell(JarHell.java:87)
        at org.elasticsearch.bootstrap.BootstrapForTesting.<clinit>(BootstrapForTesting.java:93)
        ... 4 more

提前致谢。

1 个答案:

答案 0 :(得分:5)

我通过覆盖unit test sorce文件夹中的JarHell类来修复它。

我已完成的步骤

  1. 在测试文件夹中创建一个单独的包。

    org.elasticsearch.bootstrap

  2. 创建一个新类并粘贴以下代码。

  3. package org.elasticsearch.bootstrap;
    
    import java.net.URL;
    
    public class JarHell {
        private JarHell() {}
        public static void checkJarHell() throws Exception {}
        public static void checkJarHell(URL urls[]) throws Exception {}
        public static void checkVersionFormat(String targetVersion) {}
        public static void checkJavaVersion(String resource, String targetVersion) {}
        public static URL[] parseClassPath() {return new URL[]{};}
    }
    
    1. 在执行gradlew构建时修复了问题。

    2. 要单独运行集成测试,请将以下行添加到运行配置中的VM参数中。

    3.     -ea -Dtests.security.manager=false