2

I just started learning Drools, but I'm having a bad time with it, I'm trying a simple project, but I'm getting this error.

Exception in thread "main" java.lang.NoClassDefFoundError: org/kie/api/KieServices$Factory at com.sample.App.main(App.java:12)

All suggestions of Stackoverflow and other discussion did not help. Does anyone have a solution? When I use mvn clean install is BUILD SUCCESS

This is my code

com.sample.App

package com.sample;

import com.model.Item;
import org.kie.api.KieServices;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;

public class App {
    public static void main(String[] args)
    {
        System.out.println("Rule engine");
        KieServices ks = KieServices.Factory.get();
        KieContainer kContainer = ks.getKieClasspathContainer();
        KieSession kSession = kContainer.newKieSession();

        Item item = new Item("A", (float) 123.35);
        System.out.println("Item category: " + item.getCategory());
        kSession.insert(item);
        int fired = kSession.fireAllRules();
        System.out.println("Number of Rules executed: " + fired);
        System.out.println("Item Category: " + item.getCategory());
    }
}

com.sample.model

package com.model;

public class Item {
    private String category;
    private Float cost;

    public Item(String category, Float cost) {
        this.category = category;
        this.cost = cost;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public Float getCost() {
        return cost;
    }

    public void setCost(Float cost) {
        this.cost = cost;
    }
}

This is my pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>DroolCmonDude2</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.kie</groupId>
            <artifactId>kie-api</artifactId>
            <version>7.59.0.Final</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.drools</groupId>
            <artifactId>drools-core</artifactId>
            <version>7.59.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.drools</groupId>
            <artifactId>drools-compiler</artifactId>
            <version>7.59.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.drools</groupId>
            <artifactId>drools-decisiontables</artifactId>
            <version>7.59.0.Final</version>
        </dependency>
    </dependencies>

</project>

And my Rules.drl at resources.rules

package resources.rules;

import com.model.Item;

rule "Classify Item - Low Range"
    when
        $i: Item(cost < 200)
    then
        $i.setCategory(Category.LOW_RANGE);
end

My kmodule.xml

<kmodule xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://jboss.org/kie/6.0.0/kmodule">
</kmodule>

SOLUTION

As @RoddyoftheFrozenPeas indicated the solution was to add the dependencies for drools-mvel, drools-model-compiler and sfj4 and works. This is for Drools 7> This how my pom looks now:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>DroolsAnotherChance</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.drools</groupId>
            <artifactId>drools-core</artifactId>
            <version>7.59.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.drools</groupId>
            <artifactId>drools-compiler</artifactId>
            <version>7.59.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.drools</groupId>
            <artifactId>drools-decisiontables</artifactId>
            <version>7.59.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.drools</groupId>
            <artifactId>drools-mvel</artifactId>
            <version>7.59.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.drools</groupId>
            <artifactId>drools-model-compiler</artifactId>
            <version>7.59.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.25</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-nop</artifactId>
            <version>1.7.25</version>
        </dependency>
    </dependencies>

</project>
eehmull
  • 23
  • 9
  • 2
    Does this answer your question? [How can I solve "java.lang.NoClassDefFoundError"?](https://stackoverflow.com/questions/17973970/how-can-i-solve-java-lang-noclassdeffounderror) I don't know for sure, but your specific issue looks like you have only added the dependency for `kie-api` in your pom, but you don't have the actual kie classes/library that the kie-api depends upon to run or compile your project. – sorifiend Sep 27 '21 at 23:34
  • 1
    Possibly helpful in your situation where you can build the project but not run it: https://stackoverflow.com/questions/54304712/how-to-fix-noclassdeffounderror-kieservices-factory The short version is that the library files are not distributed with the complete project (you need to copy them manually), or you need to build a uber/fat jar (that contains all the required libraries). – sorifiend Sep 27 '21 at 23:42
  • Thank you very much for your help @sorifiend I really apreciate. I Add more dependencies to the pom for Kie, but didn't work, I think I will have to try to copy them manually but, It's so weird, and in such small project I mean it's just knda a Hello world but I don't have success, and I need to implement drools to a very large project, I'll keep searching. But thanks for the help – eehmull Sep 28 '21 at 04:19
  • 1
    Your kmodule xml is incomplete, but that's not relevant. You also don't need the kie-api at all. The question linked to by sorifiend is not relevant for modern Drools. For > 7.45 you need also either drools-mvel or drools-model-compiler. Refer to the [docs](https://docs.drools.org/latest/drools-docs/html_single/#_new_wrapper_modules) about the breaking changes introduced in that release. – Roddy of the Frozen Peas Sep 28 '21 at 18:53
  • 1
    @RoddyoftheFrozenPeas you are right!! It worked, I had to add the dependecies for slf4j and didn't need the kie-api as you said. – eehmull Sep 28 '21 at 22:43
  • Glad it worked for you. Technically you only need EITHER drools-mvel OR drools-model-compiler (depending on whether you're using mvel or not). In older Drools this logic was bundled directly into the drools-compiler dependency; 7.45 introduced the breaking change where it was broken out into a separate dependency which has caused all sorts of headaches. – Roddy of the Frozen Peas Sep 29 '21 at 16:11
  • 1
    I tried only adding one of the two but didn't work, I needed both for some reason. Thanks for the help @RoddyoftheFrozenPeas I was having a bad time learning Drools hahaha – eehmull Sep 30 '21 at 18:36

1 Answers1

2

There was a non-backwards-compatible change to the Drools library introduced in 7.45. You can read about it in the documentation's release notes here.

Basically, starting in 7.45, the introduction of "executable models" caused mvel support to be broken out into a separate module. As a result you now need to include one of the following dependencies:

  • drools-mvel-compiler
  • drools-model-compiler

Which one you need depends on what you're actually doing. Prior to 7.45, the mvel compiler support was part of the drools-compiler dependency, so this extra dependency was not necessary.

(As a note, you don't need the kie-api dependency.)

Roddy of the Frozen Peas
  • 14,380
  • 9
  • 49
  • 99