我有一个使用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文件有什么问题吗?项目有多个类(但只有一个主类)
尝试使用java -cp {m2 dir或jar库的绝对路径} -jar {your jar} .jar。没有类Def发现错误的原因总是jar不是你的类路径的一部分,-cp选项会将json简单jar及其依赖项添加到类路径中,你的错误就会得到解决。
根据截图,你似乎得到ClassNotFoundException
,这主要是由于范围,scope
用于限制依赖的传递性,并且还影响用于各种构建任务的类路径。
尝试将scope
添加到json-simple依赖项。
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1</version>
<scope>provided</scope>
</dependency>
我认为问题出在scope
,请尝试在compile
之后将范围更改为“test
”或“provided
”以防万一它仍然无效。
FYI
What is <scope> under <dependency> in pom.xml for?
编辑:将provided
更改为compile
。