创建可执行 Java Jar 文件时遇到问题
我有以下 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我遇到了我相信的问题。
在您的manifest.txt文件中
manifest.txt的文本如下:
您需要在最后一行后提供换行符
因此只需在此行后键入Enter (<-)(回车符)即可。
请参阅此 Sun 文档。这是摘录。
我用这个验证了并且它有效。
希望这有帮助。
I got the problem i believe.
In your manifest.txt file
The text of manifest.txt is as follows:
You need to provide a line break after last line
So just type Enter (<-) (carriage return) after this line.
Refer to this sun documentation. here is an excerpt.
I verified with this and it works.
Hope this helps .
假设您有一个声明了 main 方法的 openfile 类,那么这应该可以工作:
如果您已经有一个清单并想要包含它,请尝试以下操作:
Assuming, you have a class openfile with a main method declared, then this should work:
If you already have a manifest and want to include it, then try this:
您的清单文件的内容必须是这样的:
清单版本: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.