Problems with Dependencies Exception in thread "main" java.lang.NoClassDefFoundError: org/kie/api/KieServices$Factory with Drools version 7.59.0
Problems with Dependencies Exception in thread "main" java.lang.NoClassDefFoundError: org/kie/api/KieServices$Factory with Drools version 7.59.0
我刚开始学习 Drools,但使用起来并不顺利,我正在尝试一个简单的项目,但出现此错误。
Exception in thread "main" java.lang.NoClassDefFoundError: org/kie/api/KieServices$Factory at com.sample.App.main(App.java:12)
Whosebug 和其他讨论的所有建议都没有帮助。有没有人有办法解决吗?当我使用 mvn clean install 是 BUILD SUCCESS
这是我的代码
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;
}
}
这是我的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>
我的 Rules.drl 在 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
我的kmodule.xml
<kmodule xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://jboss.org/kie/6.0.0/kmodule">
</kmodule>
解决方案
正如@RoddyoftheFrozenPeas 指出的那样,解决方案是添加 drools-mvel、drools-model-compiler 和 sfj4 的依赖项并起作用。这是给 Drools 7>
这是我的 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>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>
7.45 中引入的 Drools 库有一个非向后兼容的更改。您可以在文档的发行说明 here.
中阅读相关内容
基本上,从 7.45 开始,“可执行模型”的引入导致 mvel 支持被分解为一个单独的模块。因此,您现在需要包含以下依赖项之一:
- drools-mvel-编译器
- drools-模型编译器
您需要哪一个取决于您实际在做什么。在 7.45 之前,mvel 编译器支持是 drools-compiler 依赖项的一部分,因此不需要这种额外的依赖项。
(请注意,您不需要 kie-api
依赖项。)
我刚开始学习 Drools,但使用起来并不顺利,我正在尝试一个简单的项目,但出现此错误。
Exception in thread "main" java.lang.NoClassDefFoundError: org/kie/api/KieServices$Factory at com.sample.App.main(App.java:12)
Whosebug 和其他讨论的所有建议都没有帮助。有没有人有办法解决吗?当我使用 mvn clean install 是 BUILD SUCCESS
这是我的代码
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;
}
}
这是我的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>
我的 Rules.drl 在 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
我的kmodule.xml
<kmodule xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://jboss.org/kie/6.0.0/kmodule">
</kmodule>
解决方案
正如@RoddyoftheFrozenPeas 指出的那样,解决方案是添加 drools-mvel、drools-model-compiler 和 sfj4 的依赖项并起作用。这是给 Drools 7> 这是我的 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>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>
7.45 中引入的 Drools 库有一个非向后兼容的更改。您可以在文档的发行说明 here.
中阅读相关内容基本上,从 7.45 开始,“可执行模型”的引入导致 mvel 支持被分解为一个单独的模块。因此,您现在需要包含以下依赖项之一:
- drools-mvel-编译器
- drools-模型编译器
您需要哪一个取决于您实际在做什么。在 7.45 之前,mvel 编译器支持是 drools-compiler 依赖项的一部分,因此不需要这种额外的依赖项。
(请注意,您不需要 kie-api
依赖项。)