提问人:Bootfahrer 提问时间:8/10/2023 最后编辑:Bootfahrer 更新时间:8/12/2023 访问量:73
java.lang.NoClassDefFound使用 mongoDB 时出错
java.lang.NoClassDefFoundError while using mongoDB
问:
我用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.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>
这里也是一个最小值:
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;
}
答:
0赞
g00se
8/11/2023
#1
您的阴影配置看起来关闭。这是一个对我有用的方法。你可以从这个开始,然后添加你的节点,直到它中断:
<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$
评论
0赞
Bootfahrer
8/11/2023
由于某种原因仍然不起作用。但是我在 VSC 中执行程序时注意到了一些事情。当我使用连接到MongoDB的方法时,控制台显示:Aug 11, 2023 2:31:55 PM com.mongodb.diagnostics.logging.Loggers shouldUseSLF4J WARNING: SLF4J not found on the classpath. Logging is disabled for the 'org.mongodb.driver' component
0赞
g00se
8/11/2023
当然,这是完全不同的事情。这是一个警告(不是错误),告诉你你的驱动程序不会在没有slf4j作为依赖项的情况下自行调试。所以你是说你仍然在新的 Shade 配置中遇到与以前相同的错误(您需要正确指定主类以作为可执行 jar 运行)?
0赞
Bootfahrer
8/11/2023
是的,仍然是相同的错误。我指定了 ,这是我的 Main 类和我的 main 方法。但我不确定你所说的“作为可执行 jar 运行”是什么意思。我必须使用其他文件吗?org.mongodb.logic.Main
0赞
g00se
8/11/2023
我的意思是,如果你想把它作为java -jar target/passwordManager-1.0-SNAPSHOT.jar
0赞
Bootfahrer
8/12/2023
我将 pom 编辑为我的确切代码。也许你可以在那里找到一个错误。它仍然不起作用。我开始相信我必须手动启动程序,我想这很好。无论哪种方式,到目前为止,您都帮助了我很多
评论
NoClassDefError