Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NoClassDefFoundError when trying to run DaoGenerator for GreenDAO

I have an Android Project, using Android Studio 2.3, which uses GreenDAO to generate the classes to interact with the SQLite database. The DaoGenerator project always worked before... but today I just needed to add 2 columns/properties to an Entity and whenever I try to run the generator project, I get the following error:

Exception in thread "main" java.lang.NoClassDefFoundError: org/greenrobot/greendao/generator/Schema
    at com.company.daogenerator.ProjectDaoGenerator.main(ProjectDaoGenerator.java:20)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Caused by: java.lang.ClassNotFoundException: org.greenrobot.greendao.generator.Schema
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)

I'm using GreenDAO 3.2.0 in my application's Gradle file:

compile 'org.greenrobot:greendao:3.2.0'

Also, in DaoGenerator's Gradle file:

apply plugin: 'java'

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'org.greenrobot:greendao-generator:3.2.0'
}

My ProjectDaoGenerator.java file:

package com.company.daogenerator;

import org.greenrobot.greendao.generator.DaoGenerator;
import org.greenrobot.greendao.generator.Entity;
import org.greenrobot.greendao.generator.Property;
import org.greenrobot.greendao.generator.Schema;

public class ProjectDaoGenerator {
    private static Entity primaryKeyEntity;
    private static Entity itemTypeEntity;

    public static void main(String args[]) throws Exception {
        Schema schema = new Schema(1, "com.company.project.datamodel");
        schema.enableKeepSectionsByDefault();

        // Define entities
        Entity primaryKey = schema.addEntity("CDPrimaryKey");
        Entity installation = schema.addEntity("CDInstallation");

        // Z_PRIMARYKEY
        primaryKeyEntity = primaryKey;
        primaryKey.setTableName("Z_PRIMARYKEY");
        primaryKey.addLongProperty("ENT").columnName("Z_ENT").primaryKey();
        primaryKey.addIntProperty("MAX").columnName("Z_MAX");
        primaryKey.addStringProperty("NAME").columnName("Z_NAME");
        primaryKey.addIntProperty("SUPER").columnName("Z_INT");

        // CDInstallation
        installation.setTableName("ZCDINSTALLATION");
        installation.addLongProperty("packageDate").columnName("ZPACKAGEDATE");

        (...) // Other Properties

        // **** Generate Schema ****
        new DaoGenerator().generateAll(schema, "app/src/main/java");
    }
}

It's as if it couldn't find org.greenrobot.greendao.generator.Schema.

like image 706
rgomesbr Avatar asked Mar 16 '17 19:03

rgomesbr


People also ask

How to create a greendao generator using greenrobot?

Add 'org.greenrobot:greendao-gradle-plugin:3.2.0' as a class path to your dependencies and sync the gradle file We'll create a greenDao generator here. This generator will be responsible for automatically creating entities and dao files. If you Give the module a name. Here, we will use: greenDaoGenerator

How do I integrate greendao with Android applications?

The following steps outline the procudure for integrating greenDAO with Android applications. If you have an app already, you can skip this step. Otherwise, create an Android project from Android Studio: Go to your build.gradle (Module:app) app level gradle file and add 'org.greenrobot:greendao:3.2.0' to your dependencies.

What is the NoClassDefFoundError?

The NoClassDefFoundError is an error when the required class definition is not available at runtime. The NoClassDefFoundError is available in LinkageError.

How do I create a dao file in Gradle?

Open the gradle for the new module, add org.greenrobot:greendao-generator:3.2.0 to the dependencies, and sync. Now we are going to modify our generator class so we can generate the dao files and entities (Tables).


3 Answers

Set the build.gradle file for your generator like this (especially note the mainClassName):

enter image description here

Then click the "Gradle" tab in the right sidebar of Android Studio and select the "run" task of your daogenerator like this:

enter image description here

It's worked for me , more details check link : https://github.com/greenrobot/greenDAO/issues/619 http://greenrobot.org/greendao/documentation/generator/#Triggering_generation

like image 101
jesto paul Avatar answered Oct 19 '22 00:10

jesto paul


Apart from what @Jesto Paul mentioned, I changed the following in the Generator class

new DaoGenerator().generateAll(schema, "./app/src/main/java"); - shows Path not exist.

to

new DaoGenerator().generateAll(schema, "../app/src/main/java");

(added double dot for the path). After doing this the generator create the tables to the folder.

like image 22
droid kid Avatar answered Oct 18 '22 23:10

droid kid


For some reason, I ran into the same problem after updating android buildToolsVersion.

After some time of searching, i've accidentally checked "Run > Edit Configurations..." for the DaoGenerator-Application.

In the list of JRE "Android API 25 Platform" was selected. So I changed it back to the external Java running on my computer (e.g. "1.8", did it before some days ago). That solved it for me.

Edit: in this project I use GreenDAO 2.1.0

Edit 2:

https://github.com/greenrobot/greenDAO/issues/619 - http://greenrobot.org/greendao/documentation/generator/#Triggering_generation

like image 1
BenRoob Avatar answered Oct 18 '22 23:10

BenRoob