如何将 javaw.exe 控制台输出重定向到日志文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4464319/
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 redirect a javaw.exe console output to a log file?
提问by djangofan
I am wanting to start my Java program from a batch file. This is the script I want to use to start the app but the problem is that I am not able to get the console output to redirect to a log file. Can anyone offer any hints without having to edit any code, and by using Java command line options or something?
我想从批处理文件启动我的 Java 程序。这是我想用来启动应用程序的脚本,但问题是我无法让控制台输出重定向到日志文件。任何人都可以在无需编辑任何代码并使用 Java 命令行选项或其他东西的情况下提供任何提示吗?
@echo off
set TASK=MyApp
TITLE %TASK%
start javaw.exe -cp .;Server.jar;Util.jar com.manage.Program %1 > log.log 2>&1
taskkill /T /FI "WINDOWTITLE eq %TASK%"
So, the above works, and actually kills the cmd window that spawns my Swing app, but it doesn't log anything to the log file, presumably because the "start" forked the process away from the "> log.log 2>&1" arg?
因此,上述工作有效,并且实际上杀死了生成我的 Swing 应用程序的 cmd 窗口,但它没有将任何内容记录到日志文件中,大概是因为“开始”将进程从 "> log.log 2>&1 分叉出来“阿?
I could probably fix it by using start to call another batch file but I am hoping for a more elegant answer.
我可能可以通过使用 start 调用另一个批处理文件来修复它,但我希望得到一个更优雅的答案。
采纳答案by Oleg Ryaboy
It sounds like you want to log console output without actually displaying a console. I can see three ways of doing something similar:
听起来您想在不实际显示控制台的情况下记录控制台输出。我可以看到三种做类似事情的方法:
Start console minimized, so that it's less annoying. You can do that by creating a shortcut to a batch file, and then setting the properties of the shortcut to run the batch file in a minimized window instead of using the default setting of "Normal window".
Create a script to run the bat file in a hidden window, like so: http://gallery.technet.microsoft.com/ScriptCenter/en-us/8bbed56f-a7aa-491f-a296-687dd96098a3
Use a third party tool, like hstart. (Note: according to djangofan, hstart was not written for Java programs and does not really work)
最小化启动控制台,使其不那么烦人。您可以通过创建批处理文件的快捷方式来实现,然后设置快捷方式的属性以在最小化窗口中运行批处理文件,而不是使用“普通窗口”的默认设置。
创建一个脚本以在隐藏窗口中运行 bat 文件,如下所示:http: //gallery.technet.microsoft.com/ScriptCenter/en-us/8bbed56f-a7aa-491f-a296-687dd96098a3
使用第三方工具,如hstart。(注意:根据 djangofan 的说法,hstart 不是为 Java 程序编写的,实际上不起作用)
回答by camickr
As far as I know using javaw suppresses all System.out.println(...) to the console.
据我所知,使用 javaw 会将所有 System.out.println(...) 抑制到控制台。
Therefore your application needs to implement logging internally. You could use a wrapper class to redirect the output using System.setOut(...) to write to a file. Then your wrapper class would invoke you other class.
因此,您的应用程序需要在内部实现日志记录。您可以使用包装类使用 System.setOut(...) 重定向输出以写入文件。然后你的包装类会调用你的其他类。
回答by Eugene Kuleshov
You should use java.exe instead of javaw.exe
您应该使用 java.exe 而不是 javaw.exe
回答by Vanchinathan Chandrasekaran
cmd.exe something.bat > console_output.txt
I think this will work.
我认为这会奏效。
回答by LudoMC
I didn't get why you said as a comment to Eugene Kuleshov that even java.exe won't do it.
我不明白你为什么对 Eugene Kuleshov 说即使 java.exe 也不会这样做。
You have to be careful to redirect both out and err output streams.
Usually, when sending data to the console output in Java, the "normal" console log is sent to System.out
but errors are sent to System.err
. The DOS cmd handles this with two different streams.
In this case, you have to either redirect the error stream to a different file like this
您必须小心重定向 out 和 err 输出流。通常,在 Java 中将数据发送到控制台输出时,“正常”控制台日志会发送到,System.out
但错误会发送到System.err
. DOS cmd 使用两个不同的流来处理这个问题。在这种情况下,您必须像这样将错误流重定向到不同的文件
java.exe -cp .;Server.jar;Util.jar com.manage.Program %1 > log.log 2> err.log
or you can merge it in the same file by doing
或者您可以通过执行将其合并到同一个文件中
java.exe -cp .;Server.jar;Util.jar com.manage.Program %1 > log.log 2>&1
We have the same issue when using the start javaw
command.
We only use it in production and use the above java.exe when on test bench.
使用start javaw
命令时我们遇到了同样的问题。我们只在生产中使用它,在测试台上使用上面的 java.exe。
Of course, the best way would be not to output to the console which is the best practice, but who's doing this? ;)
当然,最好的方法是不要输出到控制台,这是最佳实践,但谁在这样做呢?;)
回答by Andrew T Finnell
This is all you need in the batch file
这是您在批处理文件中所需要的全部内容
start /b javaw.exe -cp .;Server.jar;Util.jar com.manage.Program %1 > log.log 2>&1
start /b javaw.exe -cp .;Server.jar;Util.jar com.manage.Program %1 > log.log 2>&1
don't put any of the other information in there. Once your program exits check the log file.
不要把任何其他信息放在那里。一旦您的程序退出,请检查日志文件。
回答by bbaassssiiee
Sometimes your webstart application crashes and you cannot see why because the Console closes with the crash. To enable console logging in Java webstart with JDK 1.6:
有时,您的 webstart 应用程序会崩溃,而您看不到原因,因为控制台会随着崩溃而关闭。要在使用 JDK 1.6 的 Java webstart 中启用控制台日志记录:
Start->Run...->javaws -viewerClose the Java Cache Viewer Advanced tab->Debuggingcheck 'Enable tracing' and 'Enable logging'
开始->运行...-> javaws -viewer关闭 Java Cache Viewer Advanced 选项卡->Debugging检查“启用跟踪”和“启用日志记录”
You logfiles can now be found in:
您现在可以在以下位置找到日志文件:
C:\Documents and Settings\USER\Application Data\Sun\Java\Deployment\log
C:\Documents and Settings\USER\Application Data\Sun\Java\Deployment\log
回答by Bharath
Try something like this
JRE\bin\java -cp .;Server.jar;Util.jar com.manage.Program >>log.log
or
JRE\bin\javaw -cp .;Server.jar;Util.jar com.manage.Program >>log.log
尝试这样的事情
JRE\bin\java -cp .;Server.jar;Util.jar com.manage.Program >>log.log
或
JRE\bin\javaw -cp .;Server.jar;Util.jar com.manage.Program >>log.log
save the above as a batch file and run.
将上述内容另存为批处理文件并运行。
回答by user3430630
Its more simple than you think.
它比你想象的更简单。
You only need to change the System.out
:
您只需要更改System.out
:
System.setOut(new PrintStream(new FileOutputStream("log.txt",true)));
And that's it!
就是这样!
Good luck.
祝你好运。
回答by Jay Kumar
The below code worked perfectly fine for me without changing a single line of code in my java application.
下面的代码对我来说非常好,而无需更改我的 Java 应用程序中的任何一行代码。
javaw -jar <jar_file>.jar >> <log_file>.log
javaw -jar <jar_file>.jar >> <log_file>.log
Sorry, I am not allowed to put a comment on Vanchinathan's post, so posting an answer here.
抱歉,我不允许对 Vanchinathan 的帖子发表评论,因此请在此处发布答案。