Windows 上的 System.err 在哪里?
我有一个基于 Java GUI 的应用程序,它将一些诊断消息写入 System.out 和 System.err。在 Windows 上运行时这些消息输出在哪里? (例如,在 Mac OS X 上,它们被打印到系统控制台日志中。)
编辑
我应该补充一点,Java 应用程序被打包为 .exe,所以(现在)我可以' t 使用 java
启动它。 (我想,我可以将各个 .JAR 文件复制到 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
实际上,您可以使用 setout() 和 setErr()。
因此,对于 exmaple,如果您想转储到文件,您可以
这样做,只需在
main(String[] args)
中执行一次,您就可以开始了。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
Just do that once in
main(String[] args)
and you should be good to go.AFAIK 如果您使用 java.exe 启动 java 应用程序,它们将被写入控制台。如果您使用 javaw.exe 启动它,它们将无处可去,因为标准输入/输出/错误不会针对 GUI 应用程序进行重定向。 (您可以手动为
javaw.exe
创建一个子进程并重定向输出,但这超出了范围,我想)我建议改用一些日志记录框架并将消息写入日志文件。
log4j
或java.util.logging
将是一个好的开始......AFAIK if you start your java application using
java.exe
, they are written to the console. If you start it usingjavaw.exe
they go nowhere, since standard in/out/err are not redirected for GUI applications. (You could manually create a subprocess forjavaw.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
orjava.util.logging
would be a good start ...这实际上取决于您如何启动应用程序。例如,如果您要从 Windows 命令行提示符(例如
java -jar myApp.jar
)启动 GUI,那么正如您所期望的那样,System.out 和 System.err 都会转到控制台。据我所知,如果您通过 Windows shell 本身启动(例如,双击可执行 JAR),那么这些错误流就会丢失。将应用程序的执行设置为服务(无论在您的情况下听起来不太可能)也会丢失输出流。使用标准输入和输出流通常不是一个好主意,除非您希望在 shell 脚本类型环境中调用您的程序,其中这些是通用的通信渠道。正如您自己所发现的,这些通常在 GUI 环境中没有得到很好的定义。如果您确实想在程序运行时捕获文本信息,请考虑使用“真正的”日志记录框架(例如 log4j 或 java.util.logging 包)将消息捕获到日志中。
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 的应用程序(在双击 .jar 文件时使用 javaw 启动)不会输出 System.out 和 System.err:
加载 java.util.logging.config.file 进行默认初始化
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
如果您通过命令行运行类,
System.err
消息将打印在控制台上。如果不这样做,您将不会看到这些消息。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.您可以使用 Java 控制台 如果它是用 java 运行的。
You could use the Java console if it were ran with java.