Question:
How to use exec() command of Java to launch a batch file (the Syntax of using it)?
Mohamed AH
2006-11-05 04:50:33 UTC
How to use exec() command of Java to launch a batch file (the Syntax of using it). A piece of code will be usefull.

Thanks in advance
Three answers:
ZressE
2006-11-05 05:13:28 UTC
I'm not a java programmer, but this is what I came up with:





import java.util.*;

import java.io.*;

public class EXECBATCH

{

public static void main(String args[])

{

try

{

Runtime rt = Runtime.getRuntime();

Process proc = rt.exec("YOURBATCHFILEGOESHERE");

InputStream stderr = proc.getErrorStream();

InputStreamReader isr = new InputStreamReader(stderr);

BufferedReader br = new BufferedReader(isr);

String line = null;

System.out.println("");

while ( (line = br.readLine()) != null)

System.out.println(line);

System.out.println("
");

int exitVal = proc.waitFor();

System.out.println("Process exitValue: " + exitVal);

} catch (Throwable t)

{

t.printStackTrace();

}

}

}



There appear to be a number of potential "pitfalls" with using

the exec command.

Depending on what your batch file does, you might run into some of them.

I've linked to an article (where I got the above code from) which discusses them, and their workarounds.
2006-11-05 13:19:29 UTC
I am a Linux java so I can just plug in the code I found at Sun java. What you ask is an involved topic because it is platform specific. You want Win.



Follow the link I give you below. It looks possible. Thanks for your question. I learned something.



import java.io.*;

import java.util.*;



// java library -- JFC

Runtime runtime = Runtime.getRuntime();



// custom class

Process process = runtime.exec(command);
Dr.Mr.Ed
2006-11-05 22:01:12 UTC
From the source itself: http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Runtime.html


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...