我想将jar文件作为独立应用程序执行。当我运行以下命令时,我收到此错误消息:
[rcbandit@Laptop target]$ /opt/jdk1.8.0/bin/java -jar DX57DC-1.0.jar
no main manifest attribute, in DX57DC-1.0.jar
[rcbandit@Laptop target]$
这是POM配置:
<?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>
<groupId>com.dx57dc</groupId>
<artifactId>DX57DC</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>DX57DC</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<mainClass></mainClass>
</properties>
<organization>
<name>Corporation Name</name>
</organization>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.8</version>
<executions>
<execution>
<id>unpack-dependencies</id>
<phase>package</phase>
<goals>
<goal>unpack-dependencies</goal>
</goals>
<configuration>
<excludeScope>system</excludeScope>
<excludeGroupIds>junit,org.mockito,org.hamcrest</excludeGroupIds>
<outputDirectory>${project.build.directory}/classes</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.zenjava</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>2.0</version>
<configuration>
<mainClass>com.dx57dc.main.DX57DC</mainClass>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
我想我错过了一个maven插件。你能告诉我怎么解决这个问题吗?
答案 0 :(得分:2)
你的jar不包含指定可执行main()函数的Manifest,因此java不知道要执行/启动哪个类。
使用maven时,请查看程序集插件。这使您可以使用正确的Manifest创建jar。
或者只需使用以下代码启动您的程序:java -cp DX57DC-1.0.jar 'your_main_class_here'
问候,
麦克
答案 1 :(得分:2)
按如下方式创建jar:
输入以下命令:
jar cfev YourJarName.jar EntryClass *
或者,如果您的课程在某个包中,那么,
转到您的包文件夹外并运行以下命令:
jar cfev YourJarName.jar YourPackage.EntryClass YourPackage/*
这将创建一个jar文件。 现在,如果Double Clicking不打开jar,那么
在终端或Cmd中找到保存jar文件的目录,然后运行以下命令:
java -jar YourJarName.jar args
这里的选项是:
-c create new archive
-f specify archive file name
-e specify application entry point for stand-alone application
bundled into an executable jar file
-v generate verbose output on standard output
希望这会有帮助。
答案 2 :(得分:1)
尝试将这样的内容添加到插件中:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<goals>
<goal>attached</goal>
</goals>
<phase>package</phase>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.package.to.my.Main</mainClass>
</manifest>
</archive>
</configuration>
</execution>
</executions>
</plugin>
答案 3 :(得分:1)
为了使JAR文件为executable,它需要一个包含Main-Class
和Class-Path
条目的清单文件:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>my.package.App</mainClass>
</manifest>
</archive>
<executions>
<execution>
<id>default-package</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</configuration>
</plugin>
这会在jar文件的根目录下的MANIFEST.MF
目录下生成一个META-INF
文件。仅列出相关条目:
Class-Path: lib/somejar.jar
Main-Class: my.package.App
Class-Path
表示jar文件所在的目录中存在lib
文件夹,其中包含文件somejar.jar
。
Main-Class
表示文件App.class
存在于包my.package
及其main
方法中。
如果lib
文件夹不存在,则在加载第一个依赖类时执行将失败。为了逃避这一点,您可以pack all the dependencies in your jar with the shade插件:
<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>my.package.App</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
由于所有依赖项都打包在一起,因此不再需要Class-Path
条目。