Java Code: Running command-line or execute external application from Java

Java Code: Running command-line or execute external application from Java

import java.io.*;

public class Main {

public static void main(String args[]) {

try {
Runtime rt = Runtime.getRuntime();
//Process pr = rt.exec(“cmd /c dir”);
Process pr = rt.exec(“c:\\helloworld.exe”);

BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));

String line=null;

while((line=input.readLine()) != null) {
System.out.println(line);
}

int exitVal = pr.waitFor();
System.out.println(“Exited with error code “+exitVal);

} catch(Exception e) {
System.out.println(e.toString());
e.printStackTrace();
}
}

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s