1

I'm trying to connect to a local MongoDB instance (version 3.2). I've specified a dependency in my build.gradle like so:

dependencies { compile 'org.mongodb:mongodb-driver:3.3.0' }

I have a simple App.java file with the following code (see below). The build/compileJava steps all run well with no errors. But when I run the code, I get: "Exception in thread "main" java.lang.NoClassDefFoundError: com/mongodb/MongoClient at App.main(App.java:9)

I'm new to Java. I'm not sure if I need to download the driver in addition to referencing it in the build.gradle dependency list, and if so, where to place it.

Here's my src/main/java/App.java:

import com.mongodb.Mongo;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoDatabase;

public class App{
        public static void main (String[] args){
                System.out.println("Connecting ... ");
                try {
                        MongoClient client = new MongoClient();
                }
                catch(Exception e) {
                        System.out.println("Failed to connect to MongoDB");
                }

        }
}
Eyal Zinder
  • 614
  • 1
  • 8
  • 21
  • I use the following command to run the application: java -cp build/classes/main App – Eyal Zinder Nov 04 '16 at 20:51
  • 1
    Your classpath argument, -cp, does not specify the mongo-driver jar. Since you are new to java, I highly suggest you take a step back and understand the classpath a bit more before introducing gradle. Official docs are here http://docs.oracle.com/javase/6/docs/technotes/tools/solaris/classpath.html and the following is also a nice supplement to that: http://enigmastation.com/2014/07/11/repost-rocket-java-that-stupid-classpath-thing-you-should-understand/ – whaley Nov 04 '16 at 22:15

2 Answers2

3

The easiest thing is to use Gradle application plugin. Application plugin will automatically add run task that will execute the specified main class with all the runtime dependencies automatically put on the classpath:

apply plugin: 'application'

mainClassName = 'App'

dependencies {
    compile 'org.mongodb:mongodb-driver:3.3.0'
}

repositories {
    mavenCentral()
}

You can run the application by: gradle run

Crazyjavahacking
  • 9,343
  • 2
  • 31
  • 40
1

You should add the mongodb-client jar and its dependencies to the classpath when you run the application.

The easiest way to do this is by starting your application from Gradle. Add the following task to your build.gradle:

task run(type: JavaExec) {
    classpath = sourceSets.main.runtimeClasspath
    main = 'App'
}

Then execute gradle --info run to start your application. I added the --info parameter, so that gradle will log the command is uses to start the application. On my machine the command shown is:

 /usr/lib/jvm/java-8-openjdk/bin/java -Dfile.encoding=UTF-8 -Duser.country=US -Duser.language=en -Duser.variant -cp /tmp/App/build/classes/main:/tmp/App/build/resources/main:/home/wg01018/.gradle/caches/modules-2/files-2.1/org.mongodb/mongodb-driver/3.3.0/398df8cc99f36c81344f37401b4284d39f84f6a5/mongodb-driver-3.3.0.jar:/home/wg01018/.gradle/caches/modules-2/files-2.1/org.mongodb/bson/3.3.0/d57b1626e7007a93deb12b2b607e85f6822bf060/bson-3.3.0.jar:/home/wg01018/.gradle/caches/modules-2/files-2.1/org.mongodb/mongodb-driver-core/3.3.0/9af003a316f17323ade866ba5a484a03de6ff025/mongodb-driver-core-3.3.0.jar App

You see that the classpath contains the mongodb-driver jar, but also some additional jars which are dependencies of the mongodb-driver jar.

gradle dependencies will show you a tree of these dependencies.

Wilco Greven
  • 2,085
  • 1
  • 10
  • 9
  • I added it verbatim.. ran "run" task with gradle.. ran: "java -cp build/classes/main App" from my project root.. same results. Did I mention I'm totally new to Gradle/Java? .. I'm not sure if I was suppose to alter that declaration in any way. – Eyal Zinder Nov 04 '16 at 21:21
  • When you run `gradle run` do you see that your application is trying to connect to MongoDB? – Wilco Greven Nov 04 '16 at 21:49
  • Yes. :::::::::::::: :run Connecting ... Nov 04, 2016 3:02:44 PM com.mongodb.diagnostics.logging.JULLogger log INFO: Cluster created with settings {hosts=[127.0.0.1:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500} BUILD SUCCESSFUL :::::::::::::::::::::: – Eyal Zinder Nov 04 '16 at 22:03