djangofan
djangofan

Reputation: 29669

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.

Upvotes: 13

Views: 32105

Answers (12)

Gustavo Vanni
Gustavo Vanni

Reputation: 1

I managed to get it working with the command below

start /B /I javaw -jar program.jar >> output.log

Don't know if it will help someone...

Upvotes: 0

Ahmed Elkhodary
Ahmed Elkhodary

Reputation: 31

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://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_redirection?view=powershell-6

Upvotes: 0

Jay
Jay

Reputation: 131

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.

Upvotes: 0

bbaassssiiee
bbaassssiiee

Reputation: 6782

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

Upvotes: 5

user3430630
user3430630

Reputation: 71

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.

Upvotes: 7

Bharath
Bharath

Reputation: 1

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.

Upvotes: 0

Oleg Ryaboy
Oleg Ryaboy

Reputation: 3069

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)

Upvotes: 4

Andrew T Finnell
Andrew T Finnell

Reputation: 13628

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.

Upvotes: 3

LudoMC
LudoMC

Reputation: 760

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

Upvotes: 4

cmd.exe something.bat > console_output.txt

I think this will work.

Upvotes: 0

Eugene Kuleshov
Eugene Kuleshov

Reputation: 31795

You should use java.exe instead of javaw.exe

Upvotes: 2

camickr
camickr

Reputation: 324118

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.

Upvotes: 6

Related Questions