简体   繁体   中英

Maven error “Unable to initialize main class” when I try to run a jar file I just created

I created a.jar file using Maven in the command line. It created the.jar file. When I tried to run it in the command line I got this error:

java -jar target\github-automation-1.0.0.jar
Error: Unable to initialize main class com.aking.app.Application
Caused by: java.lang.NoClassDefFoundError: org/openqa/selenium/WebDriver

I am not sure how to fix this issue. This is the file structure showing where important files are located (package is combined for the sake of typing):

- githubautomation (root directory)
    - pom.xml
    - src
        - main
            - java
                - com.aking.app
                    - Applicatoin.java

    - target
        - classes
            - com.aking.app
                - Application.class
        - github-automation-1.0.0.jar

My pom.xml (Edit: added suggested code)

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

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
        <maven.compiler.release>11</maven.compiler.release>
        <start-class>com.aking.app.Application</start-class>
    </properties>

    <groupId>com.aking.app</groupId>
    <artifactId>github-automation</artifactId>
    <version>1.0.0</version>
    <dependencies>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.141.59</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.2.0</version>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>com.aking.app.Application</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

I also read something that there needs to be a main manifest attribute, but I'm not sure. I want to be able to have people run my program through the jar without relying on them to have an IDE.

You have to specify your start class in pom.xml. In the properties section, include the below line

 <start-class>#path to your main class</start-class>

The path should be relative, ie from the start of your package name.

This is the root cause of your problem:

Caused by: java.lang.NoClassDefFoundError: org/openqa/selenium/WebDriver

The class loader cannot find the WebDriver class.

Why?

Probably because the JAR file containing the class is not on the runtime classpath.

So how do you get it on the classpath?

There are 3 ways:

  1. Use java -cp... com.aking.app.Application where the ... has all of the JAR files that you need. The Oracle documentation explains the syntax you need to use to express the classpath on the command line.

    Note: on Linux and Mac OS, the classpath separator is : not ; .

  2. Modify your JAR file to include a Class-Path attribute in its MANIFEST.MF file.

  3. Create an "UberJAR" that contains an exploded copy of all dependent JARs.

Variations 2 and 3 can be done using Maven.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM