Search code examples
javamavendrools

How to fix "NoClassDefFoundError KieServices.Factory"


I am just starting learning JBoss Drools. So I have made a little application out from a book, Drools Jboss Rules 5.X. As I noticed soon, the initialisation of Drools 5.X is depricated and I rewrote the Code to Drools 6.2+ using the KieServices. Building the project finished without error.But running the jar I always got the NoClassDefFoundError: org/kie/api/KieServices$Factory. Then I began at the very beginning with a code, where nothing is in, just initialising the KieServices and got the same error. All suggestions of Stackoverflow and other discussion did not help. See below the code (nearly nothing) and the maven-file. Does anyone have a solution?

Main Class with initialisation of KieServices

import org.kie.api.KieServices;

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

And here is my Maven-project

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


Solution

  • Finally I added a Maven Shade Plugin to create an Uber jar. I guarantees, that all classes are there at runtime. It works. Maybe, there is a better solution, then please leave an answer. The maven project looks now like this:

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

    Maybe it helps some others.

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