Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

How will you invoke any external process in Java?

user-image
Question added by Mohd shahnawaz khan , Associate Project , Cognizant Technology Solution
Date Posted: 2014/04/16
Mufeed Farhan
by Mufeed Farhan , HR Applications Manager , SBM

We can invoke the external process in Java using exec() method of Runtime Class .

 

Example Below:

import java.io.*;

public class Main

{      

public static void main(String args[])

{           

try {               

Runtime rt = Runtime.getRuntime();               

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();           

}        }}

 

Copied From:

http://www.javaken.com/forum/showthread.php?t=2749 .

 

yahia dekar
by yahia dekar , ingenieur d'etat en informatique , universite djilali liabbes - sidi bel abbes

exec