无法在可部署的 .jar 中加载主类
Posted
技术标签:
【中文标题】无法在可部署的 .jar 中加载主类【英文标题】:Unable to load main class in deployable .jar 【发布时间】:2013-10-17 08:52:15 【问题描述】:我正在尝试构建一个可执行 jar 并收到错误 Error: Could not find or load main class com.company.app.Main
。我已经重新启动了我的电脑并尝试清理这个项目。我也尝试过使用<addClasspath>true</addClasspath>
,但没有成功...
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.company.app.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
【问题讨论】:
我不确定,但也许 maven-jar-plugin 覆盖了 maven 程序集插件的配置。此外,maven-assembly-plugin 在打包阶段将“jar-with-dependencies”附加到生成的工件。只有那个工件是可执行的。 maven-shade-plugin 不这样做。请参阅下面我的答案中的一个工作示例。 【参考方案1】:$ tree executable-jar-with-dependencies/
executable-jar-with-dependencies/
|-- pom.xml
`-- src
`-- main
`-- java
`-- com
`-- stackexchange
`-- ***
`-- ExecutableJar.java
6 directories, 2 files
pom.xml:
<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>com.***</groupId>
<artifactId>question-19281476</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<!--
I prefer to use the maven-shade-plugin because the final built artifact maintains the original name
instead of appending "jar-with-dependencies" to the built artifact. But the maven-assembly-plugin works fine too.
-->
<!--
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals><goal>shade</goal></goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.stackexchange.***.ExecutableJar</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.stackexchange.***.ExecutableJar</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
ExecutableJar.java:
package com.stackexchange.***;
public class ExecutableJar
public static void main(String[] args)
System.out.println("Hello World");
现在从命令行执行以下命令可以证明这一点:
$ mvn clean package
$ java -jar target/question-19281476-1.0-SNAPSHOT-jar-with-dependencies.jar
Hello World
【讨论】:
【参考方案2】:maven-shade-plugin 是迄今为止获取胖可执行 jar 的最简单解决方案。
【讨论】:
以上是关于无法在可部署的 .jar 中加载主类的主要内容,如果未能解决你的问题,请参考以下文章
Maven 和 Eclipse:在 Maven 库项目中加载默认属性并在可运行的 Jar 中使用它
eclipse中启动tomcat出现错误: 找不到或无法加载主类 org.apache.tomcat.startup.Main
无法在web.xml或使用此应用程序部署的jar文件中解析绝对uri:[http://java.sun.com/jsp/jstl/core]