导出 jar 后出现 NoClassDefFoundError

我正在尝试在我的 Java 项目中使用 MongoDB。

将我的项目导出到文件后,我收到 MongoClient 的无类定义错误。

导出时我没有收到任何错误。

我使用 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();

        }

    }


LEATH
浏览 205回答 2
2回答

守着星空守着你

这就是我解决您的问题的方法:从Github 源代码检出项目:&nbsp;解压缩、构建、测试&nbsp;或&nbsp;1. 创建了一个 maven 项目mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4使用以下更新 pom.xml,从 OP 添加 mongo 客户端代码并打印集合名称执行&nbsp;mvn clean packagejava -jar test.jar&nbsp;我得到的&nbsp;执行 输出是:INFO: Opened connection [connectionId{localValue:1, serverValue:9}] to localhost:27017Jun 01, 2019 8:17:22 PM com.mongodb.diagnostics.logging.JULLogger logINFO: 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 logINFO: Opened connection [connectionId{localValue:2, serverValue:10}] to localhost:27017adminconfiglocaltest[XenonSuite] Successfully connected to MongoDBpom.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"&nbsp; xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">&nbsp; <modelVersion>4.0.0</modelVersion>&nbsp; <groupId>test</groupId>&nbsp; <artifactId>test</artifactId>&nbsp; <version>1.0-SNAPSHOT</version>&nbsp; <name>test</name>&nbsp; <!-- FIXME change it to the project's website -->&nbsp; <url>http://www.example.com</url>&nbsp; <properties>&nbsp; &nbsp; <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>&nbsp; &nbsp; <maven.compiler.source>1.8</maven.compiler.source>&nbsp; &nbsp; <maven.compiler.target>1.8</maven.compiler.target>&nbsp; &nbsp; <mainClass>test.App</mainClass>&nbsp; </properties>&nbsp; <packaging>jar</packaging>&nbsp; <dependencies>&nbsp; <!-- https://mvnrepository.com/artifact/org.mongodb/mongo-java-driver -->&nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.mongodb</groupId>&nbsp; &nbsp; &nbsp; &nbsp; <artifactId>mongo-java-driver</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; <version>3.10.2</version>&nbsp; &nbsp; </dependency>&nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; <groupId>junit</groupId>&nbsp; &nbsp; &nbsp; <artifactId>junit</artifactId>&nbsp; &nbsp; &nbsp; <version>4.11</version>&nbsp; &nbsp; &nbsp; <scope>test</scope>&nbsp; &nbsp; </dependency>&nbsp; </dependencies>&nbsp; <build>&nbsp; &nbsp; &nbsp; <plugins>&nbsp; &nbsp; &nbsp; &nbsp; <plugin>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.apache.maven.plugins</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>maven-assembly-plugin</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>3.1.1</version>&nbsp; &nbsp; &nbsp; &nbsp; </plugin>&nbsp; &nbsp; &nbsp; &nbsp; <plugin>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.apache.maven.plugins</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>maven-dependency-plugin</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>3.1.1</version>&nbsp; &nbsp; &nbsp; &nbsp; </plugin>&nbsp; &nbsp; &nbsp; &nbsp; <plugin>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.apache.maven.plugins</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>maven-jar-plugin</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>3.1.1</version>&nbsp; &nbsp; &nbsp; &nbsp; </plugin>&nbsp; &nbsp; &nbsp; &nbsp; <plugin>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.apache.maven.plugins</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>maven-shade-plugin</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>3.2.1</version>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <executions>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <execution>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <phase>package</phase>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <goals>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <goal>shade</goal>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </goals>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <configuration>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <shadedArtifactAttached>true</shadedArtifactAttached>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <transformers>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <mainClass>test.App</mainClass>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </transformer>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </transformers>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <finalName>test</finalName>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </configuration>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </execution>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </executions>&nbsp; &nbsp; &nbsp; &nbsp; </plugin>&nbsp; &nbsp; &nbsp; </plugins>&nbsp; </build></project>有多种方法可以解决这个问题 Ref: https://www.baeldung.com/executable-jar-with-maven

慕运维8079593

确保您在 Maven pom.xml 中具有以下依赖项。org.mongodb mongodb 驱动程序 3.9.1如果你不使用 maven,你必须从 mvnrepository.com 下载这个 jar 文件,你还必须下载 bson.jar,版本为 3.9.1 的 mongodb-driver-core,你必须下载 slf4j-api.jar版本 1.7.6。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java