I am using Eclipse Kepler, Java, Jboss 7.1 and Mongodb. When I am trying to send request from the Servlet to the class that works with the Mongodb I get an exception:
java.lang.ClassNotFoundException: org.bson.conversions.Bson
I am including the org.bson to the prohect by importing jar file named mongo-java-driver-3.0.3.jar.
The code is really basic and simple:
import java.net.UnknownHostException;
import org.bson.Document;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import static com.mongodb.client.model.Filters.*;
public class UserConnection {
private MongoClient client;
private MongoDatabase md;
private MongoCollection<Document>userCollection;
public void initUserConnection(){
client=new MongoClient();
md=client.getDatabase("eatFreeLottery");
userCollection=md.getCollection("users");
}
public void addClient(){
Document d=new Document();
d.append("name", "Sam");
this.initUserConnection();
userCollection.insertOne(d);
}
Servlet:
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
UserConnection uc=new UserConnection();
uc.addClient();
}
Main class:
public class Test {
public static void main(String[] args) {
UserConnection uc=new UserConnection();
uc.addClient();
}
}
Both the servlet and the class work fine as long as they don't need to work together.
At first I thought that the problem was with my glassFish server so I switched to Jboss, but that got me nowhere.
Thanks!
OK. Just copy paste the Jar file (mongo-java-driver-3.0.3.jar) under the web-inf/lib of the project and it works.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.