How to execute dos commands in java ???

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • janaki112
    New Member
    • Dec 2006
    • 5

    How to execute dos commands in java ???

    i need to execute a .jar file in command line using java... n i have to take the output obtained from tat file...
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    Originally posted by janaki112
    i need to execute a .jar file in command line using java... n i have to take the output obtained from tat file...
    you can use the exec() method from RunTime, e.g.
    Code:
    // execute a child process using java exec command and get output
    
    import java.io.*;
    import java.lang.*;
    
    public class JavaExec {
    
    public static void main (String args[]){
      try {
         // get runtime environment and execute child process
         Runtime systemShell = Runtime.getRuntime();
         Process output = systemShell.exec("java -jar hello.jar");
         // open reader to get output from process
         BufferedReader br = new BufferedReader (new InputStreamReader(output.getInputStream()));
         String line = null;
         System.out.println("<OUTPUT/>");
          while((line = br.readLine()) != null ) 
             { System.out.println(line);  }          // display process output
         System.out.println("</OUTPUT>");
         int exitVal = output.waitFor();             // get process exit value
         System.out.println("Process Exit Value : "+ exitVal);
         }
       catch (IOException ioe){ System.err.println(ioe); }
       catch (Throwable t) { t.printStackTrace();}
    }
    }

    Comment

    Working...