开发者

Where is System.err on Windows?

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.


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.


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 ...


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.


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").


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜