添加导致java.lang.ClassNotFoundException的maven依赖项

时间:2016-08-16 18:18:37

标签: java reflection json-simple maven-dependency janino

我有一个正常运行的应用程序,直到我引入了以下maven依赖项。

    <dependency>
        <groupId>joda-time</groupId>
        <artifactId>joda-time</artifactId>
        <version>2.3</version>
    </dependency>

    <dependency>
        <groupId>net.sf.supercsv</groupId>
        <artifactId>super-csv</artifactId>
        <version>2.4.0</version>
    </dependency>
    <dependency>
        <groupId>com.googlecode.json-simple</groupId>
        <artifactId>json-simple</artifactId>
        <version>1.1.1</version>
    </dependency>

    <dependency>
        <groupId>org.codehaus.janino</groupId>
        <artifactId>commons-compiler-jdk</artifactId>
        <version>3.0.0</version>
    </dependency>

在引入以下依赖项之后,jar正在构建正确,没有编译器异常,但在运行应用程序时,它失败并出现以下异常:

例外1:

Exception in thread "2/streamJsonParser:StreamingJsonParser" java.lang.NoClassDefFoundError: org/apache/log4j/spi/ThrowableInformation
at org.apache.log4j.spi.LoggingEvent.<init>(LoggingEvent.java:165)
at org.apache.log4j.Category.forcedLog(Category.java:391)
at org.apache.log4j.Category.log(Category.java:856)
at org.slf4j.impl.Log4jLoggerAdapter.error(Log4jLoggerAdapter.java:540)
at com.datatorrent.stram.engine.StreamingContainer$2.run(StreamingContainer.java:1431)
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.spi.ThrowableInformation
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 5 more

例外2:

Exception in thread "org.apache.hadoop.hdfs.PeerCache@3826c025" java.lang.NoClassDefFoundError: com/google/common/collect/LinkedListMultimap$3
at com.google.common.collect.LinkedListMultimap.values(LinkedListMultimap.java:846)
at org.apache.hadoop.hdfs.PeerCache.clear(PeerCache.java:271)
at org.apache.hadoop.hdfs.PeerCache$1.run(PeerCache.java:128)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.ClassNotFoundException: com.google.common.collect.LinkedListMultimap$3
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)

例外3:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/helpers/NullEnumeration
at org.apache.log4j.Category.getAllAppenders(Category.java:413)
at org.apache.log4j.Category.closeNestedAppenders(Category.java:226)
at org.apache.log4j.Hierarchy.shutdown(Hierarchy.java:467)
at org.apache.log4j.LogManager.shutdown(LogManager.java:267)
at com.datatorrent.stram.engine.StreamingContainer.main(StreamingContainer.java:332)
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.helpers.NullEnumeration
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 5 morexception in thread "Thread-3" java.lang.NoClassDefFoundError: org/apache/hadoop/util/ShutdownHookManager$2
at org.apache.hadoop.util.ShutdownHookManager.getShutdownHooksInOrder(ShutdownHookManager.java:124)
at org.apache.hadoop.util.ShutdownHookManager$1.run(ShutdownHookManager.java:52)
Caused by: java.lang.ClassNotFoundException: org.apache.hadoop.util.ShutdownHookManager$2
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 2 more

请告诉我需要做什么。我尝试过添加一些但是没有用。请建议。

3 个答案:

答案 0 :(得分:0)

你试过这个:

mvn clean install -U

-U要求maven强制更新依赖项。

答案 1 :(得分:0)

在您的POM中包含以下依赖项

<!-- https://mvnrepository.com/artifact/log4j/apache-log4j-extras -->
<dependency>
    <groupId>log4j</groupId>
    <artifactId>apache-log4j-extras</artifactId>
    <version>1.1</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>19.0</version>
</dependency>

缺少这些,因此你会遇到异常。

以下是STS中POM编辑器依赖项选项卡的屏幕截图。最近的日食也应该有它。

enter image description here

您还可以使用以下命令检查依赖项。

mvn dependency:tree

答案 2 :(得分:0)

我接受了上面的答案,因为用户已经向我解释了识别添加的重复依赖项的方法。

实际上是来自POM和运行时环境的重复hadoop依赖性的问题。