创建可执行 Java Jar 文件时遇到问题

发布于 2024-09-14 05:45:11 字数 1043 浏览 0 评论 0原文

我有以下 Java 代码。它做了它应该做的事情,但我在创建 jar 文件时遇到问题。

import java.io.*;
public class openfile{
    public static void main(String argv[]) {
        try {
                String line;
                Process p = Runtime.getRuntime().exec
                ("c:\\Users\\user\\Desktop\\"+ "shares.bat /A");
                 BufferedReader input =
                 new BufferedReader
                 (new InputStreamReader(p.getInputStream()));
                 input.close();
                }
           catch (Exception err) {
                  err.printStackTrace();
               }
    }
}

编译没有问题,使用java openfile运行也没有问题。当我尝试使用以下命令创建 jar 文件时,就会出现问题:

  jar cf MyJar.jar manifest.txt openfile.java openfile.class

但是,当我尝试使用以下命令运行 jar 时,

java -jar MyJar.jar

出现以下错误消息:

Failed to load Main-Class manifest attribute from MyJar.jar

manifest.txt 的文本如下:

Main-Class: openfile 

知道我做错了什么吗?

I have the following Java code. It does what it is meant to do, but I am having problems creating a jar file.

import java.io.*;
public class openfile{
    public static void main(String argv[]) {
        try {
                String line;
                Process p = Runtime.getRuntime().exec
                ("c:\\Users\\user\\Desktop\\"+ "shares.bat /A");
                 BufferedReader input =
                 new BufferedReader
                 (new InputStreamReader(p.getInputStream()));
                 input.close();
                }
           catch (Exception err) {
                  err.printStackTrace();
               }
    }
}

It compiles no problem, It runs no problem when using java openfile. The problem arises when I try to create a jar file using the following commands:

  jar cf MyJar.jar manifest.txt openfile.java openfile.class

However when I try to run the jar using

java -jar MyJar.jar

I get the following error message:

Failed to load Main-Class manifest attribute from MyJar.jar

The text of manifest.txt is as follows:

Main-Class: openfile 

Any idea what I am doing wrong?

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

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

发布评论

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

评论(3

那些过往 2024-09-21 05:45:11

我遇到了我相信的问题。

在您的manifest.txt文件中

manifest.txt的文本如下:

Main-Class: openfile 

您需要在最后一行后提供换行符

因此只需在此行后键入Enter (<-)(回车符)即可。

Main-Class: openfile 

请参阅 Sun 文档。这是摘录。

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

我用这个验证了并且它有效。
希望这有帮助。

I got the problem i believe.

In your manifest.txt file

The text of manifest.txt is as follows:

Main-Class: openfile 

You need to provide a line break after last line

So just type Enter (<-) (carriage return) after this line.

Main-Class: openfile 

Refer to this sun documentation. here is an excerpt.

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.

I verified with this and it works.
Hope this helps .

情仇皆在手 2024-09-21 05:45:11

假设您有一个声明了 main 方法的 openfile 类,那么这应该可以工作:

jar cvef MyJar.jar openfile openfile.class

如果您已经有一个清单并想要包含它,请尝试以下操作:

jar cvmf MyJar.jar manifest.txt openfile.class

Assuming, you have a class openfile with a main method declared, then this should work:

jar cvef MyJar.jar openfile openfile.class

If you already have a manifest and want to include it, then try this:

jar cvmf MyJar.jar manifest.txt openfile.class
此刻的回忆 2024-09-21 05:45:11

您的清单文件的内容必须是这样的:

清单版本:1.0
主类:openfile

在这两行之后必须保留一个空行,因为不保留它可能会产生问题。

The Content of your Manifest File must be this:

Manifest-Version: 1.0
Main-Class: openfile

A blank line must be left after these two lines as not leaving it might create problem.

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