Java 如何运行 JAR 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1238145/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to run a JAR file
提问by
I created a JAR file like this:
我创建了一个这样的 JAR 文件:
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:
我通过双击运行这个 JAR 文件(它不起作用)。所以我从 DOS 提示符下运行它,如下所示:
java -jar Predit.jar
It raised "Failed to load main class" exceptions. So I extracted this JAR file:
它引发了“无法加载主类”异常。所以我提取了这个 JAR 文件:
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
它运作良好。我不知道为什么 JAR 文件不起作用。请告诉我运行JAR文件的步骤
回答by Jon Skeet
You need to specify a Main-Class in the jar file manifest.
您需要在 jar 文件清单中指定一个 Main-Class。
Oracle's tutorialcontains a complete demonstration, but here's another one from scratch. You need two files:
Oracle 的教程包含一个完整的演示,但这里是另一个从头开始的演示。你需要两个文件:
Test.java:
测试.java:
public class Test
{
public static void main(String[] args)
{
System.out.println("Hello world");
}
}
manifest.mf:
清单.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
回答by Florian Fankhauser
You have to add a manifest to the jar, which tells the java runtime what the main class is. Create a file 'Manifest.mf' with the following content:
您必须向 jar 添加一个清单,它告诉 java 运行时主类是什么。使用以下内容创建文件“Manifest.mf”:
Manifest-Version: 1.0
Main-Class: your.programs.MainClass
Change 'your.programs.MainClass' to your actual main class. Now put the file into the Jar-file, in a subfolder named 'META-INF'. You can use any ZIP-utility for that.
将“your.programs.MainClass”更改为您的实际主类。现在将文件放入名为“META-INF”的子文件夹中的 Jar 文件中。您可以为此使用任何 ZIP 实用程序。
回答by fortran
If you don't want to deal with those details, you can also use the export jar assistants from Eclipse or NetBeans.
如果不想处理这些细节,还可以使用 Eclipse 或 NetBeans 中的导出 jar 助手。
回答by Egil
If you don`t want to create a manifest just to run the jar file, you can reference the main-class directly from the command line when you run the jar file.
如果您不想创建清单只是为了运行 jar 文件,则可以在运行 jar 文件时直接从命令行引用主类。
java -jar Predit.jar -classpath your.package.name.Test
This sets the which main-class to run in the jar file.
这将设置在 jar 文件中运行的主类。
回答by Lynch
java -classpath Predit.jar your.package.name.MainClass
回答by Deepa
To run jar, first u have to create
要运行 jar,首先你必须创建
executable jar
可执行jar
then
然后
java -jar xyz.jar
java -jar xyz.jar
command will work
命令会起作用
回答by Mohammad Shahid Siddiqui
Java
爪哇
class Hello{
public static void main(String [] args){
System.out.println("Hello Shahid");
}
}
manifest.mf
清单文件
Manifest-version: 1.0
Main-Class: Hello
On command Line:
在命令行上:
$ jar cfm HelloMss.jar manifest.mf Hello.class
$ java -jar HelloMss.jar
Output:
输出:
Hello Shahid
回答by Yash
Eclipse Runnable JAR File
Eclipse 可运行 JAR 文件
Create a Java Project – RunnableJAR
创建一个 Java 项目—— RunnableJAR
- If any jar filesare used then add them to project build path.
- Select the class having main() while creating Runnable Jar file.
- 如果使用了任何jar 文件,则将它们添加到项目构建路径中。
- 在创建 Runnable Jar 文件时选择具有 main() 的类。
Main Class
主类
public class RunnableMainClass {
public static void main(String[] args) throws InterruptedException {
System.out.println("Name : "+args[0]);
System.out.println(" ID : "+args[1]);
}
}
Run Jarfile using java program (cmd) by supplying arguments and get the output and display in eclipse console.
通过提供参数使用 java 程序(cmd)运行 Jar文件,并在 eclipse 控制台中获取输出和显示。
public class RunJar {
static StringBuilder sb = new StringBuilder();
public static void main(String[] args) throws IOException {
String jarfile = "D:\JarLocation\myRunnable.jar";
String name = "Yash";
String id = "777";
try { // jarname arguments has to be saperated by spaces
Process process = Runtime.getRuntime().exec("cmd.exe start /C java -jar "+jarfile+" "+name+" "+id);
//.exec("cmd.exe /C start dir java -jar "+jarfile+" "+name+" "+id+" dir");
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream ()));
String line = null;
while ((line = br.readLine()) != null){
sb.append(line).append("\n");
}
System.out.println("Console OUTPUT : \n"+sb.toString());
process.destroy();
}catch (Exception e){
System.err.println(e.getMessage());
}
}
}
In Eclipse to find Short cuts:
在 Eclipse 中找到Shortcuts:
Help ? Help Contents ? Java development user guide ? References ? Menus and Actions
帮助 ?帮助内容 ? Java 开发用户指南 ? 参考 ?菜单和操作
回答by Gabriel D
I have this folder structure:
我有这个文件夹结构:
D:\JavaProjects\OlivePressApp\com\lynda\olivepress\Main.class D:\JavaProjects\OlivePressApp\com\lynda\olivepress\press\OlivePress.class D:\JavaProjects\OlivePressApp\com\lynda\olivepress\olives\Kalamata.class D:\JavaProjects\OlivePressApp\com\lynda\olivepress\olives\Ligurian.class D:\JavaProjects\OlivePressApp\com\lynda\olivepress\olives\Olive.class
D:\JavaProjects\OlivePressApp\com\lynda\olivepress\Main.class D:\JavaProjects\OlivePressApp\com\lynda\olivepress\press\OlivePress.class D:\JavaProjects\OlivePressApp\com\lynda\olivepress\olives\Kalamata .class D:\JavaProjects\OlivePressApp\com\lynda\olivepress\olives\Ligurian.class D:\JavaProjects\OlivePressApp\com\lynda\olivepress\olives\Olive.class
Main.class
is in package com.lynda.olivepress
Main.class
在包裹里 com.lynda.olivepress
There are two other packages:
还有另外两个包:
com.lynda.olivepress.press
com.lynda.olivepress.olive
com.lynda.olivepress.press
com.lynda.olivepress.olive
1) Create a file named "Manifest.txt"
with Two Lines, First with Main-Class and a Second Empty Line.
1)创建一个以"Manifest.txt"
两行命名的文件,第一个是主类,第二个是空行。
Main-Class: com.lynda.olivepress.Main
D:\JavaProjects\OlivePressApp\ Manifest.txt
D:\JavaProjects\OlivePressApp\ Manifest.txt
2) Create JAR with Manifest and Main-Class Entry Point
2) 使用 Manifest 和 Main-Class 入口点创建 JAR
D:\JavaProjects\OlivePressApp>jar cfm OlivePressApp.jar Manifest.txt com/lynda/olivepress/Main.class com/lynda/olivepress/*
D:\JavaProjects\OlivePressApp>jar cfm OlivePressApp.jar Manifest.txt com/lynda/olivepress/Main.class com/lynda/olivepress/*
3) Run JAR
3)运行JAR
java -jar OlivePressApp.jar
java -jar OlivePressApp.jar
Note: com/lynda/olivepress/*
means including the other two packages mentioned above, before point 1)
注:com/lynda/olivepress/*
表示包括上面提到的其他两个包,在第1点之前)
回答by KARTHIKEYAN.A
Before run the jar check Main-Class: classname
is available or not in MANIFEST.MFfile. MANIFEST.MF is present in jar.
在运行之前,检查MANIFEST.MF文件中jar 是否Main-Class: classname
可用。MANIFEST.MF 存在于 jar 中。
java -jar filename.jar