Where is System.err on Windows?

笑着哭i 提交于 2019-12-12 13:28:29

问题


I have a Java GUI-based application that writes some diagnostic messages to System.out and System.err. Where are these messages output when running on Windows? (For example, on Mac OS X, they're printed to the system console log.)

Edit

I should add that the Java application is packaged as a .exe, so (right now) I can't launch it using java. (I can copy the individual .JAR files to the Windows test machine, I suppose.)

Also, it's an app I inherited that didn't use a logging framework before; I'd like to modify it to use one, but I was hoping to quickly get some log output to diagnose a problem right now.


回答1:


It really depends on how you launch your application. For example, if you were to launch your GUI from a windows command-line prompt (e.g. such as java -jar myApp.jar), then as you might expect both System.out and System.err would go to the console. If you're launching via the Windows shell itself (double-clicking on an executable JAR, for example) then these error streams are lost, to the best of my knowledge. Setting up execution of the app as a service (however unlikely it may sound in your situation) would also lose the output streams.

Use of the standard in put and output streams is not typically a good idea unless you're expecting your program to be called in a shell-script type environment where these are common channels for communication. As you've discovered yourself, these are typically not well defined in a GUI environment. If you really want to capture textual information while the program is running, consider using a "real" logging framework (such as log4j or the java.util.logging package) to capture messages to a log.




回答2:


You can actually change where System.out and System.err point in your application usingsetout() and setErr().

So for exmaple, if you want to dump to a file you could do

System.setOut(new PrintStream("output.txt"));
System.setErr(new PrintStream("err.txt"));

Just do that once in main(String[] args) and you should be good to go.




回答3:


AFAIK if you start your java application using java.exe, they are written to the console. If you start it using javaw.exe they go nowhere, since standard in/out/err are not redirected for GUI applications. (You could manually create a subprocess for javaw.exe and redirect the output, but that's out of scope, I suppose)

I would suggest to use some logging framework instead and write the messages to a log file. log4j or java.util.logging would be a good start ...




回答4:


GUI-based applications, started with javaw when double-clicking on the .jar file, won't output the System.out and System.err:

Load java.util.logging.config.file for default initialization

"javaw" swallows all output to System.out and System.err. Try "java" instead (without "w").




回答5:


System.err messages are printed on the console, if you run your class via command line. if you don't, you won't see those messages.




回答6:


You could use the Java console if it were ran with java.



来源:https://stackoverflow.com/questions/2083211/where-is-system-err-on-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!