原文:http://tech.ccidnet.com/art/3539/20070402/1050427_1.html

 

1.调用其他程序,好像只能是exe等可执行程序,

public class Exec {

public static void main(String[] args) {

try{
Process p=Runtime.getRuntime().exec("mspaint");
p.waitFor();
}catch(Exception e){
}
}
}

 

其中引号内mspaint是系统自带的画图板程序,当然还可以写其它自系统自带程序比如notepad等以及自己写的exe程序等等

 

2.用同样的方法,可以指定程序来打开文件:

 

public class Exec {

public static void main(String[] args) {

try{
Process p=Runtime.getRuntime().exec
("c:\\Program Files\\MicrosoftOffice\\office\\winword.exe d:\\a.doc");
p.waitFor();
}catch(Exception e){
}
}
}

上例中用word程序来打开D盘根目录下的a.doc文件

 

原文:http://blog.163.com/cshzxing@126/blog/static/37732930200711623241795/

mport java.io.IOException;
import java.io.InputStream;

public class CallCmd {
 /**
  * 调用.bat的脚本,等待脚本执行完毕后在执行后面的工作.
  * @param locationCmd bat脚本的位置C:\\2006121911111AP\\2006121911111.bat
  */
 
 public static void  callCmd(String locationCmd){
  try {
   Process child = Runtime.getRuntime().exec("cmd.exe /C start "+locationCmd);
    InputStream in = child.getInputStream();
   int c;
   while ((c = in.read()) != -1) {
   }
   in.close();
   try {
    child.waitFor();
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
   System.out.println("done");


打开百度的网站:

try {
            Runtime.getRuntime().exec("explorer http://www.baidu.com");
        } catch (Exception ex) {
            ex.printStackTrace();
        }

posted on 2011-05-05 10:05  hotty  阅读(524)  评论(0)    收藏  举报