如何修复&Quot;NoClassDefFoundError KieServices.Factory&Quot;

2022-04-14 00:00:00 java maven drools

我刚刚开始学习JBoss Drools。因此,我从一本名为Drools JBoss Rules 5.X的书中创建了一个小应用程序。正如我很快注意到的那样,Drools 5.X的初始化被取消了,我使用KieServices将代码重写为Drools 6.2+。但是运行JAR时,我总是得到NoClassDefFoundError:org/kie/api/KieServices$Factory。然后,我从一个代码开始,其中没有任何内容,只是初始化了KieServices,得到了同样的错误。所有关于Stackoverflow的建议和其他讨论都没有帮助。请看下面的代码(几乎什么都没有)和maven文件。有谁有解决方案吗?

初始化KieServices的主类

import org.kie.api.KieServices;

public class App {
    public static void main(String[] args) {
        System.out.println("start");
        KieServices ks = KieServices.Factory.get();
    }
}

这是我的Maven-项目

<properties>
    <drools.version>6.3.0.Final</drools.version>
    <java.version>1.8</java.version>
</properties>


<dependencies>
    <dependency>
        <groupId>org.drools</groupId>
        <artifactId>drools-core</artifactId>
        <version>${drools.version}</version>
    </dependency>


    <dependency>
        <groupId>org.drools</groupId>
        <artifactId>drools-compiler</artifactId>
        <version>${drools.version}</version>
    </dependency>

    <dependency>
        <groupId>org.kie</groupId>
        <artifactId>kie-api</artifactId>
        <version>${drools.version}</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.kie.server/kie-server-api -->
    <dependency>
        <groupId>org.kie.server</groupId>
        <artifactId>kie-server-api</artifactId>
        <version>${drools.version}</version>
    </dependency>

</dependencies>

<build>
    <plugins>

        <plugin>
            <groupId>org.kie</groupId>
            <artifactId>kie-maven-plugin</artifactId>
            <version>${drools.version}</version>
            <extensions>true</extensions>
        </plugin>

        <plugin>
            <!-- Build an executable JAR -->
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.1.1</version>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>App</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>


解决方案

最后,我添加了一个Maven Shade插件来创建一个Uber jar。我保证,所有类在运行时都在那里。它起作用了。也许,有更好的解决方案,那么请留下答案。Maven项目现在如下所示:

<properties>
    <drools.version>6.3.0.Final</drools.version>
    <java.version>1.8</java.version>
</properties>


<dependencies>

    <dependency>
        <groupId>org.drools</groupId>
        <artifactId>drools-core</artifactId>
        <version>${drools.version}</version>
    </dependency>


    <dependency>

也许这对其他人有帮助。

        <groupId>org.drools</groupId>
        <artifactId>drools-compiler</artifactId>
        <version>${drools.version}</version>
    </dependency>

    <dependency>
        <groupId>org.kie</groupId>
        <artifactId>kie-api</artifactId>
        <version>${drools.version}</version>
    </dependency>

    <dependency>
        <groupId>commons-lang</groupId>
        <artifactId>commons-lang</artifactId>
        <version>2.6</version>
    </dependency>

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.21</version>
    </dependency>

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-simple</artifactId>
        <version>1.7.21</version>
    </dependency>

</dependencies>

<build>
    <plugins>

        <plugin>
            <groupId>org.kie</groupId>
            <artifactId>kie-maven-plugin</artifactId>
            <version>${drools.version}</version>
            <extensions>true</extensions>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>1.6</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>


        <plugin>
            <!-- Build an executable JAR -->
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.1.1</version>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>App</mainClass>
                        <addClasspath>true</addClasspath>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>

相关文章