Problems building Drools 4 project in Eclipse

2019-07-21 03:47发布

I'm having trouble compiling a drools 4 project. I'm getting errors in the rules file saying

Only a type can be imported. <<MyClassName>> resolves to a package 

The incremental compiler isn't working because of this. How do I fix the errors or get eclipse to ignore them?

3条回答
等我变得足够好
2楼-- · 2019-07-21 04:14

This issue was mentioned for a migration from drools 3.06 to 4.0.7, so what version of eclipse and drools are you using?

This might be related to a classpath issue:

Using the debugger I realized that the Drools PackageBuilder tried to load the classes from the

Thread.currentThread().getContextClassLoader();

This ClassLoader does not contain my agent classes! Even the system class loader does not contain my classes.

The solution was:

Instead of creating plain PackageBuilder and RuleBase instances, one has to create them with a PackageBuilderConfiguration and a RuleBaseConfiguration both with the current classLoader configured:

ClassLoader classLoader = this.getClass().getClassLoader();

PackageBuilderConfiguration configuration = new PackageBuilderConfiguration();
configuration.setClassLoader(classLoader);

PackageBuilder builder = new PackageBuilder(configuration);

builder.addPackageFromDrl(source);

RuleBaseConfiguration ruleBaseConfiguration = new RuleBaseConfiguration();
ruleBaseConfiguration.setClassLoader(classLoader);

ruleBase = RuleBaseFactory.newRuleBase(ruleBaseConfiguration);
ruleBase.addPackage(builder.getPackage());
查看更多
Lonely孤独者°
3楼-- · 2019-07-21 04:32

Ensure that the MyClassName or any other class(es) that you use from the rule are in a jar file and the jar file is in classpath.

查看更多
等我变得足够好
4楼-- · 2019-07-21 04:33

Hmmm, I cleaned the project and that resolved the problem.

查看更多
登录 后发表回答