【问题标题】:Jar file Error because of simple JSON dependency由于简单的 JSON 依赖,Jar 文件错误
【发布时间】:2019-03-13 06:22:13
【问题描述】:

我有一个使用 json-simple 库的 maven 项目。当我使用

创建jar文件时
mvn clean
mvn test
mvn install

并尝试使用java -jar target/Game2048-1.0-SNAPSHOT.jar 运行 jar 文件,我收到如下所示的错误:

我的 pom.xml 文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>Game2048_GId</groupId>
<artifactId>Game2048</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <plugin>
            <!-- Build an executable JAR -->
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.1.0</version>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                        <mainClass>Main</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>
<dependencies>
    <dependency>
        <groupId>com.googlecode.json-simple</groupId>
        <artifactId>json-simple</artifactId>
        <version>1.1.1</version>
    </dependency>
</dependencies>

我的 pom 文件有什么问题吗?该项目有多个类(但只有 1 个主类)

【问题讨论】:

    标签: java maven jar json-simple


    【解决方案1】:

    尝试使用 java -cp {你的 m2 目录或 jar 库的绝对路径} -jar {你的 jar}.jar。 no class Def found 错误的原因始终是 jar 不是您的类路径的一部分,-cp 选项会将 json 简单 jar 及其依赖项添加到类路径中,您的错误将得到解决。

    【讨论】:

    • 并非总是如此,当你有 maven 时,为什么要硬编码东西??
    • 除了类不存在于类路径或 jvm 无法访问之外,noclassdeferror 的其他原因是什么?这里的硬编码是什么!!
    • 在实际的应用程序项目中,可能会发生您必须使用的依赖 jar 仍在开发中,而您只有接口 jar,在这种情况下,maven 和 nexus repos 将无法为您提供实现 jar,这就是为什么使用提供的 maven 范围,并且在生产中您可以通过 -cp 选项提供 impl jar。理想情况下,应该通过 maven 完成依赖管理,但它并不总是最适合所有问题。
    【解决方案2】:

    根据屏幕截图,您似乎得到了ClassNotFoundException,这主要是由于范围scope 用于限制依赖项的传递性,并影响classpath 用于各种构建任务。

    尝试将 scope 添加到 json-simple 依赖项。

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

    我认为问题出在scope,请尝试在provided 之后将范围更改为“compile”或“test”,以防它仍然无法正常工作。

    仅供参考

    What is <scope> under <dependency> in pom.xml for?


    编辑:将provided 更改为compile

    【讨论】:

    • 或者简单地从舞会中删除范围标签也可以工作,因为默认范围是仅编译
    猜你喜欢
    • 2018-04-08
    • 2020-11-28
    • 1970-01-01
    • 2017-09-03
    • 1970-01-01
    • 1970-01-01
    • 2013-07-25
    • 2015-10-16
    • 2019-06-20
    相关资源
    最近更新 更多