java 双击 JAR 文件不打开命令提示符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10446986/
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
Double Clicking JAR file does not open Command Prompt
提问by whitehat
I want to run a Jar file by double clicking it.
我想通过双击运行 Jar 文件。
Following is the only Java class file present in it.
以下是其中存在的唯一 Java 类文件。
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class Sysout{
public static void main(String[] args) throws IOException{
System.out.println("Hello World!");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String msg = br.readLine();
System.out.println(msg);
br.read();
}
}
And Manifest file has Main-Class defined.
并且 Manifest 文件定义了 Main-Class。
Using thislink, I successfully ran the Jar file by double-clicking the batch file. This opens the command prompt and runs the main class defined.
使用此链接,我通过双击批处理文件成功运行了 Jar 文件。这将打开命令提示符并运行定义的主类。
However, if I double click the Jar file directly, nothing happens. I also checked thislink and associated my .jar to javaw.exe Thislink also suggests the same. Also tried by associating the .jar with java.exe
但是,如果我直接双击 Jar 文件,则没有任何反应。我还检查了此链接并将我的 .jar 与 javaw.exe 相关联。 此链接也提出了相同的建议。还尝试通过将 .jar 与 java.exe 相关联
What happens is the command prompt opens for a fraction of second and vanishes off.
发生的情况是命令提示符打开几分之一秒然后消失。
Even if I am expecting the user to enter some data, double-clicking operation does not wait for the user to enter anything.
即使我期待用户输入一些数据,双击操作也不会等待用户输入任何内容。
Where is the problem?
哪里有问题?
回答by Petesh
When you use the javaw
association, it does not create a command window, and swallows all the System.out
and System.err
invocations.
当您使用javaw
关联时,它不会创建命令窗口,并且会吞下所有的System.out
和System.err
调用。
You should reassociate your .jar
file with the java
binary, which should display the requisite command window.
您应该将您的.jar
文件与java
二进制文件重新关联,这将显示必要的命令窗口。
If you used the simple Open With...
option, it will have omitted the -jar
option from the command line.
如果您使用 simpleOpen With...
选项,它将-jar
在命令行中省略该选项。
Open up an administrator command window (this is needed if you're using Vista or Windows 7 with UAC enabled) and do:
打开管理员命令窗口(如果您使用的是启用了 UAC 的 Vista 或 Windows 7,则需要此窗口)并执行以下操作:
assoc .jar=jarfileterm
ftype jarfileterm="C:\Program Files\Java\jre7\bin\java.exe" -jar "%1" %*
In your case, you should replace the C:\Program Files\Java\jre7\bin\java.exe
path with the one for your install of the jre.
在您的情况下,您应该将C:\Program Files\Java\jre7\bin\java.exe
路径替换为用于安装 jre的路径。
When you double-click following this, then it should run correctly.
当您双击以下内容时,它应该可以正常运行。
You can add another ftype:
您可以添加另一个 ftype:
ftype jarfile="C:\Program Files\Java\jre7\bin\javaw.exe" -jar "%1" %*
again substituting the path to the javaw binary with the one that's for your system.
再次用适用于您系统的路径替换 javaw 二进制文件的路径。
You should now be able to toggle between windowed and non-windowed by alternately choosing assoc .jar=jarfileterm
and assoc .jar=jarfile
您现在应该能够通过交替选择assoc .jar=jarfileterm
和assoc .jar=jarfile
If you want to keep the command window around after running the .jar, then you surround the calling of the java command with a cmd /s /k
viz:
如果您想在运行 .jar 后保留命令窗口,那么您可以使用 viz 包围 java 命令的调用cmd /s /k
:
ftype jarfileterm=cmd /s /k ""C:\Program Files\Java\jre7\bin\java.exe" -jar "%1" %*"
assoc .jar=jarfileterm
If these commands worked, then double clicking on the jar file will cause a command window to pop-up and persist.
如果这些命令有效,则双击 jar 文件将导致弹出一个命令窗口并持续存在。
You cannot set a complex enough command line with either Open With...
or using Default Programs
that will allow the jar file to run. If you have successfully tried all these efforts ftype
and assoc
commands and it still doesn't work, then you will need to peel out the registry editor.
您不能使用Open With...
或 using设置足够复杂的命令行,Default Programs
这将允许 jar 文件运行。如果您已经成功尝试了所有这些努力ftype
和assoc
命令,但仍然无法正常工作,那么您将需要剥离注册表编辑器。
Launch regedit
, and search for a key called .jar under HKEY_CLASSES_ROOT
- this should result in a single value underneath it called (Default)
with a value, if your ftype command invocations worked, then it should read jarfileterm
. If it didn't work, then you're looking at an association that may have been created by another application (I don't know if the java updater replaces these entries, but if it does, then this could be the issue)
Launch regedit
,并在下面搜索一个名为 .jar 的键HKEY_CLASSES_ROOT
- 这应该会导致它下面的单个值(Default)
用一个值调用,如果您的 ftype 命令调用有效,那么它应该读取jarfileterm
. 如果它不起作用,那么您正在查看可能已由另一个应用程序创建的关联(我不知道 java 更新程序是否替换了这些条目,但如果确实如此,那么这可能是问题所在)
You need to next look for this key in the HKEY_CLASSES_ROOT
. It will find this entry, which should contain the a key Shell
(i.e. expand the folder jarfileterm
and it should reveal another folder Shell
), which contains a key Open
which contains a key Command
which contains a (Default)
value that should contain the invocation command for launching .jar files. This command should match the last ftype jarfileterm=...
entries that you typed in. If it doesn't then you should make it match one of the cmd /s /k
or "c:\program files\java\jre7\bin\java.exe" options (depending on if you want to persist the command window in the event of an error in launching or not)
您接下来需要在HKEY_CLASSES_ROOT
. 它将找到这个条目,它应该包含一个键Shell
(即展开文件夹jarfileterm
,它应该显示另一个文件夹Shell
),其中包含一个键Open
,该键包含一个键,该键Command
包含一个(Default)
值,该值应该包含启动 .jar 文件的调用命令。此命令应与ftype jarfileterm=...
您输入的最后一个条目匹配。如果不匹配,则应使其与cmd /s /k
“c:\program files\java\jre7\bin\java.exe”选项之一匹配(取决于您是否想要在启动或未出现错误时保留命令窗口)
回答by RP-
May be your .jar file has binded with any other default program, Right click and open with 'Java(TM) Platform SE binary'. This should work if have a executable jar file.
可能是您的 .jar 文件已与任何其他默认程序绑定,右键单击并使用“Java(TM) Platform SE 二进制文件”打开。如果有一个可执行的 jar 文件,这应该可以工作。