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.
You should use java.exe instead of javaw.exe
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With