Java – Problems building Drools 4 project in Eclipse

buildingdroolseclipsejava

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?

Best Answer

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());