0

I want to run batch file with JAVA.

I want to log the output of batch commands to a file and also to hide the cmd console.

Batch file is working fine but with Java it is neither logging correctly nor hiding the console.

Batch file "Ftp.bat" contents:

ftp -s:FtpCommands.txt >> Output.log
exit

Thanks in advance.

2 Answers2

0

Take a look at the ProcessBuilder api,

ProcessBuilder pb = new ProcessBuilder("batchfile.bat", "arg1", "arg2");
Process p = pb.start();
...
p.destroy();

Dont forget to catch any exceptions and when caught run p.destroy()

See this question on how to listen in on the output from the batch file

Community
  • 1
  • 1
Theodor
  • 5,536
  • 15
  • 41
  • 55
  • No, I think the ProcessBuilder is excelent for these types of problems. – Theodor Apr 20 '12 at 07:59
  • But the whole point of the question is what to do with process's output. How does the `ProcessBuilder` API make it easier? – Marko Topolnik Apr 20 '12 at 09:19
  • The example is missing work with stdout. It's a bit harsh to stop half word from complete answer. Also it is not obvious that one should not forget to call Process.destroy() after completion or close process streams explicitly (or they will leak). – Petr Gladkikh Apr 20 '12 at 09:21
0

First make sure that you have VBScript and then try this:

launch.bat

CD /D %~dp0
wscript.exe "%CD%\invisible.vbs" "%CD%\process.bat"

invisible.vbs

CreateObject("Wscript.Shell").Run """" & WScript.Arguments(0) & """", 0, False

process.bat

[whatever you want to do] [perimeters] >> output.log >2>&1

This will launch it invisibly and log it and any errors as well