如何运行JAR文件 [英] How to run a JAR file
问题描述
我创建了一个这样的JAR文件:
jar cf Predit.jar *。*
我通过双击它来运行这个JAR文件(它不起作用)。所以我从DOS提示符运行它如下:
java -jar Predit.jar
它引发了无法加载主类异常。所以我解压缩了这个JAR文件:
jar -xf Predit.jar
我运行了类文件:
java Predit
效果很好。我不知道为什么JAR文件不起作用。请告诉我运行JAR文件的步骤
您需要在jar文件清单中指定一个Main-Class。 / p>
Oracle的教程包含一个完整的演示,但这是另一个从头开始。你需要两个文件:
Test.java:
公共课测试
{
public static void main(String [] args)
{
System.out.println(Hello world);
}
}
manifest.mf:
清单版本:1.0
主类:测试
请注意,文本文件必须以新行或回车结束。
如果最后一行没有以
新行或回车结束,则不会正确解析。
然后运行:
javac Test.java
jar cfm test.jar manifest.mf Test.class
java -jar test.jar
输出:
Hello world
I created a JAR file like this:
jar cf Predit.jar *.*
I ran this JAR file by double clicking on it (it didn't work). So I ran it from the DOS prompt like this:
java -jar Predit.jar
It raised "Failed to load main class" exceptions. So I extracted this JAR file:
jar -xf Predit.jar
and I ran the class file:
java Predit
It worked well. I do not know why the JAR file did not work. Please tell me the steps to run the JAR file
You need to specify a Main-Class in the jar file manifest.
Oracle's tutorial contains a complete demonstration, but here's another one from scratch. You need two files:
Test.java:
public class Test
{
public static void main(String[] args)
{
System.out.println("Hello world");
}
}
manifest.mf:
Manifest-version: 1.0
Main-Class: Test
Note that 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.
Then run:
javac Test.java
jar cfm test.jar manifest.mf Test.class
java -jar test.jar
Output:
Hello world
这篇关于如何运行JAR文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!