gpt4 book ai didi

java - 导出jar后出现NoClassDefFoundError

转载 作者:行者123 更新时间:2023-12-02 04:56:48 25 4
gpt4 key购买 nike

我正在尝试在我的 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();
}
}

最佳答案

这就是我解决您问题的方法:
Github source code: 结帐项目
解压、构建、测试

1.创建maven项目
mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4

  • 使用以下内容更新 pom.xml,添加来自 OP 的 mongo 客户端代码并打印集合名称
  • 执行mvn 清理包
  • 执行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(忽略包名称)
    检查阴影插件配置

    <?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 - 导出jar后出现NoClassDefFoundError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56407017/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com