Problems to connect MongoDB from JAVA
I am working in Text Mining with articles from Medline. I have indexed all Medline2011 with MongoDB (http://www.mongodb.org/), now I can get one article with its PubMedID. I want join it with UIMA (http://uima.apache.org/), and I have created the DescriptorReader to get the article String from MongoDB (using MondoDB Driver for JAVA). I have this code (a quick example) and works:
public static void main(String[] args) throws UnknownHostException, MongoException {
Mongo m = new Mongo("localhost", 27017);
DB db = m.getDB(DB_NAME);
DBCollection coll = db.getCollection(COLLECTION_NAME);
BasicDBObject query = new BasicDBObject();
String pmid = "6889938";
query.put("_id", pmid);
DBObject myDoc = coll.findOne(query);
System.out.println(myDoc);
}
But, when I try write the same code in a project with UIMA I have the next error:
When I use:
private String getAbstractXMLFromMongoDB(String pmid) throws UnknownHostException, MongoException{
Mongo m = new Mongo(); // <-----ERROR
...
return "something"
The error in console is:
ThreadGroup.uncaughtException()-Got Error
java.lang.NoClassDefFoundError: com/mongodb/Mongo
...
And if I use try/catch:
private String getAbstractXMLFromMongoDB(String pmid){
try {
Mongo m = new Mongo(); // <-----ERROR
...
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (MongoException e) {
e.printStackTrace();
}
return "something"
The error in console is:
Exception in thread "Thread-5" java.lang.NoClassDefFoundError: com/mongodb/MongoException
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:247)
...
I have tried (in the Example code) access to MongoDB with 100 threads at once and works... I do not know and do not undertands what is happening...
Any suggestions?
thx.开发者_开发技巧
Provide the mongo libs to your classpath and the error should be gone.
Your try/catch
approach would work, if you remove the MongoException
(this one isn't known by your classpath, jars missing) and replace it with a generic Exception
If you are developing in eclipse just add the mongo jars to your buildpath. I don't know UIMA but that shouldn't matter
精彩评论