0

I downloaded the mongo-java-driver-2.11.2.jar file from here - http://central.maven.org/maven2/org/mongodb/mongo-java-driver/2.11.3/ In my example application i have following code -

import com.mongodb.MongoClient;
import com.mongodb.MongoException;
import com.mongodb.WriteConcern;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.DBCursor;
import com.mongodb.ServerAddress;
import java.util.Arrays;
MongoClient mongoclient = new MongoClient("localhost",27017);

DB db = mongoclient.getDB("test1");

I always get this error -

[parsing started RegularFileObject[myfile.java]]
myfile.java:11: class, interface, or enum expected
MongoClient mongoclient = new MongoClient("localhost",27017);
^
myfile.java:12: class, interface, or enum expected
DB db = mongoclient.getDB("test1");▒
^
[parsing completed 26ms]
[total 54ms]
2 errors 

I tried various compiling options:

javac myfile.java 

since .jar is in same directory.

javac -cp '.:mongo-java-driver-2.11.2.jar' myfile.java

or even the full path to the file

javac -classpath '/home/nimish/HTMLProjects/mongodbJavaIntro/mongo-java-driver-2.11.2-sources.jar' myfile.java -verbose

But to no avail. Can anyone shed any light on this.

Hobby_Web_programmer
  • 775
  • 2
  • 10
  • 18
  • 2
    Your file is not properly structured java. Please look [at the java hello world tutorial](http://docs.oracle.com/javase/tutorial/getStarted/cupojava/index.html), which should lead you to the correct form of the file so that it can be compiled – Anya Shenanigans Jan 02 '14 at 12:41

1 Answers1

1

You are lacking a class name:

public class myfile { ...

And lacks some basic programming practices. Should name your class MyFile or so

import com.mongodb.MongoClient; 
import com.mongodb.MongoException; 
import com.mongodb.WriteConcern;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.DBCursor;
import com.mongodb.ServerAddress;
import java.util.Arrays;

public class myfile {

    DB db; 

    public myfile {
        initDB();
    }

    public void initDB() {
        MongoClient mongoclient = new MongoClient("localhost",27017);
        db = mongoclient.getDB("test1");
    }

    ...


} 
Ori Dar
  • 18,687
  • 5
  • 58
  • 72