无法执行jar文件:“无主清单属性” [英] Can't execute jar- file: "no main manifest attribute"
问题描述
我已经安装了一个应用程序,当我尝试运行它时(它是一个可执行的jar)没有任何反应。当我从命令行运行它时:
I have installed an application, when I try to run it (it's an executable jar) nothing happens. When I run it from the commandline with:
java -jarapp.jar
java -jar "app.jar"
我收到以下消息:
在app.jar中没有主要清单属性
no main manifest attribute, in "app.jar"
通常情况下,如果我自己创建了程序,我会在清单文件中添加一个主类属性。但在这种情况下,由于文件来自应用程序,我不能这样做。我也尝试提取jar以查看是否可以找到主类,但是有许多类,并且它们中没有一个在其名称中包含main一词。必须有办法解决这个问题,因为该程序在其他系统上运行良好。
Normally, if I had created the program myself, I would have added a main class attribute to the manifest file. But in this case, since the file is from an application, i cannot do that. I also tried extracting the jar to see if I could find the main class, but there are to many classes and none of them has the word "main" in it's name. There must be a way to fix this because the program runs fine on other systems.
推荐答案
首先,它有点奇怪,看你运行 java -jarapp
而不是 java -jar app.jar
First, it's kind of weird, to see you run java -jar "app"
and not java -jar app.jar
其次,要制作一个jar可执行文件...你需要jar一个名为META-INF / MANIFEST.MF的文件
Second, to make a jar executable... you need to jar a file called META-INF/MANIFEST.MF
文件本身应该(至少)这一个班轮:
the file itself should have (at least) this one liner:
Main-Class: com.mypackage.MyClass
其中 com.mypackage.MyClass
是持有 public的班级static void main(String [] args)入口点。
请注意,有几种方法可以使用CLI,Maven,Ant或Gradle:
Note that there are several ways to get this done either with the CLI, Maven, Ant or Gradle:
对于 CLI ,以下命令将执行:(tks @ dvvrt )
jar cmvf META-INF / MANIFEST.MF< new-jar-filename> .jar<要包含的文件& GT;
For CLI, the following command will do: (tks @dvvrt)
jar cmvf META-INF/MANIFEST.MF <new-jar-filename>.jar <files to include>
对于 Maven ,类似以下代码段的内容应该可以解决问题。请注意,这只是插件定义,而不是完整的 pom.xml :
For Maven, something like the following snippet should do the trick. Note that this is only the plugin definition, not the full pom.xml:
<build>
<plugins>
<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.mypackage.MyClass</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
(选择 < version>
适用于您的项目。)
(Pick a <version>
appropriate to your project.)
对于 Ant ,下面的代码段应该有所帮助:
For Ant, the snippet below should help:
<jar destfile="build/main/checksites.jar">
<fileset dir="build/main/classes"/>
<zipfileset includes="**/*.class" src="lib/main/some.jar"/>
<manifest>
<attribute name="Main-Class" value="com.acme.checksites.Main"/>
</manifest>
</jar>
积分Michael Niemand -
Credits Michael Niemand -
For Gradle :
plugins {
id 'java'
}
jar {
manifest {
attributes(
'Main-Class': 'com.mypackage.MyClass'
)
}
}
这篇关于无法执行jar文件:“无主清单属性”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!