I'm trying to connect a MongoDB database to Tomcat. I downloaded the three 3.10.1 Java Driver jar files, mongodb-driver-3.10.1-javadoc.jar
, mongodb-driver-3.10.1-sources.jar
, and mongodb-driver-3.10.1.jar
. I added them to WEB-INF\\lib
. I also added a MongoDB resource in META-INF\\context.xml
, and a resource reference in WEB-INF\\web.xml
. Still, Tomcat is saying that the package com.mongodb does not exist. Does anyone have a fix for this?
My context.xml file:
<Context reloadable="true">
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<Resource name="mongodb/MyMongoClient"
auth="Container"
type="com.mongodb.MongoClient"
closeMethod="close"
factory="com.mongodb.client.jndi.MongoClientFactory"
singleton="true"
connectionString="mongodb://localhost:80"/>
</Context>
My web.xml file:
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<servlet>
<servlet-name>Test</servlet-name>
<servlet-class>Test</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Test</servlet-name>
<url-pattern>/test</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>TestCheck</servlet-name>
<servlet-class>TestCheck</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestCheck</servlet-name>
<url-pattern>/testCheck</url-pattern>
</servlet-mapping>
<resource-ref>
<res-ref-name>
mongodb/MyMongoClient
</res-ref-name>
<res-type>
com.mongodb.MongoClient
</res-type>
<res-auth>
Container
</res-auth>
</resource-ref>
</web-app>
My Test.java file:
import javax.servlet.annotation.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import java.io.IOException;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.BasicDBObject;
@WebServer("/test")
public class Test extends HttpServlet{
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
//Connect
MongoClient mongoClient = new MongoClient(new MongoClientURI("mongodb://localhost:80"));
DB database = mongoClient.getDB("testDB");
DBCollection collection = database.getCollection("testCollection");
DBObject testDoc = new BasicDBObject("_id", "This is a test");
collection.insert(testDoc);
//close
mongoClient.close();
}
}
And finally, the output from the command prompt after trying to compile:
Test.java:11: error: package com.mongodb does not exist
import com.mongodb.MongoClient;
^
Test.java:12: error: package com.mongodb does not exist
import com.mongodb.MongoClientURI;
^
Test.java:14: error: package com.mongodb does not exist
import com.mongodb.DB;
^
Test.java:15: error: package com.mongodb does not exist
import com.mongodb.DBCollection;
^
Test.java:16: error: package com.mongodb does not exist
import com.mongodb.DBObject;
^
Test.java:17: error: package com.mongodb does not exist
import com.mongodb.BasicDBObject;
^
Test.java:19: error: cannot find symbol
@WebServer("/test")
^
symbol: class WebServer
Test.java:27: error: cannot find symbol
MongoClient mongoClient = new MongoClient(new MongoClientURI("mo
ngodb://localhost:80"));
^
symbol: class MongoClient
location: class Test
Test.java:27: error: cannot find symbol
MongoClient mongoClient = new MongoClient(new MongoClientURI("mo
ngodb://localhost:80"));
^
symbol: class MongoClient
location: class Test
Test.java:27: error: cannot find symbol
MongoClient mongoClient = new MongoClient(new MongoClientURI("mo
ngodb://localhost:80"));
^
symbol: class MongoClientURI
location: class Test
Test.java:30: error: cannot find symbol
DB database = mongoClient.getDB("testDB");
^
symbol: class DB
location: class Test
Test.java:31: error: cannot find symbol
DBCollection collection = database.getCollection("testCollection
");
^
symbol: class DBCollection
location: class Test
Test.java:33: error: cannot find symbol
DBObject testDoc = new BasicDBObject("_id", "Hello!");
^
symbol: class DBObject
location: class Test
Test.java:33: error: cannot find symbol
DBObject testDoc = new BasicDBObject("_id", "Hello!");
^
symbol: class BasicDBObject
location: class Test
14 errors
Any help would be much appreciated.
It's not Tomcat, it's compiler. To add the MongoDB libraries to Tomcat is needed to be able to run the (web)app, but in order to compile it you have to add them to the compiler's classpath. If you use the javac
directly, see the -cp
switch (or have a look here ), if you use some build system (like Maven), look into its documentation how to add a compile dependency.
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.