简体   繁体   中英

newKieSession() returns null when project is exported to JAR

Very confusing problem here. I see behavior which differs between running the project in Eclipse and running the project as an exported JAR. When the project runs in Eclipse, it runs fine. When it runs as a JAR, it crashes. The crash results from the newKieSession method.

Here is the code block:

KieServices ks = KieServices.Factory.get();
KieContainer kContainer = ks.getKieClasspathContainer();
KieSession kSession = kContainer.newKieSession("ksession-rules");

When run from Eclipse, it works fine. When I export the JAR and run it, the kSession is null . Later on I access the kSession object which causes a NullPointerException . I have tried breaking immediately following the above line just to confirm I'm not somehow nullifying it later. I can confirm that ksession is null immediately following that call to newKieSession() .

Does anyone have any idea why the behavior would differ? I have confirmed I am exporting using the exact same run configuration. When I export, I have tried both "Extract required libraries into generated JAR" as well as "Package required libraries into generated jar" . The error happens with both.

UPDATE:

Noticed another error message in the deluge of information returned at the console before the exception.

[main] ERROR org.drools.compiler.kie.builder.impl.KieContainerImpl - Unknown KieSession name: ksession-rules

I don't get this when I run via Eclipse.

UPDATE:

Attached is the structure of the project in Eclipse. I have expanded the resource folder since it seems the most relevant to the situation.

在此处输入图片说明

UPDATE:

The jar file contains a resources folder with the same structure as the eclipse screenshot. So, at the root of the jar, we can find the following:

/resources/rules/*.drl
/resources/META-INF/kmodule.xml

Content of kmodule.xml:

<?xml version="1.0" encoding="UTF-8"?>
<kmodule xmlns="http://jboss.org/kie/6.0.0/kmodule">
    <kbase name="rules" packages="com.company.project">
        <ksession name="ksession-rules"/>
    </kbase>
</kmodule>

I also tried the following kmodule with the same results:

<?xml version="1.0" encoding="UTF-8"?>
<kmodule xmlns="http://jboss.org/kie/6.0.0/kmodule">
    <kbase name="rules" packages="rules">
        <ksession name="ksession-rules"/>
    </kbase>
</kmodule>

UPDATE

The root of the jar appears to have a second META-INF folder. So the jar has the following:

/META-INF
/resources/rules/*.drl
/resources/META-INF/kmodule.xml

I tried building a sample drools program, and I noted that the kmodule.xml file ends up at /META-INF/kmodule.xml when sample drools program is exported to jar. I have no idea why the behavior of my jar export is different. The paths in the project are exactly the same and I am exporting it using exactly the same process.

The problem was in Eclipses build path settings. The resources folder had a value set for "Included". After removing that (thus setting "Included" to "All"), the build began to work once again.

If someone is experiencing this problem you can find the settings I am describing by:

Project Properties > Java Build Path > Source > /src/main/resources > Included > Edit

Thanks to Esteban for suggesting to me there might be a problem with the classpath.

I had the same error message. I removed the "ksession-rules" parameter, it fixed the issue for me.

Change => KieSession kSession = kContainer.newKieSession("ksession-rules");

to => KieSession kSession = kContainer.newKieSession();

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