Java 无法从 Windows 上的 jar 加载主类清单属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18183782/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 23:05:21  来源:igfitidea点击:

Failed to load Main-Class manifest attribute from a jar on windows

javawindows

提问by Amira

I'm executing a jar in linux and it works fine here's the command :

我正在 linux 中执行一个 jar 并且它工作正常,这是命令:

 java -Djava.ext.dirs=/home/amira/workspace/srd/remap/libs fr.srd.remap.compiler.main.Main ~/Desktop/TDP/VQHFACCOI/VQHFACCOI.xml ~/Desktop/TDP/VQHFACCOI

So for a technical issues i need to run it on windows machine so here's the used command :

所以对于技术问题,我需要在 Windows 机器上运行它,所以这是使用的命令:

C:\Documents and Settings\walinbj\Desktop\remap-to-win>java -jar remap.jar -Djava.ext.dirs=Facture\libs fr.srd.remap.compiler.Main  C:\Documents and Settings\walinbj\Desktop\remap-to-win\Facture\TDPFAC010.xml

i'm getting this error Failed to load Main-Class manifest attribute from remap.jar

我收到这个错误 Failed to load Main-Class manifest attribute from remap.jar

采纳答案by Juned Ahsan

From the javadocs:

javadocs

Setting an Application's Entry Point

设置应用程序的入口点

If you have an application bundled in a JAR file, you need some way to indicate which class within the JAR file is your application's entry point. You provide this information with the Main-Class header in the manifest, which has the general form:

Main-Class: classname The value classname is the name of the class that is your application's entry point.

Recall that the entry point is a class having a method with signature public static void main(String[] args).

After you have set the Main-Class header in the manifest, you then run the JAR file using the following form of the java command:

java -jar JAR-name The main method of the class specified in the Main-Class header is executed.

如果您的应用程序捆绑在 JAR 文件中,您需要某种方式来指明 JAR 文件中的哪个类是您的应用程序的入口点。您在清单中的 Main-Class 标头中提供此信息,其具有一般形式:

Main-Class: classname 值 classname 是作为应用程序入口点的类的名称。

回想一下,入口点是一个具有签名 public static void main(String[] args) 的方法的类。

在清单中设置 Main-Class 标头后,然后使用以下形式的 java 命令运行 JAR 文件:

java -jar JAR-name 执行 Main-Class 头中指定的类的 main 方法。

You basically need to have a manifest file in your jar and in that you need to define your Main-Class attribute:

您基本上需要在 jar 中有一个清单文件,并且您需要定义 Main-Class 属性:

Manifest-Version: 1.0 Created-By: 1.7.0_06 (Oracle Corporation) Main-Class: fr.srd.remap.compiler.main.Main

清单版本:1.0 创建者:1.7.0_06(甲骨文公司)主类:fr.srd.remap.compiler.main.Main

When you run the JAR file with the following command, the main method of Main executes:

当您使用以下命令运行 JAR 文件时,Main 的 main 方法会执行:

java -jar MyJar.jar

java -jar MyJar.jar

回答by wolfroma

Your jar file remap.jar doesn't contain a MANIFEST file with provided main class option. Your need to specify main class as an argument (You did it for linux command line) and provide classpath to the jar. You can't use -jar option for that. Try to change to use following command:

您的 jar 文件 remap.jar 不包含具有提供的主类选项的清单文件。您需要将主类指定为参数(您是为 linux 命令行执行的)并为 jar 提供类路径。您不能为此使用 -jar 选项。尝试更改为使用以下命令:

C:\Documents and Settings\walinbj\Desktop\remap-to-win>java -classpath remap.jar -Djava.ext.dirs=Facture\libs fr.srd.remap.compiler.Main  C:\Documents and Settings\walinbj\Desktop\remap-to-win\Facture\TDPFAC010.xml