英文:
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)
<?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.
答案1
得分: 2
你必须在 pom.xml 中指定你的起始类。
在 properties 部分,加入以下行:
<start-class>#你的主类路径</start-class>
路径应为相对路径,即从你的包名开始。
英文:
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, i.e from the start of your package name.
答案2
得分: 1
这是您问题的根本原因:
引发原因:java.lang.NoClassDefFoundError: org/openqa/selenium/WebDriver
类加载器无法找到 WebDriver
类。
为什么会这样?
可能是因为包含该类的 JAR 文件未出现在运行时类路径上。
那么如何将它添加到类路径中?
有3种方法:
-
使用
java -cp ... com.aking.app.Application
命令,其中...
包含您所需的所有 JAR 文件。Oracle 文档 解释了您在命令行上表示类路径所需使用的语法。注意:在 Linux 和 Mac OS 上,类路径分隔符是
:
,而不是;
。 -
修改您的 JAR 文件,以在其 MANIFEST.MF 文件中包含
Class-Path
属性。 -
创建一个"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:
-
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;
. -
Modify your JAR file to include a
Class-Path
attribute in its MANIFEST.MF file. -
Create an "UberJAR" that contains an exploded copy of all dependent JARs.
Variations 2 and 3 can be done using Maven.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论