How do I redirect a javaw.exe console output to a

2020-08-11 11:19发布

问题:

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?

@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?

I could probably fix it by using start to call another batch file but I am hoping for a more elegant answer.

回答1:

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)



回答2:

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.



回答3:

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.



回答4:

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



回答5:

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? ;)



回答6:

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.



回答7:

You should use java.exe instead of javaw.exe



回答8:

cmd.exe something.bat > console_output.txt

I think this will work.



回答9:

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.



回答10:

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.



回答11:

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