导出jar后出现NoClassDefFoundError

时间:2019-06-01 13:04:50

标签: java mongodb 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();
        }
    }

2 个答案:

答案 0 :(得分:0)

请确保您在Maven pom.xml中具有以下依赖性。

    org.mongodb     mongodb驱动程序     3.9.1

如果您不使用maven,则必须从mvnrepository.com下载此jar文件,还必须下载bson.jar,mongodb-driver-core版本3.9.1,并且必须下载slf4j-api .jar版本1.7.6。

答案 1 :(得分:0)

这就是我解决您的问题的方式:
来自Github source code:的结帐项目
解压缩,构建,测试

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>

有多种方法可以解决此问题 参考:https://www.baeldung.com/executable-jar-with-maven