Home > Enterprise >  Problems with Dependencies Exception in thread "main" java.lang.NoClassDefFoundError: org/
Problems with Dependencies Exception in thread "main" java.lang.NoClassDefFoundError: org/

Time:10-01

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>

CodePudding user response:

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.)

  • Related