maven java.lang.NoClassDefFoundError while using mongoDB

qnakjoqk  于 7个月前  发布在  Maven
关注(0)|答案(1)|浏览(104)

我用java做了自己的密码管理器。我用MongoDB存储数据,我的IDE是VSCode。我可以在VSC中运行该项目没有任何问题。我做了一个.bat文件从我的桌面启动程序。但当我使用此快捷方式时,我收到以下错误消息:

Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: com/mongodb/client/MongoClients

i用途:

java -cp target/passwordManager-1.0-SNAPSHOT.jar org.mongodb.logic.Main

作为.bat文件中的start命令。
下面是我的pom.xml:

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.mongodb</groupId>
  <artifactId>passwordManager</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>passwordManager</name>
  <url>http://maven.apache.org</url>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.mongodb</groupId>
      <artifactId>mongodb-driver-sync</artifactId>
      <version>4.7.0</version>
    </dependency>
  </dependencies>

  <properties>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.1</version>
          <configuration>
            <source>1.7</source>
            <target>1.7</target>
            <fork>true</fork>
            <executable>C:\Program Files\Java\jdk1.8.0_202\bin\javac</executable>
          </configuration>
        </plugin>

        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-shade-plugin</artifactId>
          <version>3.1.0</version>
          <executions>
            <execution>
              <phase>package</phase>
              <goals>
                <goal>shade</goal>
              </goals>
              <configuration>
                <filters>
                  <filter>
                    <artifact>*:*</artifact>
                    <excludes>
                      <exclude>META-INF/*.SF</exclude>
                      <exclude>META-INF/*.DSA</exclude>
                      <exclude>META-INF/*.RSA</exclude>
                    </excludes>
                  </filter>
                </filters>
                <transformers>
                  <transformer
                    implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                    <manifestEntries>
                      <Main-Class>org.mongodb.logic.Main</Main-Class>
                      <Build-Number>1</Build-Number>
                    </manifestEntries>
                  </transformer>
                </transformers>
              </configuration>
            </execution>
          </executions>
        </plugin>

        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-jar-plugin</artifactId>
          <configuration>
            <archive>
              <manifest>
                <addClasspath>true</addClasspath>
                <mainClass>org.mongodb.logic.Main</mainClass>
              </manifest>
            </archive>
          </configuration>
        </plugin>
        
      </plugins>
    </pluginManagement>
  </build>
</project>

这里也有一个Minimal:

public static void main(String[] args) {
        System.out.println(getMail("test"));
    }

private static String getMail(String nameOfWebsite) {
        String email = null;

        try (MongoClient mongoClient = MongoClients.create(uri)) {
            MongoDatabase db = mongoClient.getDatabase("passwordManager");
            MongoCollection<Document> collPW = db.getCollection("passwords");

            Bson filter = Filters.eq("nameOfWebsite", nameOfWebsite);
            MongoCursor<Document> curserPW = collPW.find(filter).iterator();

            try {
                while (curserPW.hasNext()) {
                    email = (String) curserPW.next().get("email");
                }
            } finally {
                curserPW.close();
            }
        }
        return email;
    }
x9ybnkn6

x9ybnkn61#

你的遮光配置看起来不太好。这是一个为我工作。你可以从这个开始,添加你的节点,直到它断开:

<plugin>
    <artifactId>maven-shade-plugin</artifactId>
    <version>3.1.0</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <filters>
                    <filter>
                        <artifact>*:*</artifact>
                        <excludes>
                            <exclude>META-INF/*.SF</exclude>
                            <exclude>META-INF/*.DSA</exclude>
                            <exclude>META-INF/*.RSA</exclude>
                        </excludes>
                    </filter>
                </filters>
                <transformers>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <manifestEntries>
                            <Main-Class>${main.class}</Main-Class>
                            <Build-Number>1</Build-Number>
                        </manifestEntries>
                    </transformer>
                </transformers>
            </configuration>
        </execution>
    </executions>
</plugin>

你的代码运行我的参数建议(错误,因为我没有mongodb,所以服务器url无效):

goose@t410:/tmp/java-quick-start-master$ java -jar target/java-quick-start-1.0-SNAPSHOT.jar mongodb://localhost
Exception in thread "main" com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting to connect. Client view of cluster state is {type=UNKNOWN, servers=[{address=localhost:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused}}]
    at com.mongodb.internal.connection.BaseCluster.getDescription(BaseCluster.java:184)
    at com.mongodb.internal.connection.SingleServerCluster.getDescription(SingleServerCluster.java:46)
    at com.mongodb.client.internal.MongoClientDelegate.getConnectedClusterDescription(MongoClientDelegate.java:144)
    at com.mongodb.client.internal.MongoClientDelegate.createClientSession(MongoClientDelegate.java:101)
    at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.getClientSession(MongoClientDelegate.java:291)
    at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.execute(MongoClientDelegate.java:183)
    at com.mongodb.client.internal.MongoIterableImpl.execute(MongoIterableImpl.java:133)
    at com.mongodb.client.internal.MongoIterableImpl.iterator(MongoIterableImpl.java:90)
    at org.mongodb.logic.Main.getMail(Main.java:25)
    at org.mongodb.logic.Main.main(Main.java:14)
goose@t410:/tmp/java-quick-start-master$

相关问题