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

发布于 2024-08-06 08:25:23 字数 643 浏览 2 评论 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 正在后台启动,但它是没有显示原始程序给出的输出。

问题可能是什么?

I have these files board.class and x4.class (x4.class has main() method).

To jar these files, I wrote

jar cf x4.jar *.class

and got a x4.jar file.

I copied this x4.jar file to my Desktop (on windows Vista) and double-clicked it. I am getting this error:

Failed to load Main-Class manifest
attribute from
C:\Users\eSKay\Desktop\x4.jar

What should I do to make this file run as a jar executable (without installing any software)?


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:

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.

What can the problem be?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柠栀 2024-08-13 08:25:23

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

Manifest-Version: 1.0 
Main-Class: x4    

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

然后运行:

jar cfm x4.jar manifest.txt *.class

要测试它,请运行:

java -jar x4.jar

You need to create a manifest file which contains the Main-Class attribute to specify its entry point. Then use the "m" flag in the jar command to specify it. For example, you might have a file called 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.

Then run:

jar cfm x4.jar manifest.txt *.class

To test it, run:

java -jar x4.jar
如梦初醒的夏天 2024-08-13 08:25:23

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

设置应用程序的入口点

警告:文本文件必须以
新行或回车。最后一个
如果该行将无法正确解析
不以新行结束或
回车。

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

“e”标志(代表“入口点”),
JDK 6 中引入,创建或
覆盖清单的主类
属性。可以同时使用它
创建或更新 jar 文件。使用
它指定应用程序入口
点而无需编辑或创建
清单文件。例如,这个
命令创建 app.jar 其中
Main-Class 属性值
清单设置为 MyApp:

jar cfe app.jar MyApp MyApp.class

你可以直接调用这个
通过运行以下命令来应用程序
命令:

java -jar app.jar

如果入口点类名位于
包可能会使用“.” (点)
字符作为分隔符。为了
例如,如果 Main.class 在包中
名为 foo 的入口点可以是
通过以下方式指定:

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

I think @Jon is correct, just make sure you end the file with a 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.

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文