为了账号安全,请及时绑定邮箱和手机立即绑定

导出 jar 后出现 NoClassDefFoundError

导出 jar 后出现 NoClassDefFoundError

LEATH 2022-12-28 14:12:58
我正在尝试在我的 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();        }    }
查看完整描述

2 回答

?
守着星空守着你

TA贡献1799条经验 获得超8个赞

这就是我解决您的问题的方法:从Github 源代码
检出项目: 解压缩、构建、测试 或 1. 创建了一个 maven 项目



mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4

  1. 使用以下更新 pom.xml,从 OP 添加 mongo 客户端代码并打印集合名称

  2. 执行 mvn clean package

  3. 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>


有多种方法可以解决这个问题 Ref: https://www.baeldung.com/executable-jar-with-maven


查看完整回答
反对 回复 2022-12-28
?
慕运维8079593

TA贡献1876条经验 获得超5个赞

确保您在 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。


查看完整回答
反对 回复 2022-12-28
  • 2 回答
  • 0 关注
  • 108 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信