ks.getKieClasspathContainer()抛出java.lang.ExceptionInInitializerError

时间:2015-07-24 13:55:02

标签: android android-studio drools

我一直在尝试将Drools生态系统整合到我的基本Android应用程序中。 到目前为止,我已经能够集成所有必需的Drools库,并且我还基于Drools HelloWorld示例实现了一个简短的Drools算法。

不知何故,我仍然遇到以下几个问题:

KieServices ks = KieServices.Factory.get();
KieContainer kContainer = ks.getKieClasspathContainer();

ks正在实施,但当我们到达ks.getKieClasspathContainer()时,会抛出以下异常:

java.lang.ExceptionInInitializerError

在调试模式下,我还能够看到有关异常的以下详细信息:

Cause: java.lang.IllegalArgumentException
Details: http://www.w3.org/2001/xmlschema

这是我的DroolsTest.java文件:

public class DroolsTest {

    public static  void test() {
        try {
            // load up the knowledge base
            KieServices ks = KieServices.Factory.get();
            KieContainer kContainer = ks.getKieClasspathContainer();
            KieSession kSession = kContainer.newKieSession("ksession-rules");

            Purchase cashPurchase = new Purchase("john", 150, PaymentMethod.CASH);
            Purchase debitPurchase = new Purchase("peter", 200, PaymentMethod.DEBIT);
            Purchase creditPurchase = new Purchase("george", 300, PaymentMethod.CREDIT);

            kSession.insert(cashPurchase);
            kSession.insert(debitPurchase);
            kSession.insert(creditPurchase);

            kSession.fireAllRules();

        } catch (Throwable t) {
            Log.e("error", "error in drools");
        }
    }
}

这是我的MainActivity.java文件:

public class MainActivity extends AppCompatActivity {

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public void rules(View view){
        DroolsTest.test();
    }
}

单击界面中的“确定”按钮时,将调用rules()中的MainActivity函数。

有谁知道如何解决此错误?

非常感谢。

ETA: 您可以在GitHub上找到整个代码。

ETA: 这是堆栈跟踪:

java.lang.ExceptionInInitializerError
        at org.drools.compiler.kproject.models.KieModuleModelImpl$kModuleMarshaller.fromXML(KieModuleModelImpl.java:189)
        at org.drools.compiler.kproject.models.KieModuleModelImpl.fromXML(KieModuleModelImpl.java:139)
        at org.drools.compiler.kie.builder.impl.ClasspathKieProject.fetchKModule(ClasspathKieProject.java:163)
        at org.drools.compiler.kie.builder.impl.ClasspathKieProject.fetchKModule(ClasspathKieProject.java:125)
        at org.drools.compiler.kie.builder.impl.ClasspathKieProject.discoverKieModules(ClasspathKieProject.java:96)
        at org.drools.compiler.kie.builder.impl.ClasspathKieProject.init(ClasspathKieProject.java:68)
        at org.drools.compiler.kie.builder.impl.KieContainerImpl.<init>(KieContainerImpl.java:84)
        at org.drools.compiler.kie.builder.impl.KieServicesImpl.newKieClasspathContainer(KieServicesImpl.java:83)
        at org.drools.compiler.kie.builder.impl.KieServicesImpl.getKieClasspathContainer(KieServicesImpl.java:66)
        at org.drools.compiler.kie.builder.impl.KieServicesImpl.getKieClasspathContainer(KieServicesImpl.java:57)
        at com.example.jeanineharb.myapplication.DroolsTest.test(DroolsTest.java:15)
        at com.example.jeanineharb.myapplication.MainActivity.rules(MainActivity.java:40)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at android.view.View$1.onClick(View.java:4015)
        at android.view.View.performClick(View.java:4780)
        at android.view.View$PerformClick.run(View.java:19866)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5257)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
 Caused by: java.lang.IllegalArgumentException: http://www.w3.org/2001/XMLSchema
        at javax.xml.validation.SchemaFactory.newInstance(SchemaFactory.java:192)
        at org.drools.compiler.kproject.models.KieModuleModelImpl$KieModuleValidator.loadSchema(KieModuleModelImpl.java:236)
        at org.drools.compiler.kproject.models.KieModuleModelImpl$KieModuleValidator.<clinit>(KieModuleModelImpl.java:233)
            at org.drools.compiler.kproject.models.KieModuleModelImpl$kModuleMarshaller.fromXML(KieModuleModelImpl.java:189)
            at org.drools.compiler.kproject.models.KieModuleModelImpl.fromXML(KieModuleModelImpl.java:139)
            at org.drools.compiler.kie.builder.impl.ClasspathKieProject.fetchKModule(ClasspathKieProject.java:163)
            at org.drools.compiler.kie.builder.impl.ClasspathKieProject.fetchKModule(ClasspathKieProject.java:125)
            at org.drools.compiler.kie.builder.impl.ClasspathKieProject.discoverKieModules(ClasspathKieProject.java:96)
            at org.drools.compiler.kie.builder.impl.ClasspathKieProject.init(ClasspathKieProject.java:68)
            at org.drools.compiler.kie.builder.impl.KieContainerImpl.<init>(KieContainerImpl.java:84)
            at org.drools.compiler.kie.builder.impl.KieServicesImpl.newKieClasspathContainer(KieServicesImpl.java:83)
            at org.drools.compiler.kie.builder.impl.KieServicesImpl.getKieClasspathContainer(KieServicesImpl.java:66)
            at org.drools.compiler.kie.builder.impl.KieServicesImpl.getKieClasspathContainer(KieServicesImpl.java:57)
            at com.example.jeanineharb.myapplication.DroolsTest.test(DroolsTest.java:15)
            at com.example.jeanineharb.myapplication.MainActivity.rules(MainActivity.java:40)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at android.view.View$1.onClick(View.java:4015)
            at android.view.View.performClick(View.java:4780)
            at android.view.View$PerformClick.run(View.java:19866)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

NB: 我正在使用Drools v6.2.0。

1 个答案:

答案 0 :(得分:0)

问题是由于KieModuleImpl中的代码试图获取类com.sun.org.apache.xerces.internal.jaxp.validation.XMLSchemaFactory的对象,即用于为该Schema语言加载XML Schema工厂的对象。确保在类路径中有一些库可以找到该类。 (要检查,运行这个简单的代码:

SchemaFactory factory =
  SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
System.out.println( factory.getClass() );

但是有可能完全避免KieContainer并从KieFileSystem构建(可能)DRL资源被复制到其中。你试过这个吗?