如何将 Java 控制台输出通过管道传输到文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/626389/
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
How do I pipe the Java console output to a file?
提问by Cedric Meury
I found a bug in an application that completely freezes the JVM. The produced stacktrace would provide valuable information for the developers and I would like to retrieve it from the Java console. When the JVM crashes, the console is frozen and I cannot copy the contained text anymore.
我在一个应用程序中发现了一个完全冻结 JVM 的错误。生成的堆栈跟踪将为开发人员提供有价值的信息,我想从 Java 控制台检索它。当 JVM 崩溃时,控制台被冻结,我无法再复制包含的文本。
Is there way to pipe the Java console directly to a file or some other means of accessing the console output of a Java application?
有没有办法将 Java 控制台直接通过管道传输到文件或其他访问 Java 应用程序控制台输出的方法?
Update: I forgot to mention, without changing the code. I am a manual tester.
更新:我忘了提,没有更改代码。我是一名手动测试员。
Update 2: This is under Windows XP and it's actually a web start application. Piping the output of
更新 2:这是在 Windows XP 下,它实际上是一个网络启动应用程序。管道输出
javaws jnlp-url不起作用(空文件)。
采纳答案by Cedric Meury
Actually one can activate tracingin the Java Control Panel. This will pipe anything that ends up in the Java console in a tracing file.
实际上可以在 Java 控制面板中激活跟踪。这会将在 Java 控制台中结束的任何内容通过管道传输到跟踪文件中。
The log files will end up in:
日志文件最终会出现在:
- <user.home>/.java/deployment/log on Unix/Linux
- <User Application Data Folder>\Sun\Java\Deployment\log on Windows
- /~/Library/Caches/Java/log on OS X
- <user.home>/.java/deployment/log on Unix/Linux
- <用户应用程序数据文件夹>\Sun\Java\Deployment\log(在 Windows 上)
- /~/Library/Caches/Java/log on OS X
回答by oxbow_lakes
(If you can modify the code) you can set the System.out
field to a different value:
(如果您可以修改代码)您可以将该System.out
字段设置为不同的值:
System.setOut(new PrintStream(new FileOutputStream(fileName)));
If you are running a script (invoking the program via java
) from Unix
you could do:
如果您正在运行脚本(通过 调用程序java
),Unix
您可以执行以下操作:
/path/to/script.sh >& path/to/output.log
回答by Tom Hawtin - tackline
A frozen console probably means a deadlock (it could also mean repeated throwing of an exception). You can get a stack dump using jstack
. jps
may make finding the process easier.
冻结的控制台可能意味着死锁(也可能意味着重复抛出异常)。您可以使用jstack
. jps
可能使查找过程更容易。
回答by Jan Jirout
In Mac 10.8.2 logs could be found at /Users/<userName>/Library/Application Support/Oracle/Java/Deployment/log/
.
在 Mac 10.8.2 中,可以在/Users/<userName>/Library/Application Support/Oracle/Java/Deployment/log/
.
Before you have to enable logging from Java Control Panel. Option "Enable logging
" is at tab "Advanced
". Java Control Panel could be started from "System preferences
".
在您必须从 Java 控制面板启用日志记录之前。选项“ Enable logging
”在选项卡“ Advanced
”处。Java 控制面板可以从“ System preferences
”启动。
回答by Muhammad Suleman
try this guideit works for me. it also guides you that how you can set "System.setOut(fileStream);", "System.setErr(fileStream);"
试试这个指南,它对我有用。它还指导您如何设置“System.setOut(fileStream);”、“System.setErr(fileStream);”