Java资源网

| JAVA基础 | 环境配置 | JDBC | 线程技术 | Socket编程 | JavaMail | JAVA与XML | 设计模式 | 技术新闻 | Java认证 | 程序人生 软件下载
| JSP&Servlet | Spring | Struts | Hibernate | JBuilder | Eclipse | WebService | EJB技术 | J2ME开发 | 应用服务器 | JXTA | Ajax
Articles search文章搜索
   关键字:
   类 别:
       
New download 最新下载
· [组件]HTML Parser 1.5
· [教程]WebSphere Studio应用教程
· [组件]JDom 1.0
· [工具]Junit3.8.1
· [教程]EJB编程及J2EE系统架构和设计
· [教程]EJB教程
· [教程]J2EE Tutorial中文版
· [教程]Java编程思想2(英文)
· [教程]java编程思想(完整版)
· [教程]Java网络编程
New articles 最新文章
· 设计移动 Web 服务
· 解析XML的时候完全忽略DTD
· 理解XML Schema XML Schema 初步
· 标签库的深入研究
· 提升JSP应用程序的七大绝招
· 如何使用JDOM对XML文件进行操作
· 处理XML字符串中特殊字符
· 利用Digester把XML转换成为Java对象
· 使用WebService 和RMI远程协作
· 使用Axis开发Web Service程序
Articles top 热门文章
· Eclipse基础--plugin插件安装(6644)
· eclipse+tomcat+lomboz的安装配置说明(4774)
· Java程序员就业前景(4584)
· Windows下JAVA环境变量的设置祥解(3788)
· Tomcat下JSP、Servlet和JavaBean环境的配置(3716)
· 使用links方式安装Eclipse插件(3698)
· 一个老程序员的心理话(3533)
· linux下jdk的安装与配置(3459)
· 初学者入门:Structs中基本配置入门(3334)
· Eclipse 运行命令行参数大全(3084)
您的位置:首页>>JAVA基础>>JAVA实现调用本地命令
JAVA实现调用本地命令
2005-07-20   来源:CSDN  作者:lovelyxc
请问能不能实现像在  “运行”窗口  里键入一行url,然后依靠文件关联打开指定文件呢。  
 
我知道Runtime.getRuntime().exec(String)可以实现调用本地命令的功能,但怎么打开一个文件就不知道了。  
 
any  idea?  
---------------------------------------------------------------  
 
String  path  =  "e:\\Project1.exe";  
Runtime.getRuntime().exec("cmd  /c  start  "  +  path);  
---------------------------------------------------------------  
 
http://www.rgagnon.com/javadetails/java-0014.html  
Execute  an  external  program  
This  example  will  capture  the  output  (from  stdio)  of  an  external  program.  import  java.io.*;  
public  class  CmdExec  {  
   public  CmdExec(String  cmdline)  {  
       try  {  
         String  line;  
         Process  p  =  Runtime.getRuntime().exec(cmdline);  
         BufferedReader  input  =    
             new  BufferedReader  
                 (new  InputStreamReader(p.getInputStream()));  
         while  ((line  =  input.readLine())  !=  null)  {  
             System.out.println(line);  
             }  
         input.close();  
         }    
       catch  (Exception  err)  {  
         err.printStackTrace();  
         }  
     }  
 
public  static  void  main(String  argv[])  {  
   new  CmdExec("myprog.bat");  
   }  
}  
 
   
[myprog.bat]  
echo  hello  world!  
 
   
NOTE  1:  Reading  a  BAT  file  output  containing  the  dir  command  may  hang.  I  don't  know  why...    
NOTE  2:  To  make  a  shortcut  :  Create  a  shortcut  to  CmdExec.class.  Go  in  the  shortcut  properties  and  change  the  target  as  "java  CmdExec".  Change  the  run  option  to  Minimized  to  hide  the  black  DOS  console  when  the  BAT  is  executed.    
NOTE  3  :  A  useful  link  on  the  subject  :  JavaWorld  article,  thanks  to  O.  Thomann.    
--------------------------------------------------------------------------------  
 
The  following  example  start  a  Dial-up  connection  on  the  Win  plateform  :  [Dialup.java]  
public  class  Dialup  {  
     public  static  void  main(String[]  args)  throws  Exception  {  
         Process  p  =  Runtime.getRuntime().exec("dialup.bat");  
         p.waitFor();  
         System.out.println("Done.");  
         }  
     }  
   
The  BAT  file  contains  the  DOS  call  to  bring  the  Dialog.  The  "MyConnection"  is  the  DUN  and  it's  case  sensitive.  [dialup.bat]  
rundll32.exe  rnaui.dll,RnaDial  MyConnection  
 
   
You  still  need  to  press  ENTER  to  CONNECT,  there  is  an  option  in  the  Connection  properties  to  connect  automatically.    
 
If  you  want  to  wait  until  the  BAT  file  is  finished,  use  "start  /wait".  [dialup.bat]  
start  /wait  rundll32.exe  rnaui.dll,RnaDial  MyConnection  
 
   
On  NT  and  W2K,  rnaui.dll  is  not  available.  Use  rasdial.exe  instead.  rasdial  "connection  name"  
rasdial  "connection  name"  /d  to  drop  
rasdial  /?  for  more  options  
 
   
 
 
 
--------------------------------------------------------------------------------  
 
To  launch  a  Unix  script  String[]  cmd  =  {"/bin/sh",  "-c",  "ls  >  hello"};  
Runtime.getRuntime().exec(cmd);  
 
   
 
 
 
--------------------------------------------------------------------------------  
 
You  can  include  a  path  for  the  program  to  be  executed.  On  the  Win  plateform,  you  need  to  put  the  path  in  quotes  if  the  path  contains  spaces  in  it.    public  class  Test  {  
     public  static  void  main(String[]  args)  throws  Exception  {  
         Process  p  =  Runtime.getRuntime().exec(  
             "\"c:/program  files/windows/notepad.exe\"");  
               p.waitFor();  
         }  
   }  
   
 
 
 
 
--------------------------------------------------------------------------------  
If  you  need  to  pass  arguments,  it's  safer  to  a  String  array  especially  if  they  contain  spaces.  String[]  cmd  =  {  "myProgram.exe",  "-o=This  is  an  option"  };  
Runtime.getRuntime().exec(cmd);  
 
   
 
 
 
--------------------------------------------------------------------------------  
 
PDF  (Windows  only)  
[ShowPDF.java]  
public  class  ShowPDF  {  
     public  static  void  main(String[]  args)  throws  Exception  {  
         Process  p  =  Runtime.getRuntime().exec("showpdf.bat  mypdf.pdf");  
         p.waitFor();  
         System.out.println("Done.");  
         }  
     }  
 
[showpdf.bat]  
rundll32  url.dll,FileProtocolHandler  %1  
   
 
 
 
--------------------------------------------------------------------------------  
 
VBSCRIPT  
//  Win9x  
Runtime.getRuntime().exec("start  myscript.vbs");  
 
//  WinNT  
Runtime.getRuntime().exec("cmd  /c  start  myscript.vbs");  
 
   
 
 
 
--------------------------------------------------------------------------------  
 
HTML  Help  (Windows  only)  
Runtime.getRuntime().exec("hh.exe  myhelpfile.chm");  
 
   
 
 
 
--------------------------------------------------------------------------------  
 
Any  program  using  the  Windows  file  association  mechanism    
where  "file"  is  the  filename  of  the  data  file  ex.  myresume.doc  to  start  Word  if  the  doc  extension  is  associated  with  it.  Runtime.getRuntime().exec  
   ("rundll32  SHELL32.DLL,ShellExec_RunDLL  "  +  file.getAbsolutePath());  
 
   
 
 
 
--------------------------------------------------------------------------------  
Written  and  compiled  by  Réal  Gagnon  ©1998-2003    
---------------------------------------------------------------  
 
Runtime.getRuntime().exec("cmd  /c  start  E:\\test.txt");

作者Blog:http://blog.csdn.net/lovelyxc/
  --相关文章--
· J2EE全面介绍(二) (2007-04-13)
· 项目经验二则:读取war包中的文件及Ant使用中的OutOfMemoryError解决 (2007-04-13)
· 走向J2EE,漫长的道路 (2007-04-13)
· 步入J2EE架构和过程(2) (2007-04-13)
· 步入J2EE架构和过程(1) (2007-04-13)
· 方兴未艾的CORBA (2007-04-13)

版权所有©2005-2006 JAVA资源网 渝ICP备05007591号 虚拟主机 | 关于我们 | 联系方式 | 广告业务 | 网站地图 | 友情链接