关于java Runtime.getRunTime.exec(String command)的使用

来源:互联网 发布:华夏网络工具包 编辑:程序博客网 时间:2024/05/16 02:01

原文地址:http://sealbird.iteye.com/blog/1202214

 

Java代码  收藏代码
  1. 2008-09-26 19:44当要调用一个外部程序的时候,java提供了exec方法,具体用法是:Runtime.getRunTime.exec("cmd /C Start mailto: abc@xxx.com").其中cmd /c是调用cmd下的start命令,它相当于对一个文件双击。也可以用Runtime.getRunTime.exec("c:\\EXCEl.exe d:\\a.xls")来打开D盘下的excel文件.  
  2.   
  3.   
  4. import java.io.BufferedReader;  
  5. import java.io.IOException;  
  6. import java.io.InputStream;  
  7. import java.io.InputStreamReader;  
  8.   
  9. public   class   test   {  
  10.   
  11. /**  
  12.    *   @param   args  
  13.    */   
  14. public   static   void   main(String[]   args)   {   
  15. //    TODO   Auto-generated   method   stub   
  16. Runtime rn=Runtime.getRuntime();   
  17. Process p= null;   
  18. try {   
  19. //p = rn.exec( "cmd   /k   dir ");   
  20.    p = rn.exec( "C:\\Windows\\system32\\notepad.exe f:\\gg.txt");   
  21.   
  22. InputStream   in   =p.getInputStream();   
  23. BufferedReader   br   =   new   BufferedReader(new   InputStreamReader(in));   
  24. String   str   =   null;   
  25. while((str=br.readLine())!= null){   
  26. System.out.println(str);   
  27. }   
  28. br.close();   
  29. }   catch   (Exception   e)   {   
  30. System.out.println( "Error   exec   notepad ");   
  31. }   
  32. }   
  33. }  
  34.   
  35.   
  36. /* 
  37. public class Test { 
  38.  
  39. /** 
  40. * @param args 
  41. * @throws IOException  
  42. * @throws InterruptedException  
  43. */  
  44. /*public static void main(String[] args) throws IOException, InterruptedException  
  45. { 
  46.    // TODO 自动生成方法存根 
  47.    Process process = Runtime.getRuntime().exec("C:\\Program Files\\Microsoft Office\\Office12\\winword.exe f:\\gg.docx");  
  48.    //process.waitFor( );cmd /c java f:\\T 
  49.    /*String ls_1;  
  50.    Process process = Runtime.getRuntime().exec("cmd /c dir \\windows");  
  51.    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream())); 
  52.    while ( (ls_1=bufferedReader.readLine()) != null)  
  53.    System.out.println(ls_1); */  
  54.    //C:\\Program Files\\Microsoft Office\\Office12\\winword.exe  
  55. //   C:\Program Files\Microsoft Office\Office12\winword.exe  
  56. /*   
  57.    process.waitFor( );  
  58.    //process.destroy(); 
  59.  
  60. } 
  61.  
  62. }*/  
  63.   
  64. Process process = Runtime.getRuntime().exec("cmd /c del f:\\aaa.doc");   
  65.   
  66. 这样的调用是没有问题~  
  67.   
  68.   
  69. 真正最正确的用BAT运行JAVA不显示DOS窗口(连闪一下都不闪)   
  70.   
  71. 今天写一个独立于RCP项目之外的SWT小工具,需要用批处理启动,偶写了一个批处理没闪DOS窗口,看得同事一愣一愣的。于是赶快把自己当年一点心得和大家分享下。  
  72.   
  73.   
  74. 很多朋友在WINDOWS下会用批处理去启动自己的java程序,  
  75.   
  76. 一般的写法是  
  77.   
  78. 运行class:  
  79.   
  80. java xx  
  81.   
  82.   
  83. 运行jar:  
  84.   
  85. java -jar xxx.jar  
  86.   
  87. 但是这样运行会有一个恶心的对话框停在那直到我们关闭程序。  
  88.   
  89.   
  90.   
  91. 于是很多人说可以这样  
  92.   
  93. 运行class:  
  94.   
  95. start javaw xx  
  96.   
  97. 运行jar:  
  98.   
  99. start javaw -jar xxx.jar   
  100.   
  101.   
  102. 这种方法DOS窗口还是会一闪而过,这就算解决问题了吗?!网上很多人说是的.  
  103.   
  104. 对我们这种追求完美的人来说闪一下还是不能接受滴.  
  105.   
  106.   
  107. 于是终极解决方案出现了!  
  108.   
  109. 那就是在批处理第一行加上@echo off  
  110.   
  111.   
  112. 这样我们的批处理就变成了  
  113.   
  114.   
  115. 运行class:  
  116.   
  117. @echo off  
  118.   
  119. start javaw xx  
  120.   
  121.   
  122. 运行jar:  
  123.   
  124. @echo off  
  125.   
  126. start javaw -jar xxx.jar   
  127.   
  128.   
  129.   
  130. 快试试吧,绝对不闪了。哈哈哈。  
  131.   
  132.   
  133. 解释一下  
  134.   
  135. echo off  
  136.   
  137. 表示在此语句后所有运行的命令都不显示命令行本身   
  138.   
  139. @ 表示运行时不显示本命令行  
  140.   
  141.   
  142. public class TestCmd {   
  143. public TestCmd() {   
  144. }   
  145.   
  146. public static void main(String args[]) {   
  147. try {   
  148.   
  149. // 登网站   
  150. Process process = Runtime.getRuntime().exec(   
  151. "cmd.exe /c start http://www.hao123.net/");   
  152.   
  153. // 使用用Ping命令   
  154.   
  155. Process ee = Runtime.getRuntime().exec(   
  156. "cmd.exe /c start ping 10.5.2.19");   
  157.   
  158. catch (Exception e) {   
  159. e.printStackTrace();   
  160. }   
  161. }   
  162. }   
  163.   
  164. 运行这个类你会看到效果   
  165. 这个是运行了ping命令   
  166.   
  167.   
  168. 我使用Process pc = Runtime.getRuntime().exec("cmd /c ping 127.0.0.1");可以成功  

 

 

原创粉丝点击