Maven错误:“无法初始化主类”,当我尝试运行刚刚创建的jar文件时。

huangapple go评论101阅读模式
英文:

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

问题

我在命令行中使用Maven创建了一个.jar文件。它创建了该.jar文件。当我尝试在命令行中运行它时,出现了以下错误:

java -jar target\github-automation-1.0.0.jar
错误:无法初始化主类com.aking.app.Application
原因:java.lang.NoClassDefFoundError: org/openqa/selenium/WebDriver

我不确定如何解决这个问题。以下是显示重要文件位置的文件结构(为了方便输入,包已合并):

- githubautomation(根目录)
    - pom.xml
    - src
        - main
            - java
                - com.aking.app
                    - Applicatoin.java

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

我的pom.xml(编辑:添加建议的代码)

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

我还读到需要有一个主清单属性,但我不确定。我希望人们能够通过jar运行我的程序,而不需要依赖于IDE。

英文:

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)

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot;
         xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
         xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;

    &lt;properties&gt;
        &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
        &lt;project.reporting.outputEncoding&gt;UTF-8&lt;/project.reporting.outputEncoding&gt;
        &lt;maven.compiler.source&gt;11&lt;/maven.compiler.source&gt;
        &lt;maven.compiler.target&gt;11&lt;/maven.compiler.target&gt;
        &lt;maven.compiler.release&gt;11&lt;/maven.compiler.release&gt;
        &lt;start-class&gt;com.aking.app.Application&lt;/start-class&gt;
    &lt;/properties&gt;

    &lt;groupId&gt;com.aking.app&lt;/groupId&gt;
    &lt;artifactId&gt;github-automation&lt;/artifactId&gt;
    &lt;version&gt;1.0.0&lt;/version&gt;
    &lt;dependencies&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.seleniumhq.selenium&lt;/groupId&gt;
            &lt;artifactId&gt;selenium-java&lt;/artifactId&gt;
            &lt;version&gt;3.141.59&lt;/version&gt;
        &lt;/dependency&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;junit&lt;/groupId&gt;
            &lt;artifactId&gt;junit&lt;/artifactId&gt;
            &lt;version&gt;4.12&lt;/version&gt;
            &lt;scope&gt;test&lt;/scope&gt;
        &lt;/dependency&gt;
    &lt;/dependencies&gt;

    &lt;build&gt;
        &lt;plugins&gt;
            &lt;plugin&gt;
                &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                &lt;artifactId&gt;maven-jar-plugin&lt;/artifactId&gt;
                &lt;version&gt;3.2.0&lt;/version&gt;
                &lt;configuration&gt;
                    &lt;archive&gt;
                        &lt;manifest&gt;
                            &lt;mainClass&gt;com.aking.app.Application&lt;/mainClass&gt;
                        &lt;/manifest&gt;
                    &lt;/archive&gt;
                &lt;/configuration&gt;
            &lt;/plugin&gt;
        &lt;/plugins&gt;
    &lt;/build&gt;
&lt;/project&gt;

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.

答案1

得分: 2

你必须在 pom.xml 中指定你的起始类。
在 properties 部分,加入以下行:

 &lt;start-class&gt;#你的主类路径&lt;/start-class&gt;

路径应为相对路径,即从你的包名开始。

英文:

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

 &lt;start-class&gt;#path to your main class&lt;/start-class&gt;

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

答案2

得分: 1

这是您问题的根本原因:

引发原因:java.lang.NoClassDefFoundError: org/openqa/selenium/WebDriver

类加载器无法找到 WebDriver 类。

为什么会这样?

可能是因为包含该类的 JAR 文件未出现在运行时类路径上。

那么如何将它添加到类路径中?

有3种方法:

  1. 使用 java -cp ... com.aking.app.Application 命令,其中 ... 包含您所需的所有 JAR 文件。Oracle 文档 解释了您在命令行上表示类路径所需使用的语法。

    注意:在 Linux 和 Mac OS 上,类路径分隔符是 :,而不是 ;

  2. 修改您的 JAR 文件,以在其 MANIFEST.MF 文件中包含 Class-Path 属性。

  3. 创建一个"UberJAR",其中包含所有依赖 JAR 的解压副本。

变体 2 和 3 可以使用 Maven 完成。

英文:

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.

huangapple
  • 本文由 发表于 2020年5月3日 13:17:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/61569944.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定