简体   繁体   English

无法在Windows上运行我的jar文件

[英]Not able to run my jar file on Windows

I have these files board.class and x4.class (x4.class has main() method). 我有这些文件board.classx4.class (x4.class具有main()方法)。

To jar these files, I wrote 要打包这些文件,我写道

jar cf x4.jar *.class

and got a x4.jar file. 并得到一个x4.jar文件。

I copied this x4.jar file to my Desktop (on windows Vista) and double-clicked it. 我将此x4.jar文件复制到了我的桌面(在Windows Vista上)并双击了它。 I am getting this error: 我收到此错误:

Failed to load Main-Class manifest attribute from C:\\Users\\eSKay\\Desktop\\x4.jar 无法从C:\\Users\\eSKay\\Desktop\\x4.jar加载Main-Class清单属性

What should I do to make this file run as a jar executable (without installing any software)? 如何使该文件作为jar可执行文件运行(无需安装任何软件)?


UPDATE: I used a manifest file to fix the problem. 更新:我使用清单文件来解决此问题。 I have got the jar file I needeed and it is running fine if you do: 我已经收到需要的jar文件,如果您执行的话,它运行良好:

java -jar x4.jar

But, when I double click x4.jar nothing happens, I checked Task Manager and found that a javaw.exe is being started in the background, but it is not showing the output the original program was giving. 但是,当我双击x4.jar时,没有任何反应,我检查了任务管理器,发现javaw.exe在后台启动,但未显示原始程序给出的输出。

What can the problem be? 可能是什么问题?

You need to create a manifest file which contains the Main-Class attribute to specify its entry point . 您需要创建一个清单文件 ,其中包含Main-Class属性以指定其入口点 Then use the "m" flag in the jar command to specify it. 然后在jar命令中使用“ m”标志来指定它。 For example, you might have a file called manifest.txt: 例如,您可能有一个名为manifest.txt的文件:

Manifest-Version: 1.0 
Main-Class: x4    

Note that you need to have an empty line at the end of the file, or the jar tool won't process it properly, ignoring the final line silently . 请注意,您需要在文件末尾有一个空行,否则jar工具将无法正确处理它,从而无声地忽略了最后一行。

Then run: 然后运行:

jar cfm x4.jar manifest.txt *.class

To test it, run: 要测试它,请运行:

java -jar x4.jar

I think @Jon is correct, just make sure you end the file with a CR/LF. 我认为@Jon是正确的,只需确保以CR / LF结尾文件即可。

Setting an Application's Entry Point 设置应用程序的入口点

Warning: The text file must end with a new line or carriage return. 警告:文本文件必须以换行符或回车符结尾。 The last line will not be parsed properly if it does not end with a new line or carriage return. 如果最后一行未以新行或回车结尾,则将无法正确解析。

Or you can let the jar program automatically create the Main-Class attribute for you. 或者,您可以让jar程序自动为您创建Main-Class属性。

The 'e' flag (for 'entrypoint'), introduced in JDK 6, creates or overrides the manifest's Main-Class attribute. JDK 6中引入的“ e”标志(用于“ entrypoint”)用于创建或覆盖清单的Main-Class属性。 It can be used while creating or updating a jar file. 它可以在创建或更新jar文件时使用。 Use it to specify the application entry point without editing or creating the manifest file. 使用它可以指定应用程序入口点,而无需编辑或创建清单文件。 For example, this command creates app.jar where the Main-Class attribute value in the manifest is set to MyApp: 例如,此命令创建app.jar,其中清单中的Main-Class属性值设置为MyApp:

 jar cfe app.jar MyApp MyApp.class 

You can directly invoke this application by running the following command: 您可以通过运行以下命令直接调用此应用程序:

 java -jar app.jar 

If the entrypoint class name is in a package it may use a '.' 如果入口点类名称在包中,则可以使用“。”。 (dot) character as the delimiter. (点)字符作为分隔符。 For example, if Main.class is in a package called foo the entry point can be specified in the following ways: 例如,如果Main.class位于名为foo的软件包中,则可以通过以下方式指定入口点:

 jar cfe Main.jar foo.Main foo/Main.class 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM