NoClassDefFoundError和ExceptionInitializeError

时间:2017-02-21 14:51:01

标签: java mongodb

当我运行我应该使用mongolab连接到mongo数据库的代码时,我经常遇到以下错误。我也有一个try catch,它输出的错误是“ExceptionInitializeError”。请帮忙:(

 FATAL EXCEPTION: main
              Process: com.example.chadedwards.projectichnaea, PID: 8451
              java.lang.NoClassDefFoundError: com/mongodb/connection/ClientMetadataHelper
                  at com.mongodb.connection.InternalStreamConnectionFactory.<init>(InternalStreamConnectionFactory.java:41)
                  at com.mongodb.connection.DefaultClusterableServerFactory.create(DefaultClusterableServerFactory.java:68)
                  at com.mongodb.connection.BaseCluster.createServer(BaseCluster.java:360)
                  at com.mongodb.connection.SingleServerCluster.<init>(SingleServerCluster.java:54)
                  at com.mongodb.connection.DefaultClusterFactory.create(DefaultClusterFactory.java:114)
                  at com.mongodb.Mongo.createCluster(Mongo.java:744)
                  at com.mongodb.Mongo.createCluster(Mongo.java:728)
                  at com.mongodb.Mongo.<init>(Mongo.java:293)
                  at com.mongodb.Mongo.<init>(Mongo.java:288)
                  at com.mongodb.MongoClient.<init>(MongoClient.java:192)
                  at com.mongodb.MongoClient.<init>(MongoClient.java:168)
                  at com.example.chadedwards.projectichnaea.BusAlarmActivity.onCreate(BusAlarmActivity.java:37)
                  at android.app.Activity.performCreate(Activity.java:5231)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
                  at android.app.ActivityThread.access$800(ActivityThread.java:135)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:136)
                  at android.app.ActivityThread.main(ActivityThread.java:5017)
                  at java.lang.reflect.Method.invokeNative(Native Method)
                  at java.lang.reflect.Method.invoke(Method.java:515)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
                  at dalvik.system.NativeStart.main(Native Method)

我的傻瓜

    apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        multiDexEnabled true
        applicationId "com.example.chadedwards.projectichnaea"
        minSdkVersion 19
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {

    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.google.android.gms:play-services-maps:10.0.1'
    compile 'com.android.support:appcompat-v7:25.0.1'
    compile 'org.mongodb:mongodb-driver:3.4.2'
    testCompile 'junit:junit:4.12'
}

和我的代码

package com.example.chadedwards.projectichnaea;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoDatabase;

import java.util.Arrays;

public class BusAlarmActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bus_alarm);


       try {

           //MongoClient mongoClient = new MongoClient("ds063546.mlab.com", 63546);

           String userName = "admin";
           String database = "ichnaea";

           char[] password = new char[] {'W', 'e', 'l', 'c', 'o', 'm' , 'e', '1', '2', '3'};
           MongoCredential credential = MongoCredential.createCredential(userName, database, password);
           MongoClient mongoClient = new MongoClient(new ServerAddress("ds063546.mlab.com", 63546), Arrays.asList(credential));


           MongoDatabase db = mongoClient.getDatabase("test");

           DBCollection coll = (DBCollection) db.getCollection("testCollection");

           BasicDBObject doc = new BasicDBObject("name", "MongoDB")
                   .append("type", "database")
                   .append("count", 1)
                   .append("info", new BasicDBObject("x", 203).append("y", 102));
           coll.insert(doc);




    }
    catch(ExceptionInInitializerError ex){

        Toast.makeText(this, ex.toString() , Toast.LENGTH_SHORT).show();

    }

    }
}

编辑 - 经过更多的故障排除后,似乎问题从这行开始MongoClient mongoClient = new MongoClient

1 个答案:

答案 0 :(得分:2)

您缺少运行mongodb-driver的某些依赖项。

将这些行添加到gradle.build

compile group: 'org.mongodb', name: 'mongodb-driver-core', version: '3.4.2'
compile group: 'org.mongodb', name: 'bson', version: '3.4.2'

将这些放在当前的依赖项之后。