3

I'm looking to create a Java Application that is able to run windows commands in a "batch" manner; what I mean by this is that it's persistent in between commands and isn't as if you're only executing one at a time.

An example of this might be:

@echo off
pushd C:\foldertobepushedto\
call batchfiletobecalled.bat
popd
pushd anotherdirectorytobepushedinto
call anotherbatchfiletobecalled.bat
popd

I would like to be able to use the Process process = Runtime.getRuntime().exec(CMD); manner to be able to run each of these lines but kept persistent so that it's not as if each line is run completely separate from the rest of them.

I hope I am making a bit of sense; I intend to essentially remove the use of batch files all together and store each line as an element in a vector/array and execute them "batch" style that way.

DBD
  • 23,075
  • 12
  • 60
  • 84
Kane Charles
  • 379
  • 3
  • 10
  • 19
  • you can put the commands in array list and loop through array list to execute the commands, or you can consolidate all the patch file in one file and execute it using java? i cant find the question ! – Saddam Abu Ghaida Feb 04 '13 at 00:07
  • I'm trying to make this as clear as I can but it's hard to explain. Running all of those commands I gave as am example separately through command line would be useless as they wouldn't carry onto the next line if that makes sense, however if you run them as a batch file they do carry onto the next line? – Kane Charles Feb 04 '13 at 02:22
  • If I understand you correctly, this effect would probably be achieved with a console program. But I don't know if Java can compile console applications. – Andriy M Feb 04 '13 at 08:06
  • 2
    I would recommend either 1. building a .bat file and then running the entire script, or 2. concatenating the command onto one line and then executing. `@echo off & pushd C:\foldertobepushedto\ && ( call batchfiletobecalled.bat & popd ) & pushd anotherdirectorytobepushedinto && ( call anotherbatchfiletobecalled.bat & popd )` – David Ruhmann Feb 04 '13 at 14:38
  • @DavidRuhmann that sounds like a great idea, I will try concatenating. I could just write some batch files to take arguments and pass them in from within java. Will see what happens – Kane Charles Feb 04 '13 at 23:23
  • Any luck yet getting this running? – Lizz Mar 01 '13 at 07:28
  • @Lizz, if you place a `&&` between each command it appears to be persistent. In the end I found it easier to create some batch files that take arguments and then convert them to `.exe` format for the use of my java application. So if you were to do something along the lines of this: `Process process = Runtime.getRuntime().exec("cmd /c pushd C:\\ && executecommand && popd);` or something along those lines it should work. – Kane Charles Mar 01 '13 at 22:34
  • Interesting! Could you post in the Answer box? :) – Lizz Mar 01 '13 at 22:42

1 Answers1

1

Excuse me. I know a way to solve this problem in JScript; I ignore if there is an equivalent method for Java.

You may achieve the desired effect you want if you:

Execute CMD.EXE file alone with NO parameters, but via WshShell.Exec method:

var WshShell = new ActiveXObject("WScript.Shell");
var oExec = WshShell.Exec("cmd");

WshShell.Exec method provide access to the process standard I/O channels.

After that, send to the process' STDIN channel the commands you want to execute:

oExec.Stdin.WriteLine("@echo off");
oExec.Stdin.WriteLine("pushd C:\foldertobepushedto\");
oExec.Stdin.WriteLine("call batchfiletobecalled.bat");
oExec.Stdin.WriteLine("popd");
oExec.Stdin.WriteLine("exit");

This way, all commands will be executed in a way entirely equivalent as if they would be included in a Batch file. The only difference is that certain commands will not be correctly executed, like GOTO.

Aacini
  • 65,180
  • 12
  • 72
  • 108