如何运行 JAR 文件 [英] How to run a JAR file

查看:28
本文介绍了如何运行 JAR 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个这样的 JAR 文件:

I created a JAR file like this:

jar cf Predit.jar *.*

我通过双击运行了这个 JAR 文件(它不起作用).所以我在 DOS 提示符下运行它,如下所示:

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

它引发了无法加载主类"异常.所以我提取了这个 JAR 文件:

It raised "Failed to load main class" exceptions. So I extracted this JAR file:

jar -xf Predit.jar

然后我运行了类文件:

java Predit

效果很好.我不知道为什么 JAR 文件不起作用.请告诉我运行JAR文件的步骤

It worked well. I do not know why the JAR file did not work. Please tell me the steps to run the JAR file

推荐答案

您需要在 jar 文件清单中指定一个 Main-Class.

You need to specify a Main-Class in the jar file manifest.

Oracle 的教程 包含一个完整的演示,但这里有另一个一个从头开始.您需要两个文件:

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.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.

然后运行:

javac Test.java
jar cfm test.jar manifest.mf Test.class
java -jar test.jar

输出:

Hello world

这篇关于如何运行 JAR 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆