当我尝试 运行 我刚创建的 jar 文件时 Maven 错误 "Unable to initialize main class"
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
Error: Unable to initialize main class com.aking.app.Application
Caused by: java.lang.NoClassDefFoundError: org/openqa/selenium/WebDriver
我不确定如何解决这个问题。这是显示重要文件所在位置的文件结构(为了打字而组合包):
- 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
我的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.
您必须在 pom.xml 中指定您的起点 class。
在属性部分,包括以下行
<start-class>#path to your main class</start-class>
路径应该是相对的,即从你的包名开始。
这是您问题的根本原因:
Caused by: java.lang.NoClassDefFoundError: org/openqa/selenium/WebDriver
class 加载程序找不到 WebDriver
class。
为什么?
可能是因为包含 class 的 JAR 文件不在 runtime classpath.
那么如何在 class 路径上获取它呢?
有3种方式:
使用 java -cp ... com.aking.app.Application
,其中 ...
包含您需要的所有 JAR 文件。 Oracle documentation 解释了在命令行中表达 class 路径所需的语法。
注意:在 Linux 和 Mac OS 上,class 路径分隔符是 :
而不是 ;
.
修改您的 JAR 文件以在其 MANIFEST.MF 文件中包含一个 Class-Path
属性。
创建一个 "UberJAR" 包含所有相关 JAR 的分解副本。
变体 2 和 3 可以使用 Maven 完成。
我在命令行中使用 Maven 创建了一个 .jar 文件。它创建了 .jar 文件。当我尝试在命令行中 运行 它时,我得到了这个错误:
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
我不确定如何解决这个问题。这是显示重要文件所在位置的文件结构(为了打字而组合包):
- 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
我的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.
您必须在 pom.xml 中指定您的起点 class。 在属性部分,包括以下行
<start-class>#path to your main class</start-class>
路径应该是相对的,即从你的包名开始。
这是您问题的根本原因:
Caused by: java.lang.NoClassDefFoundError: org/openqa/selenium/WebDriver
class 加载程序找不到 WebDriver
class。
为什么?
可能是因为包含 class 的 JAR 文件不在 runtime classpath.
那么如何在 class 路径上获取它呢?
有3种方式:
使用
java -cp ... com.aking.app.Application
,其中...
包含您需要的所有 JAR 文件。 Oracle documentation 解释了在命令行中表达 class 路径所需的语法。注意:在 Linux 和 Mac OS 上,class 路径分隔符是
:
而不是;
.修改您的 JAR 文件以在其 MANIFEST.MF 文件中包含一个
Class-Path
属性。创建一个 "UberJAR" 包含所有相关 JAR 的分解副本。
变体 2 和 3 可以使用 Maven 完成。