Problems with process executing batch file. startup.bat for example
1 posts in topic
Flat View  Flat View

Posted By:   Ramon_Ramos
Posted On:   Thursday, June 20, 2002 12:28 PM

When I try to execute some .bat through runtime and process, win98 cannot.
Any system property?
Any problem with security?
I 'm using waitFor and nothing occurs.
System blocks to eternity
Help me.

Re: Problems with process executing batch file. startup.bat for example

Posted By:   Animesh_Srivastava  
Posted On:   Thursday, July 18, 2002 03:19 PM

It actually depends upon the contents of the batch file. Runtime.exec() does this -

1. The process fires the batch file.

2. The batch file in turn fires the commands inside it one by one till all commands get used up.

3. The batch file terminates and the process returns.

The batch file is executed no doubt, but the waitFor returns only when the batch file execution gets completed, which depends on what you are doing in the batch file. For example let the java file be :

public class BatchExample
public static void main (String[] args) throws java.io.IOException, InterruptedException
String[] callAndArgs = { "a.bat"};
Runtime rt = Runtime.getRuntime();
Process child = rt.exec(callAndArgs);
System.out.println("Process exit code is: " + child.exitValue());

Now write a batch file (a.bat) which has one line only in which it calls notepad. That means a.bat has one line which is "notepad". Compiling and running BatchExample it seems that everything is ok - the batch file is called, the notepad is opened, and when closed the process returns.

Now, just change the a.bat to do a "dir" (instead of calling "notepad"). And try out the same thing. It would hang for etertnity (as you said :)

As a thumbrule, os commands which can run successfully directly under Runtime.exec() will also run if specified in a batch file and called via Runtime.exec(name_of_batch_file). Calling "notepad" directly using Runtime.exec() causes no problems, but calling "dir" seems to hang the process. So a batch file which calls "notepad" would work fine but a batch file which calls "dir" hangs.

Hope that helps.

About | Sitemap | Contact