title: Groovy program to connect mongo db
body: <p>I am trying to connect mongodb using Groovy language but i am getting error like this (which is posted below) and i have added necessary jar file.I am using mongodb verion : mongodb-driver-3.2.2.jar .
  Please help me to solve this problem</p>
  
  <pre><code>Exception in thread "main" java.lang.NoClassDefFoundError: com/mongodb/operation/OperationExecutor
      at java.lang.Class.getDeclaredConstructors0(Native Method)
      at java.lang.Class.privateGetDeclaredConstructors(Unknown Source)
      at java.lang.Class.getDeclaredConstructors(Unknown Source)
      at org.codehaus.groovy.reflection.CachedClass$2$1.run(CachedClass.java:69)
      at java.security.AccessController.doPrivileged(Native Method)
      at org.codehaus.groovy.reflection.CachedClass$2.initValue(CachedClass.java:67)
      at org.codehaus.groovy.reflection.CachedClass$2.initValue(CachedClass.java:64)
      at org.codehaus.groovy.util.LazyReference.getLocked(LazyReference.java:46)
      at org.codehaus.groovy.util.LazyReference.get(LazyReference.java:33)
      at org.codehaus.groovy.reflection.CachedClass.getConstructors(CachedClass.java:258)
      at groovy.lang.MetaClassImpl.&lt;init&gt;(MetaClassImpl.java:213)
      at groovy.lang.MetaClassImpl.&lt;init&gt;(MetaClassImpl.java:223)
      at groovy.lang.MetaClassRegistry$MetaClassCreationHandle.createNormalMetaClass(MetaClassRegistry.java:168)
      at groovy.lang.MetaClassRegistry$MetaClassCreationHandle.createWithCustomLookup(MetaClassRegistry.java:158)
      at groovy.lang.MetaClassRegistry$MetaClassCreationHandle.create(MetaClassRegistry.java:141)
      at org.codehaus.groovy.reflection.ClassInfo.getMetaClassUnderLock(ClassInfo.java:209)
      at org.codehaus.groovy.reflection.ClassInfo.getMetaClass(ClassInfo.java:241)
      at org.codehaus.groovy.runtime.metaclass.MetaClassRegistryImpl.getMetaClass(MetaClassRegistryImpl.java:255)
      at org.codehaus.groovy.runtime.InvokerHelper.getMetaClass(InvokerHelper.java:859)
      at org.codehaus.groovy.runtime.callsite.CallSiteArray.createCallConstructorSite(CallSiteArray.java:84)
      at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallConstructor(CallSiteArray.java:57)
      at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:182)
      at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:194)
      at com.sample.MongoService.client(MongoService.groovy:14)
      at com.sample.MongoService.collection(MongoService.groovy:21)
      at com.sample.MongoService$collection.call(Unknown Source)
      at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
      at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
      at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
      at com.sample.MongoDBController.method(MongoDBController.groovy:10)
      at com.sample.MongoDBController$method.call(Unknown Source)
      at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
      at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
      at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:112)
      at com.sample.GroovyDemoApp.main(GroovyDemoApp.groovy:12)
  Caused by: java.lang.ClassNotFoundException: com.mongodb.operation.OperationExecutor
      at java.net.URLClassLoader$1.run(Unknown Source)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.net.URLClassLoader.findClass(Unknown Source)
      at java.lang.ClassLoader.loadClass(Unknown Source)
      at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
      at java.lang.ClassLoader.loadClass(Unknown Source)
      at java.lang.ClassLoader.loadClassInternal(Unknown Source)
      ... 35 more
  </code></pre>
  
  <p><strong>Update :</strong></p>
  
  <p>This is my Groovy code. with this code i am trying to connect Mongodb</p>
  
  <pre><code>// error is in this line . i .e - 
  // The type com.mongodb.operation.CreateIndexesOperation cannot be resolved.
  // It is indirectly referenced from required .class files )
  package com.sample
  
  import com.mongodb.*
  
  class MongoService { 
    private static MongoClient mongoClient 
    private static host = "localhost" //your host name 
    private static port = 27017 //your port no. 
    private static databaseName = "db"
  
    public static MongoClient client() {
      if(mongoClient == null){
          return new MongoClient(host,port)
      }else {
          return mongoClient
      }
    }
  
    public DBCollection collection(collectionName) {
      DB db = client().getDB(databaseName)
      return db.getCollection(collectionName)
    }
  }
  </code></pre>