我正在使用maven创建一个可以启动的jar。
所以我会:
mvn clean install
我拿到了罐子。
当我这样做时:
java -jar target/ProjetMRS-0.0.1-SNAPSHOT.jar
我发现以下错误:
Exception in thread "main" java.lang.NoClassDefFoundError: com/mongodb/MongoClient
at iut.nantes.Projet.service.ServicePersonne.<init>(ServicePersonne.java:21)
at iut.nantes.projetMRS.Api.<clinit>(Api.java:21)
Caused by: java.lang.ClassNotFoundException: com.mongodb.MongoClient
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:338)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 2 more
当我查看文件时,所有导入都是最新的:
package iut.nantes.projetMRS.service;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import org.bson.types.ObjectId;
import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.Morphia;
import org.mongodb.morphia.query.Query;
import org.mongodb.morphia.query.UpdateOperations;
import com.mongodb.MongoClient;
import iut.nantes.projetMRS.entity.EntityPersonne;
public class ServicePersonne {
/*Error here ==>*/ MongoClient client = new MongoClient("localhost", 27017); //connect to mongodb
[...]
最后一件非常奇怪的事情是,当我在自己的计算机上启动应用程序时,一切都正常,我得到了0个错误,并且找到了MongoClient。
我看了这里:
https://stackoverflow.com/questions/45045681/how-do-i-fix-a-noclassdeffounderror-for-mongoclient-in-my-sparkjava-app
但是没有人回答他
我找到了一些答案,但没有一个对我的案子有用。
下面我向您展示了我的pom文件以及所有最新的依赖项:
<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>iut.nantes</groupId>
<artifactId>projetMRS</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>projetMRS</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>iut.nantes.projetMRS.Api</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.6.3</version>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver</artifactId>
<version>3.6.3</version>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-core</artifactId>
<version>3.6.3</version>
</dependency>
<dependency>
<groupId>com.sparkjava</groupId>
<artifactId>spark-core</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>bson</artifactId>
<version>3.6.3</version>
</dependency>
<dependency>
<groupId>org.mongodb.morphia</groupId>
<artifactId>morphia</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.3.1</version>
</dependency>
</dependencies>
</project>