问题描述
我正在尝试在我的 Java 项目中使用 MongoDB.
将我的项目导出到文件后,我收到 MongoClient 未定义类的错误.
导出时没有出现错误.
我使用 Maven 导入不同的依赖项,并且所有 jars 都正确导出
I am trying to work with MongoDB in my Java projects.
After exporting my project to a file I get a no class defined error for the MongoClient.
I get no errors while exporting.
I use Maven to import the different dependencies and all jars are being exported properly
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();
}
}
推荐答案
我是这样解决您的问题的:
结帐项目来自 Github 源代码:
解压、构建、测试
或
1.创建了一个maven项目mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4
This is how I solved your issue:
Checkout project from Github source code:
unzip, build, test
OR
1. Created a maven projectmvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4
- 使用以下内容更新 pom.xml,添加来自 OP 的 mongo 客户端代码并打印集合名称
- 执行
mvn clean package
- 执行
java -jar test.jar
我得到的输出是:
- Update pom.xml with following, added mongo client code from OP and printing collection names
- Execute
mvn clean package
- Execute
java -jar test.jar
output I got is:
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(忽略包名)
检查阴影插件配置
<?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
There are multiple ways to solve this problemRef: https://www.baeldung.com/executable-jar-with-maven
这篇关于导出 jar 后出现 NoClassDefFoundError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!