简体   繁体   中英

Getting a No Class Definition Found Error in mistake?

I am writing an android app but using JUnit to test it out some of the more java-specific functionality before creating activities and the android-specific functions. As I write out the JUnit tests and execute them bit by bit, I am finding failures at the line dbHelper = new DatabaseSystemHelper(context, null, null, 0);

public DatabaseSystem(Context context) {
   dbHelper = new DatabaseSystemHelper(context, null, null, 0);
   database = dbHelper.getWritableDatabase();
}


class DatabaseSystemHelper extends SQLiteOpenHelper {
...
    DatabaseSystemHelper(Context context, String name, CursorFactory factory, int version) {
        super(context, name, factory, version);
        setDatabaseValues(context);
    }


**java.lang.NoClassDefFoundError: android/database/sqlite/SQLiteOpenHelper**
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    at edu.project2.systems.DatabaseSystem.<init>(DatabaseSystem.java:58)
    at edu.project2.systems.DatabaseSystem$SingletonHolder.<clinit>(DatabaseSystem.java:193)
    at edu.project2.systems.DatabaseSystem.getInstance(DatabaseSystem.java:203)
    at edu.project2.systems.DatabaseSystemTest.setUp(DatabaseSystemTest.java:28)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.ClassNotFoundException: android.database.sqlite.SQLiteOpenHelper
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    ... 42 more

I am at a bit of a loss. It seems that my code never gets past the line for making a new DatabaseHelper object. In fact, when I "step-into" the call, the debug cursor never even gets into the constructor. Any advice would be great

Please make sure the class SQLiteOpenHelper is available to the JVM at runtime.

The NoClassDefFoundError usually occurs when a class needed to compile the project is present during build time but missing during run time.

Ensure the class is included both in the build path and the classpath.

I got the tests to work by creating a separate Android Test Project and making my classes extend the AnroidTestCase class. https://developer.android.com/tools/testing/index.html

I know this answer isn't fully fleshed out, but in short it seems that you cannot use POJO's or run the regular JUnit/JVM when you are dealing with android specific classes since those classes need the Android emulator/devise to run on.

Here is what my test class looks like (keeping in my that it running in the android VM)

import android.content.Context; import android.test.AndroidTestCase;

public class DatabaseSystemsTest extends AndroidTestCase {
    private DatabaseSystem dbSystem;

    @Override
    protected void setUp() throws Exception {
        super.setUp();

        Context context = getContext();
        DatabaseSystem.setContext(context);
        dbSystem = DatabaseSystem.getInstance();
        dbSystem.getDbHelper().onCreate(dbSystem.getDatabase());

    } // end of setUp() method definition
}

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