Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 403
djangofan Avatar asked Dec 16 '10 18:12

djangofan


2 Answers

You should use java.exe instead of javaw.exe

like image 190
Eugene Kuleshov Avatar answered Sep 27 '22 21:09

Eugene Kuleshov


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.

like image 44
user3430630 Avatar answered Sep 27 '22 22:09

user3430630