外部库的 java.lang.NoClassDefFoundError

标签 java maven

我有一个简单的程序,可以将 csv 文件解析为 json 对象。 我将 json.simple-1.1 添加到我的 Maven 中。我的maven的内容是这样的:

    <groupId>IoT</groupId>
    <artifactId>ETL2</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
    <dependency>
        <groupId>com.googlecode.json-simple</groupId>
        <artifactId>json-simple</artifactId>
        <version>1.1</version>
    </dependency>
    </dependencies>



    <build>
        <plugins>
            <plugin>
                <!-- Build an executable JAR -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.0.2</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>ETL2</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build> 

我尝试在终端中执行 jar Artifact 。 我收到这些错误。

Error: A JNI error has occurred, please check your installation and try again Exception in thread "main" java.lang.NoClassDefFoundError: org/json/simple/parser/ParseException at java.lang.Class.getDeclaredMethods0(Native Method) at java.lang.Class.privateGetDeclaredMethods(Class.java:2701) at java.lang.Class.privateGetMethodRecursive(Class.java:3048) at java.lang.Class.getMethod0(Class.java:3018) at java.lang.Class.getMethod(Class.java:1784) at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544) at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526) > Caused by: java.lang.ClassNotFoundException: org.json.simple.parser.ParseException at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ... 7 more

我在这里读到

This is caused when there is a class file that your code depends on and it is present at compile time but not found at runtime. Look for differences in your build time and runtime classpaths.

这就是为什么我怀疑我的 Maven 配置可能不正确。

最佳答案

将您的插件更改为以下代码片段,强烈建议您阅读 How to build an executable jar with dependencies

<build>
    <plugins>
        <plugin>
            <!-- Build an executable JAR -->
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.0.2</version>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                        <mainClass>ETL2</mainClass>
                    </manifest>
                </archive>
                <descriptorRefs>
                  <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
        </plugin>
    </plugins>
</build> 

关于外部库的 java.lang.NoClassDefFoundError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49190639/

相关文章:

java - Maven:执行wsgen时找不到tools.jar

java - 如何使用推特登录我的 Codename one 应用程序

java - 如何减小java中截图图像的大小

java - 从 ActionListener 访问变量并将其传输到新方法

java - 另一个Maven "Source option 6 is no longer supported. Use 7 or later."

java - 在gradle中使用maven-publish插件生成SHA512校验和文件

grails - java后端和grails前端

java - 字符 15 处不存在 Postgres 函数

java - 在 for 循环中等待输入的最佳方法?

Eclipse m2e 内容帮助 POM 无法超越顶级元素