Java - Executing a .bat

Soldato
Joined
22 Oct 2005
Posts
2,883
Location
Moving...
I've used a java program to create a batch file, now I want to execute the .bat from within the source code. I've got the following code but it doesn't seem to do anything:

Code:
public void runBatch() throws IOException{
        
        System.out.println("Running the batch script"); 
        Runtime.getRuntime().exec("G:/Java/Batch.bat"); 
        System.out.println("Finished running the batch script"); 
      
    }

The function doesn't chuck up any errors, it looks as though it works but the .bat file doesn't run. Double clicking the .bat file whilst in windows works fine so there's nothing wrong with the .bat file.

What do I need to add/change to the code to get it running?
Thanks.
 
I'm not expert at this but I had a similar problem with my laptop the other day. It turned out that the PATH declaration didn't include the location of the javaw.exe file.

This might be your problem?
 
Code:
String cmd = "G:/Java/Batch.bat";

try
{
  Runtime rTime = Runtime.getRuntime();
  Process process = rTime.exec(cmd);
 
  InputStream p_in = process.getInputStream();
  OutputStream p_out = process.getOutputStream();
  InputStream p_err = process.getErrorStream();

  p_in.close();
  p_out.close();
  p_err.close();
}
catch(Exception e) {
  throw new Exception("Unable to start, "+ cmd;
}

Try this, I don't have a compiler around at the moment to test on. So might not work. You might have issues accessing the file system like this cos of the JVM sandbox.
 
Back
Top Bottom