Search code examples
javadrools

Unable to instantiate service for Class 'org.drools.builder.KnowledgeBuilderFactoryService'


I updated from Drools 5.5.0 to Drools 6.1.0.

And when I execute my code, I have this problem.

java.lang.IllegalArgumentException: Unable to instantiate service for Class 'org.drools.builder.KnowledgeBuilderFactoryService'
    at org.drools.util.ServiceRegistryImpl.get(ServiceRegistryImpl.java:166)
    at org.drools.builder.KnowledgeBuilderFactory.loadServiceFactory(KnowledgeBuilderFactory.java:130)
    at org.drools.builder.KnowledgeBuilderFactory.getKnowledgeBuilderServiceFactory(KnowledgeBuilderFactory.java:124)
    at org.drools.builder.KnowledgeBuilderFactory.newKnowledgeBuilder(KnowledgeBuilderFactory.java:46)
    at com.drools.optimizer.AppExpertSystem.readKnowledgeBase(AppExpertSystem.java:133)
    at com.drools.optimizer.AppExpertSystem.loadKnowledgeBase(AppExpertSystem.java:119)
    at com.drools.optimizer.AppExpertSystem.<init>(AppExpertSystem.java:56)
    at eventlauncher.OptimizerLauncher.NuevoEscenario(OptimizerLauncher.java:57)
    at eventlauncher.DemoPruebas.main(DemoPruebas.java:18)
Caused by: java.lang.IllegalArgumentException: Unable to instantiate 'org.drools.builder.impl.KnowledgeBuilderFactoryServiceImpl'
    at org.drools.util.ServiceRegistryImpl$ReflectionInstantiator.newInstance(ServiceRegistryImpl.java:217)
    at org.drools.util.ServiceRegistryImpl$ReflectionInstantiator.call(ServiceRegistryImpl.java:209)
    at org.drools.util.ServiceRegistryImpl.get(ServiceRegistryImpl.java:164)
    ... 8 more
Caused by: java.lang.ClassNotFoundException: org.drools.builder.impl.KnowledgeBuilderFactoryServiceImpl
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:169)
    at org.drools.util.ServiceRegistryImpl$ReflectionInstantiator.newInstance(ServiceRegistryImpl.java:214)
    ... 10 more
Exception in thread "main" java.lang.RuntimeException: java.lang.IllegalArgumentException: Unable to instantiate service for Class 'org.drools.builder.KnowledgeBuilderFactoryService'
    at com.drools.optimizer.AppExpertSystem.loadKnowledgeBase(AppExpertSystem.java:126)
    at com.drools.optimizer.AppExpertSystem.<init>(AppExpertSystem.java:56)
    at eventlauncher.OptimizerLauncher.NuevoEscenario(OptimizerLauncher.java:57)
    at eventlauncher.DemoPruebas.main(DemoPruebas.java:18)
Caused by: java.lang.IllegalArgumentException: Unable to instantiate service for Class 'org.drools.builder.KnowledgeBuilderFactoryService'
    at org.drools.util.ServiceRegistryImpl.get(ServiceRegistryImpl.java:166)
    at org.drools.builder.KnowledgeBuilderFactory.loadServiceFactory(KnowledgeBuilderFactory.java:130)
    at org.drools.builder.KnowledgeBuilderFactory.getKnowledgeBuilderServiceFactory(KnowledgeBuilderFactory.java:124)
    at org.drools.builder.KnowledgeBuilderFactory.newKnowledgeBuilder(KnowledgeBuilderFactory.java:46)
    at com.drools.optimizer.AppExpertSystem.readKnowledgeBase(AppExpertSystem.java:133)
    at com.drools.optimizer.AppExpertSystem.loadKnowledgeBase(AppExpertSystem.java:119)
    ... 3 more
Caused by: java.lang.IllegalArgumentException: Unable to instantiate 'org.drools.builder.impl.KnowledgeBuilderFactoryServiceImpl'
    at org.drools.util.ServiceRegistryImpl$ReflectionInstantiator.newInstance(ServiceRegistryImpl.java:217)
    at org.drools.util.ServiceRegistryImpl$ReflectionInstantiator.call(ServiceRegistryImpl.java:209)
    at org.drools.util.ServiceRegistryImpl.get(ServiceRegistryImpl.java:164)
    ... 8 more
Caused by: java.lang.ClassNotFoundException: org.drools.builder.impl.KnowledgeBuilderFactoryServiceImpl
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:169)
    at org.drools.util.ServiceRegistryImpl$ReflectionInstantiator.newInstance(ServiceRegistryImpl.java:214)
    ... 10 more

The line 133 in which is the error is the bold line in the next method:

private static KnowledgeBase readKnowledgeBase() throws Exception {

        **KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();**

        kbuilder.add(ResourceFactory.newClassPathResource(CTES.RULES),
                ResourceType.DRL);

        KnowledgeBuilderErrors errors = kbuilder.getErrors();

        if (errors.size() > 0) {
            for (KnowledgeBuilderError error : errors) {
                System.err.println(error);
            }
            throw new IllegalArgumentException("Could not parse knowledge.");
        }
        KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
        kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
        KnowledgeBaseConfiguration config = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
        config.setOption(EventProcessingOption.STREAM );

        return kbase;
    }

I don't have change any line of my code, and I don't understand what it is happening :(

Anybody can help me?

Best regards!


Solution

  • Here's a simple compile and create session according to 6.x. See the javadoc for further options and details.

    import org.kie.api.*;
    import org.kie.api.builder.*;
    import org.kie.api.runtime.*;
    
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem();
    FileInputStream fis = new FileInputStream( CTES.RULES );
    kfs.write( "src/main/resources/sale.drl",
                ks.getResources().newInputStreamResource( fis ) );
    KieBuilder kieBuilder = ks.newKieBuilder( kfs ).buildAll();
    Results results = kieBuilder.getResults();
    if( results.hasMessages( Message.Level.ERROR ) ){
         System.out.println( results.getMessages() );
         throw new IllegalStateException( "### errors ###" );
    }
    KieContainer kieContainer =
         ks.newKieContainer( ks.getRepository().getDefaultReleaseId() );
    
    KieBaseConfiguration config = ks.newKieBaseConfiguration();
    config.setOption( EventProcessingOption.STREAM );
    KieBase kieBase = kieContainer.newKieBase( config );
    KieSession kieSession = kieBase.newKieSession();