本文介绍了尝试为GreenDAO运行DaoGenerator时出现NoClassDefFoundError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用Android Studio 2.3的Android项目,该项目使用GreenDAO生成与SQLite数据库进行交互的类. DaoGenerator项目以前一直可以工作...但是今天,我只需要向Entity中添加2列/属性,每当我尝试运行Generator项目时,都会出现以下错误:

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)

我在应用程序的Gradle文件中使用GreenDAO 3.2.0:

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

compile 'org.greenrobot:greendao:3.2.0'

此外,在DaoGenerator的Gradle文件中:

Also, in DaoGenerator's Gradle file:

apply plugin: 'java'

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

我的ProjectDaoGenerator.java文件:

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");
    }
}

好像找不到org.greenrobot.greendao.generator.Schema.

推荐答案

为生成器设置build.gradle文件,如下所示(特别注意mainClassName):

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

然后单击Android Studio右侧栏中的渐变"标签,然后选择生成器的运行"任务,如下所示:

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

它对我有用,更多详细信息请查看链接: https://github.com/greenrobot/greenDAO/issues/619 http://greenrobot.org/greendao/documentation/generator/#Triggering_generation

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

这篇关于尝试为GreenDAO运行DaoGenerator时出现NoClassDefFoundError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-02 03:08