我用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;
}
1条答案
按热度按时间x9ybnkn61#
你的遮光配置看起来不太好。这是一个为我工作。你可以从这个开始,添加你的节点,直到它断开:
你的代码运行我的参数建议(错误,因为我没有mongodb,所以服务器url无效):