How do I redirect a javaw.exe console output to a log file?

断了今生、忘了曾经 提交于 2019-12-05 02:11:49

It sounds like you want to log console output without actually displaying a console. I can see three ways of doing something similar:

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

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

  3. Use a third party tool, like hstart. (Note: according to djangofan, hstart was not written for Java programs and does not really work)

As far as I know using javaw suppresses all System.out.println(...) to the console.

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.

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:

Start->Run...->javaws -viewer Close the Java Cache Viewer Advanced tab->Debugging check 'Enable tracing' and 'Enable logging'

You logfiles can now be found in:

C:\Documents and Settings\USER\Application Data\Sun\Java\Deployment\log

user3430630

Its more simple than you think.

You only need to change the System.out:

System.setOut(new PrintStream(new FileOutputStream("log.txt",true)));

And that's it!

Good luck.

I didn't get why you said as a comment to Eugene Kuleshov that even java.exe won't do it.

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

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.

Of course, the best way would be not to output to the console which is the best practice, but who's doing this? ;)

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

don't put any of the other information in there. Once your program exits check the log file.

You should use java.exe instead of javaw.exe

cmd.exe something.bat > console_output.txt

I think this will work.

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

save the above as a batch file and run.

The below code worked perfectly fine for me without changing a single line of code in my java application.

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.

Ahmed Elkhodary

you can use this command line at cmd

C:\Program Files\Java\jdk1.8.0_151\bin>javaw -jar D:\ccr_notification\ccr-api-0. 0.1-SNAPSHOT.jar 2>&1 >D:\ccr_notification\output.log

Reference https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_redirection?view=powershell-6

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