简体   繁体   中英

NoClassDefFoundError and ClassNotFoundException on JavaCV library

I'm trying to use JavaCV in Android Studio but every time I tried to use the library and run it, it crashes and the logcat shows this error..

01-03 09:16:32.242 15233-15233/com.example.user.dmr E/AndroidRuntime: FATAL 

EXCEPTION: main
01-03 09:16:32.242 15233-15233/com.example.user.dmr E/AndroidRuntime: java.lang.NoClassDefFoundError: java.lang.ClassNotFoundException: org.bytedeco.javacpp.opencv_core
01-03 09:16:32.242 15233-15233/com.example.user.dmr E/AndroidRuntime:     at org.bytedeco.javacpp.Loader.load(Loader.java:392)
01-03 09:16:32.242 15233-15233/com.example.user.dmr E/AndroidRuntime:     at org.bytedeco.javacpp.Loader.load(Loader.java:358)
01-03 09:16:32.242 15233-15233/com.example.user.dmr E/AndroidRuntime:     at org.bytedeco.javacpp.opencv_imgcodecs.<clinit>(opencv_imgcodecs.java:13)
01-03 09:16:32.242 15233-15233/com.example.user.dmr E/AndroidRuntime:     at com.example.user.dmr.MainActivity.onCreate(MainActivity.java:20)
01-03 09:16:32.242 15233-15233/com.example.user.dmr E/AndroidRuntime:     at android.app.Activity.performCreate(Activity.java:5122)
01-03 09:16:32.242 15233-15233/com.example.user.dmr E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1084)
01-03 09:16:32.242 15233-15233/com.example.user.dmr E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
01-03 09:16:32.242 15233-15233/com.example.user.dmr E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
01-03 09:16:32.242 15233-15233/com.example.user.dmr E/AndroidRuntime:     at android.app.ActivityThread.access$600(ActivityThread.java:162)
01-03 09:16:32.242 15233-15233/com.example.user.dmr E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
01-03 09:16:32.242 15233-15233/com.example.user.dmr E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:107)
01-03 09:16:32.242 15233-15233/com.example.user.dmr E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:194)
01-03 09:16:32.242 15233-15233/com.example.user.dmr E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5392)
01-03 09:16:32.242 15233-15233/com.example.user.dmr E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
01-03 09:16:32.242 15233-15233/com.example.user.dmr E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:525)
01-03 09:16:32.242 15233-15233/com.example.user.dmr E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
01-03 09:16:32.242 15233-15233/com.example.user.dmr E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
01-03 09:16:32.242 15233-15233/com.example.user.dmr E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method)
01-03 09:16:32.242 15233-15233/com.example.user.dmr E/AndroidRuntime:  Caused by: java.lang.ClassNotFoundException: org.bytedeco.javacpp.opencv_core
01-03 09:16:32.242 15233-15233/com.example.user.dmr E/AndroidRuntime:     at java.lang.Class.classForName(Native Method)
01-03 09:16:32.242 15233-15233/com.example.user.dmr E/AndroidRuntime:     at java.lang.Class.forName(Class.java:217)

This is the java code in the Android Studio that I'm trying to run:

package com.example.user.dmr;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

import static org.bytedeco.javacpp.opencv_core.*;
import static org.bytedeco.javacpp.opencv_imgcodecs.cvLoadImage;


public class MainActivity extends AppCompatActivity {

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

        TextView v = (TextView) findViewById(R.id.textview1);

        IplImage image = cvLoadImage("drawable/face1.JPG"); //crashes when it read this line

        if (image == null) {
            v.setText("Fail");
        } else {
            v.setText("Succeed");
        }
    }
}

I've already added the javacv library in the external libraries. This is the dependencies I've added into build.gradle.

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile group: 'org.bytedeco', name: 'javacv', version: '1.0'
}

Any idea why it keeps happening and how to fix it? Thanks.

I think you need javacpp-presets also.

Try this in your gradle dependencies:

compile group: 'org.bytedeco.javacpp-presets', name: 'opencv', version: '3.0.0' + '-1.0'

compile group: 'org.bytedeco', name: 'javacv', version: '1.0'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM