By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,538 Members | 1,130 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,538 IT Pros & Developers. It's quick & easy.

How to execute dos commands in java ???

P: 5
i need to execute a .jar file in command line using java... n i have to take the output obtained from tat file...
Dec 23 '06 #1
Share this Question
Share on Google+
1 Reply


Expert 100+
P: 1,510
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.
Expand|Select|Wrap|Line Numbers
  1. // execute a child process using java exec command and get output
  2.  
  3. import java.io.*;
  4. import java.lang.*;
  5.  
  6. public class JavaExec {
  7.  
  8. public static void main (String args[]){
  9.   try {
  10.      // get runtime environment and execute child process
  11.      Runtime systemShell = Runtime.getRuntime();
  12.      Process output = systemShell.exec("java -jar hello.jar");
  13.      // open reader to get output from process
  14.      BufferedReader br = new BufferedReader (new InputStreamReader(output.getInputStream()));
  15.      String line = null;
  16.      System.out.println("<OUTPUT/>");
  17.       while((line = br.readLine()) != null ) 
  18.          { System.out.println(line);  }          // display process output
  19.      System.out.println("</OUTPUT>");
  20.      int exitVal = output.waitFor();             // get process exit value
  21.      System.out.println("Process Exit Value : "+ exitVal);
  22.      }
  23.    catch (IOException ioe){ System.err.println(ioe); }
  24.    catch (Throwable t) { t.printStackTrace();}
  25. }
  26. }
  27.  
Dec 23 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.