2

I am trying to create an executable jar file for my maven cucumber project. The main class that the jar is trying to run:

public class ULPTestRunner {
    public static void main(String[] args) throws Throwable {
        System.out.println("Jar Testing");
        io.cucumber.core.cli.Main.main(
                new String[]{
                        "features",
                        "-g", "stepImplementations"

                }
        );
    }

When I run the class independently it is successful. However, when I run the jar file from target using the command : java -jar Test-maven-1.0-SNAPSHOT.jar The first line gets executed then I get the following error when Jar tries to run the cucumber main class:

Jar Testing

**Exception in thread "main" java.lang.NoClassDefFoundError: io/cucumber/core/cli/Main**
        at cucumberTests.ULPTestRunner.main(ULPTestRunner.java:14)
Caused by: java.lang.ClassNotFoundException: io.cucumber.core.cli.Main
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 1 more

Here is the structure of my project

enter image description here

pom.xml file:

<?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>Test-maven</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-chrome-driver</artifactId>
            <version>4.0.0-alpha-5</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-support -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-support</artifactId>
            <version>4.0.0-alpha-7</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>30.1.1-jre</version>
        </dependency>
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-java</artifactId>
            <version>5.7.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-junit -->
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-junit</artifactId>
            <version>6.10.4</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-expressions -->
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-expressions</artifactId>
            <version>3.0.0</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.1</version>
        </dependency>
    </dependencies>
    <build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M5</version>
                <configuration>
                    <testSourceDirectory>Test-maven/src/main</testSourceDirectory>
                <includes>
                    <include>**/*ULPTestRunner.java</include>
                </includes>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifestFile>src/main/resources/META-INF/MANIFEST.MF</manifestFile>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
    </build>
</project>
  • You need to build a so called fat jar with all the dependencies included, see https://stackoverflow.com/questions/16222748/building-a-fat-jar-using-maven. – CrazyCoder Jul 15 '21 at 15:46
  • Thank you @CrazyCoder that worked for me! I will post my answer below – Nouha Nouman Jul 18 '21 at 05:28
  • Does this answer your question? [Why am I getting a NoClassDefFoundError in Java?](https://stackoverflow.com/questions/34413/why-am-i-getting-a-noclassdeffounderror-in-java) – Danubian Sailor May 27 '22 at 12:24

0 Answers0