导出 jar 后出现 NoClassDefFoundError
NoClassDefFoundError after exporting jar
我正尝试在我的 Java 项目中使用 MongoDB。
将项目导出到文件后,我收到 MongoClient 的未 class 定义错误。
导出时没有错误。
我使用 Maven 导入不同的依赖项,所有 jar 都被正确导出
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
public class MongoDB {
private static MongoClient client;
public static void init() {
try {
client = MongoClients.create("mongodb://localhost:27017/xenoncraft");
System.out.println("[XenonSuite] Successfully connected to MongoDB");
} catch(Exception e) {
System.out.println("[XenonSuite] Following errors were catched while connecting to MongoDB");
e.printStackTrace();
}
}
确保您在 Maven 中具有以下依赖项 pom.xml。
org.mongodb
mongodb-driver
3.9.1
如果您不使用 maven,则必须从 mvnrepository.com 下载此 jar 文件,还必须下载 bson.jar、mongodb-driver-core 版本 3.9.1你必须下载 slf4j-api.jar 版本 1.7.6.
我就是这样解决你的问题的:
从 Github source code:
签出项目
解压缩、构建、测试
或者
1.创建了一个maven项目
mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4
- 使用以下内容更新 pom.xml,添加 mongo 来自 OP 的客户端代码并打印集合名称
- 执行
mvn clean package
- 执行
java -jar test.jar
我得到的输出是:
INFO: Opened connection [connectionId{localValue:1, serverValue:9}] to localhost:27017
Jun 01, 2019 8:17:22 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Monitor thread successfully connected to server with description ServerDescription{address=localhost:27017, type=STANDALONE, state=CONNECTED, ok=true, version=ServerVersion{versionList=[4, 0, 5]}, minWireVersion=0, maxWireVersion=7, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=2249770}
Jun 01, 2019 8:17:22 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Opened connection [connectionId{localValue:2, serverValue:10}] to localhost:27017
admin
config
local
test
[XenonSuite] Successfully connected to MongoDB
pom.xml(忽略包名)
检查 shade 插件配置
<?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>test</groupId>
<artifactId>test</artifactId>
<version>1.0-SNAPSHOT</version>
<name>test</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<mainClass>test.App</mainClass>
</properties>
<packaging>jar</packaging>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.mongodb/mongo-java-driver -->
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.10.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.1.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<shadedArtifactAttached>true</shadedArtifactAttached>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>test.App</mainClass>
</transformer>
</transformers>
<finalName>test</finalName>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
有多种方法可以解决这个问题
参考:https://www.baeldung.com/executable-jar-with-maven
我正尝试在我的 Java 项目中使用 MongoDB。
将项目导出到文件后,我收到 MongoClient 的未 class 定义错误。
导出时没有错误。
我使用 Maven 导入不同的依赖项,所有 jar 都被正确导出
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
public class MongoDB {
private static MongoClient client;
public static void init() {
try {
client = MongoClients.create("mongodb://localhost:27017/xenoncraft");
System.out.println("[XenonSuite] Successfully connected to MongoDB");
} catch(Exception e) {
System.out.println("[XenonSuite] Following errors were catched while connecting to MongoDB");
e.printStackTrace();
}
}
确保您在 Maven 中具有以下依赖项 pom.xml。
org.mongodb mongodb-driver 3.9.1
如果您不使用 maven,则必须从 mvnrepository.com 下载此 jar 文件,还必须下载 bson.jar、mongodb-driver-core 版本 3.9.1你必须下载 slf4j-api.jar 版本 1.7.6.
我就是这样解决你的问题的:
从 Github source code:
签出项目
解压缩、构建、测试
或者
1.创建了一个maven项目
mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4
- 使用以下内容更新 pom.xml,添加 mongo 来自 OP 的客户端代码并打印集合名称
- 执行
mvn clean package
- 执行
java -jar test.jar
我得到的输出是:
INFO: Opened connection [connectionId{localValue:1, serverValue:9}] to localhost:27017
Jun 01, 2019 8:17:22 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Monitor thread successfully connected to server with description ServerDescription{address=localhost:27017, type=STANDALONE, state=CONNECTED, ok=true, version=ServerVersion{versionList=[4, 0, 5]}, minWireVersion=0, maxWireVersion=7, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=2249770}
Jun 01, 2019 8:17:22 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Opened connection [connectionId{localValue:2, serverValue:10}] to localhost:27017
admin
config
local
test
[XenonSuite] Successfully connected to MongoDB
pom.xml(忽略包名)
检查 shade 插件配置
<?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>test</groupId>
<artifactId>test</artifactId>
<version>1.0-SNAPSHOT</version>
<name>test</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<mainClass>test.App</mainClass>
</properties>
<packaging>jar</packaging>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.mongodb/mongo-java-driver -->
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.10.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.1.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<shadedArtifactAttached>true</shadedArtifactAttached>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>test.App</mainClass>
</transformer>
</transformers>
<finalName>test</finalName>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
有多种方法可以解决这个问题 参考:https://www.baeldung.com/executable-jar-with-maven