Is it possible to execute Windows commands in Java in batch mode? those. one by one, but kept constant?

I'm looking to create a Java application that can run Windows commands in a "batch" way; I mean it's constant between commands and not like you're only doing one thing at a time.

An example of this would be:

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

      

I would like to use a way Process process = Runtime.getRuntime().exec(CMD);

to be able to run each of these lines, but keep it consistent so it doesn't look like each line is being executed completely separate from the others.

I hope I got it a little bit; I intend to substantially remove the use of batch files and store each line as an element in a vector / array and thus perform their "batch" style.

+3


source to share


1 answer


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

You can achieve the desired effect if you like:

Execute only CMD.EXE file without NO parameters, but using WshShell.Exec method:

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

      



The WshShell.Exec method provides access to the standard I / O pipes of the process.

Then send the 'STDIN channel' process 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 as if they were included in the batch file. The only difference is that some commands are not executed correctly, for example GOTO

.

+1


source







All Articles