我尝试使用Eclipse Maven构建具有依赖项的jar,但是当我执行时
java -jar App.jar
得到了错误:“未找到主要方法”
我的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.maventest</groupId> <artifactId>aproject</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>aproject</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project>
这只是一个简单的Java代码:
package com.maventest.aproject; public class App { public static void main( String[] args ) { System.out.println( "Hello World!" ); } }
蚀版本:4.5.2
谁能帮我这个?谢谢。
您可以为此目的使用maven-jar-plugin。
<build> <plugins> <plugin> <!-- Build an executable JAR --> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>${maven-jar-plugin.version}</version> <configuration> <archive> <manifest> <!-- <addClasspath>true</addClasspath> --> <mainClass>twaren.elk.elkMaven.App</mainClass> </manifest> </archive> </configuration> </plugin> </plugins> </build>