java 通过双击在命令提示符中运行 .jar 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16543234/
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
Running a .jar file in a command prompt from double click
提问by Nexus490
I'll start of by saying Im on windows 7.
我将首先说我在 Windows 7 上。
I have created a .jar file which executes fine from the command line using the - java -jar myJar.jar approach
我创建了一个 .jar 文件,它使用 - java -jar myJar.jar 方法从命令行正常执行
But what I'm after is to be able to double click on the jar file and for it to open up the command prompt window and run in the command prompt as if i've just typed the java -jar myJar.jar line.
但我所追求的是能够双击 jar 文件并打开命令提示符窗口并在命令提示符下运行,就好像我刚刚输入了 java -jar myJar.jar 行一样。
When I double click the jar file I do think it is running because a visual part of the java is appearing, but there is no command prompt window showing my console output.
当我双击 jar 文件时,我确实认为它正在运行,因为出现了 java 的可视部分,但没有显示控制台输出的命令提示符窗口。
After looking around I've come across people saying that javaw which is what the jar files are associated with don't have a console and that I need to associate jar files with java.exe instead of javaw.exe. I've tried this and it didn't seem to work.
环顾四周后,我遇到有人说与 jar 文件相关联的 javaw 没有控制台,我需要将 jar 文件与 java.exe 而不是 javaw.exe 相关联。我试过这个,它似乎没有用。
Can anyone help? A step by step would be nice.
任何人都可以帮忙吗?一步一步会很好。
采纳答案by Behnil
This is IMHO not possible. You could open the consolefrom the application itself, but that is OS-dependent. If you need the console open, you have to run the application from it as you already do.
恕我直言,这是不可能的。您可以从应用程序本身打开控制台,但这取决于操作系统。如果您需要打开控制台,您必须像之前一样从它运行应用程序。
回答by user2439749
I had the same question and the bat file idea was genius and saved me a lot of time rewriting code. Thanks!(I would have upvoted,but apparently I don't have enough rep.)
我有同样的问题,bat 文件的想法是天才,为我节省了大量重写代码的时间。谢谢!(我会赞成,但显然我没有足够的代表。)
Batch (or Bat) files are super easy to make.
批处理(或 Bat)文件非常容易制作。
Just put the java -jar YourFile.jar
into notepad (if you're on windows), save as Title.bat
, and put into the same folder as your jar.
只需将其java -jar YourFile.jar
放入记事本(如果您使用的是 Windows),另存为Title.bat
,然后放入与 jar 相同的文件夹中。
presto! a program open-able by the general public.
快点!一个可供公众开放的程序。
回答by Ahmed KRAIEM
I would do something like this:
我会做这样的事情:
(Tested in Widows XP with JRE 1.6, to support other OSs you should verify the path for each OS and select the appropriate console emulator (xterm
, gnome-terminal
... (check for existance and preference...)))
(在带有 JRE 1.6 的 Widows XP 中测试,要支持其他操作系统,您应该验证每个操作系统的路径并选择适当的控制台模拟器(xterm
, gnome-terminal
...(检查存在和偏好...)))
public static void main(String[] args) throws Exception {
if (args.length == 0) {
String path = Main.class.getProtectionDomain().getCodeSource().getLocation().getPath().substring(1);//Adds extra slash (??) didn't know why
String decodedPath = URLDecoder.decode(path, "UTF-8");
System.out.println(decodedPath);
Runtime.getRuntime().exec("cmd /c start java -jar \"" + decodedPath + "\" actual_run");
}
else {
System.out.println("Hello World");
JOptionPane.showMessageDialog(null, "Hello World");
System.in.read();
}
}
回答by Full Frontal Nudity
Alternatively I suggest creating a bat file with this content :
或者,我建议使用以下内容创建一个 bat 文件:
java -jar yourjar.jar
This will launch your jar as well as open the command prompt automatically, all from a simple double click on a bat file. (The bat file needs to be in the same folder as your jar file, else you need to specify the path to the jar, not just the jar name)
这将启动您的 jar 并自动打开命令提示符,只需双击 bat 文件即可。(bat文件需要和你的jar文件在同一个文件夹中,否则你需要指定jar的路径,而不仅仅是jar名称)
回答by Julien Bodin
If you want to display the command line you have to launch your jar with the command line.
如果要显示命令行,则必须使用命令行启动 jar。
java -jar MyJar.jar
java -jar MyJar.jar
回答by lewis4u
This is the easiest solution for beginners:
这是初学者最简单的解决方案:
- Open any text editor
write this two lines:
java "yourmainclassname" pause
save that file as "name".bat
- Run it with double click from windows GUI
- 打开任何文本编辑器
写下这两行:
java "yourmainclassname" pause
将该文件保存为“名称”.bat
- 从 Windows GUI 双击运行它
(of course this new created .bat file must be in the same folder as the .class)
(当然这个新创建的 .bat 文件必须与 .class 在同一个文件夹中)
回答by p.karnaukhov
Check your MANIFEST.MF
检查您的 MANIFEST.MF
- Extract your "executable" jar file into a folder
- find MANIFEST.MF in META-INF folder
- check presence of this field: Main-Class: YourMainClassHere
- 将您的“可执行”jar 文件解压缩到一个文件夹中
- 在 META-INF 文件夹中找到 MANIFEST.MF
- 检查此字段是否存在: Main-Class: YourMainClassHere
If this field dissapeared then open your original MANIFEST.txt for this point: Main-Class: YourMainClassHeremust end with a new line or carriage return
如果此字段消失,则为此点打开您的原始 MANIFEST.txt: Main-Class: YourMainClassHeremust end with a new line or回车
回答by Andrew Thompson
..but there is no command prompt window showing my console output.
..但是没有显示我的控制台输出的命令提示符窗口。
No there wouldn't be a console for an executable Jar. You'll need to put that output in the GUI.
不,不会有可执行 Jar 的控制台。您需要将该输出放在 GUI 中。