Dustin
Dustin

Reputation: 142

Java Problem running a .bat file

I'm trying to run two bat files from a Java app. I'm using:

try
{
  Runtime rt = Runtime.getRuntime();
  Process proc = rt.exec(fullCommand);
  InputStream stderr = proc.getErrorStream();
  InputStreamReader isr = new InputStreamReader(stderr);
  BufferedReader br = new BufferedReader(isr);

  int exitVal = proc.waitFor();
  System.out.println("Process exitValue: " + exitVal);

  System.out.println("Working...");
} //End try

catch (Throwable t)
{
  t.printStackTrace();
} //End catch

} //End method

The bat file calls another bat file. It never seems to exit and return control to the original bat file.

From 1.bat

set zzname=%1
zzlookup.bat %zzname%

The other bat file runs a few commands and then should exit. Do I need to do something special with the runtime part?

Thanks in advance, Dustin

Upvotes: 2

Views: 2084

Answers (3)

carej
carej

Reputation:

In addition to the advice above I don't think Runtime.exec allows running .bat files directly. Try prefixing your command with "cmd \c" first.

See http://ant.apache.org/manual/Tasks/exec.html

Upvotes: 4

javashlook
javashlook

Reputation: 10471

If the batch file generates output, you need to drain the streams representing standard and error output.

There are already working examples here.

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1500385

To call one batch file from another and still get back to the original, you need

call zzlookup.bat %zzname%

Otherwise as soon as zzlookup.bat exits, the process will stop.

For example:

withcall.bat:

@echo Before
@call other.bat
@echo After

direct.bat:

@echo Before
@other.bat
@echo After

other.bat:

@echo Other

Output:

c:\Users\Jon\Test>withcall
Before
Other
After

c:\Users\Jon\Test>direct
Before
Other

Upvotes: 3

Related Questions