Java 从 Windows 命令行调用 jar 文件时 System.out 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/949451/
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
System.out not working when calling jar-file from Windows command line
提问by Jordi
I have this class:
我有这门课:
public class Test {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
And I used Eclipse's "Export to runnable JAR" function to make it into a executable jar-file (test.jar). When I run this program in Eclipse, it prints out "Hello World!", but when I call the jar-file from the command line, nothing happens.
我使用 Eclipse 的“Export to runnable JAR”功能将其转换为可执行的 jar 文件 (test.jar)。当我在 Eclipse 中运行这个程序时,它会打印出“Hello World!”,但是当我从命令行调用 jar 文件时,没有任何反应。
I have tried calling the jar-file like this:
我试过像这样调用 jar 文件:
test.jar
test.jar -jar
There is no output whatsoever.
没有任何输出。
I have another program that also has a side effect (in addition to output to stdout) and while the side effect is being performed (which tells me the jar-file was definitely executed), again no output is given to the command line. I have also tried using stderr, but that makes no difference. Does anybody know how I can make this work?
我有另一个程序也有副作用(除了输出到标准输出),并且在执行副作用时(它告诉我 jar 文件确实被执行了),同样没有输出到命令行。我也尝试过使用 stderr,但这没什么区别。有谁知道我怎样才能做到这一点?
Thanks!
谢谢!
采纳答案by victor hugo
You must runthe JAR using
您必须使用运行JAR
java -jar test.jar
(Your JRE's bin folder must be added to PATH in order to get the command working from any location)
(您的 JRE 的 bin 文件夹必须添加到 PATH 以便从任何位置运行命令)
NOTE: I know you created the JAR using Eclipse but you might want to know how does an executableJAR works
回答by Petesh
When you invoke the jar using 'test.jar' the starting of the app is handed off to the registered java handler for jar files, which doesn't run in the context of the command line.
The default jar handler doesn't open console based System.{out,err} file handles, as it would mean a cmd style window for each of the jar files launched, which is not an ideal situation.
The previous answer, using java -jar test.jar causes it to run within the context of the current cmd window, and thus you will see the output.
当您使用“test.jar”调用 jar 时,应用程序的启动将移交给已注册的 jar 文件的 java 处理程序,该处理程序不在命令行的上下文中运行。
默认 jar 处理程序不会打开基于控制台的 System.{out,err} 文件句柄,因为这意味着启动的每个 jar 文件都有一个 cmd 样式窗口,这不是理想的情况。
上一个答案,使用 java -jar test.jar 使其在当前 cmd 窗口的上下文中运行,因此您将看到输出。
回答by Billy
The previous answers are correct, so I'll just clarify a bit the "executable jar" concept. There's no such thing as an "executable jar", equivalent to an exe file in windows, in an "executable jar" you'll be only specifying the "entry" point ( your main class you want to be executed by default )
前面的答案是正确的,所以我将澄清一下“可执行 jar”的概念。没有“可执行 jar”之类的东西,相当于 Windows 中的 exe 文件,在“可执行 jar”中,您只需指定“入口”点(您希望默认执行的主类)
A jar is basically just an archive, you'll still need java to actually launch the jar ( java -jar your.jar )
jar 基本上只是一个存档,您仍然需要 java 来实际启动 jar ( java -jar your.jar )
The fact that in windows you might have an association with javaw.exe means this will be launched by the OS when you double-click on the jar, like a .txt file being opened automatically with notepad, it doesn't make the .txt an executable file.
在 Windows 中,您可能与 javaw.exe 有关联,这一事实意味着当您双击 jar 时,操作系统会启动它,就像用记事本自动打开 .txt 文件一样,它不会使 .txt一个可执行文件。
Check this out as well : JAR files revealed
也检查一下: JAR 文件显示