0

我有这些文件board.classx4.class(x4.class 有 main() 方法)。

为了 jar 这些文件,我写了

jar cf x4.jar *.class

并得到一个x4.jar文件。

我将此 x4.jar 文件复制到我的桌面(在 Windows Vista 上)并双击它。我收到此错误:

无法从 C:\Users\eSKay\Desktop\x4.jar

我应该怎么做才能使这个文件作为 jar 可执行文件运行(不安装任何软件)?


更新: 我使用清单文件来解决问题。我有我需要的 jar 文件,如果你这样做,它运行良好:

java -jar x4.jar

但是,当我双击 x4.jar 时没有任何反应,我检查了任务管理器,发现后台正在启动一个 javaw.exe,但它没有显示原始程序给出的输出。

问题可能是什么?

4

2 回答 2

5

您需要创建一个清单文件,其中包含指定其入口点Main-Class的属性。然后使用 jar 命令中的“m”标志来指定它。例如,您可能有一个名为 manifest.txt 的文件:

Manifest-Version: 1.0 
Main-Class: x4    

请注意,您需要在文件末尾有一个空行,否则该jar工具将无法正确处理它,而忽略最后一行

然后运行:

jar cfm x4.jar manifest.txt *.class

要对其进行测试,请运行:

java -jar x4.jar
于 2009-10-06T18:54:53.987 回答
2

我认为@Jon 是正确的,只要确保以 CR/LF 结束文件即可。

设置应用程序的入口点

警告:文本文件必须以换行符或回车符结尾。如果最后一行没有以新行或回车结束,则不会正确解析。

或者你可以让 jar 程序自动为你创建 Main-Class 属性。

The 'e' flag (for 'entrypoint'), introduced in JDK 6, creates or overrides the manifest's Main-Class attribute. It can be used while creating or updating a jar file. 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:

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:

jar cfe Main.jar foo.Main foo/Main.class
于 2009-10-06T19:52:48.303 回答