java.lang.NoClassDefFoundError

问题描述 投票:0回答:1

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

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

我用:

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

作为我在 .bat 文件中的启动命令。

这是我的 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.1</version>
          <executions>
            <execution>
              <phase>package</phase>
              <goals>
                <goal>shade</goal>
              </goals>
              <configuration>
                <minimizeJar>true</minimizeJar>
                <createDependencyReducedPom>true</createDependencyReducedPom>
                <dependencyReducedPomLocation>
                  ${java.io.tmpdir}/dependency-reduced-pom.xml
                </dependencyReducedPomLocation>
                <relocations>
                  <relocation>
                    <pattern>com.acme.coyote</pattern>
                    <shadedPattern>hidden.coyote</shadedPattern>
                  </relocation>
                </relocations>
              </configuration>
            </execution>
          </executions>
        </plugin>

        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-jar-plugin</artifactId>
          <configuration>
            <archive>
              <manifest>
                <addClasspath>true</addClasspath>
                <mainClass>fully.qualified.MainClass</mainClass>
              </manifest>
            </archive>
          </configuration>
        </plugin>
        
      </plugins>
    </pluginManagement>
  </build>
</project>
java mongodb maven swing noclassdeffounderror
1个回答
0
投票

您的阴影配置看起来不正确。这对我有用。您可以从此开始并添加节点直到它损坏:

                <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>
© www.soinside.com 2019 - 2024. All rights reserved.