读书人

实施可执行程序

发布时间: 2012-08-28 12:37:01 作者: rapoo

执行可执行程序

实施可执行程序

2实施可执行程序 String s;

3实施可执行程序 Process process = Runtime.getRuntime().exec("cmd /c dir \\windows");

4实施可执行程序 BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream());

5实施可执行程序 while((s=bufferedReader.readLine()) != null)

6实施可执行程序 System.out.println(s);

7实施可执行程序 process.waitfor();

?

1实施可执行程序实施可执行程序

2实施可执行程序 Process process = Runtime.getRuntime().exec(".\\p.exe");

3实施可执行程序 process.waitfor();

4实施可执行程序实施可执行程序

在上面的程序中,第一行的“.\\p.exe”是要执行的程序名,Runtime.getRuntime()返回当前应用程序的Runtime对象,该对象的exec()方法指示Java虚拟机创建一个子进程执行指定的可执行程序,并返回与该子进程对应的Process对象实例。通过Process可以控制该子进程的执行或获取该子进程的信息。第二条语句的目的等待子进程完成再往下执行。

但在windows平台上,如果处理不当,有时并不能得到预期的结果。下面是笔者在实际编程中总结的几种需要注意的情况:

1、执行DOS的内部命令

如果要执行一条DOS内部命令,有两种方法。一种方法是把命令解释器包含在exec()的参数中。例如,执行dir命令,在NT上,可写成exec("cmd.exe /c dir"),在windows95/98下,可写成“command.exe /c dir”,其中参数“/c”表示命令执行后关闭DOS立即关闭窗口。另一种方法是,把内部命令放在一个批命令my_dir.bat文件中,在Java程序中写成exec("my_dir.bat")。如果仅仅写成exec("dir"),Java虚拟机则会报运行时错误。前一种方法要保证程序的可移植性,需要在程序中读取运行的操作系统平台,以调用不同的命令解释器。后一种方法则不需要做更多的处理。

2、打开一个不可执行的文件

打开一个不可执行的文件,但该文件存在关联的应用程序,则可以有两种方式。以打开一个word文档a.doc文件为例,Java中可以有以下两种写法:

1实施可执行程序 exec("start .\\a.doc");

2实施可执行程序 exec("Files\\Microsoft Office\\office\\winword.exe .\\a.doc");

?

转自http://blog.csdn.net/turkeyzhou/article/details/2323269

?

例子:

Java Process类的浅学习

文章分类:Java编程

Java代码
  1. package?com.iwtxokhtd.other; ??
  2. ??
  3. import?java.io.IOException; ??
  4. ??
  5. public?class?ProcessTest?{ ??
  6. ??
  7. ????public?static?void?main(String[]?args)?{ ??
  8. ????????try?{ ??
  9. ????????????????????????Process?proc=Runtime.getRuntime().exec("notepad"); ??
  10. ????????}?catch?(IOException?e)?{ ??
  11. ????????????//?TODO?Auto-generated?catch?block[java]
  12. package?com.iwtxokhtd.other;??
  13. ??
  14. import?java.io.IOException;??
  15. ??
  16. public?class?ProcessTest?{??
  17. ??
  18. ????public?static?void?main(String[]?args)?{??
  19. ????????try?{??
  20. ????????????????????????Process?proc=Runtime.getRuntime().exec("notepad");??
  21. ????????}?catch?(IOException?e)?{??
  22. ????????????//?TODO?Auto-generated?catch?block??
  23. ????????????e.printStackTrace();??
  24. ????????}??
  25. ??
  26. ????}??
  27. ??
  28. }??

?

(2)使用它的其它构造方法执行相关的命令,如下例:

?

Java代码
  1. package?com.iwtxokhtd.other; ??
  2. ??
  3. import?java.io.IOException; ??
  4. ??
  5. public?class?ProcessTest?{ ??
  6. ??
  7. ????public?static?void?main(String[]?args)?{ ??
  8. ????????try?{ ??
  9. ???????????? ??
  10. ????????????String?exeFullPathName="C:/Program?Files/Internet?Explorer/IEXPLORE.EXE"; ??
  11. ????????????String?message="www.google.com"; ??
  12. ????????????String?[]cmd={exeFullPathName,message}; ??
  13. ????????????Process?proc=Runtime.getRuntime().exec(cmd); ??
  14. ????????}?catch?(IOException?e)?{ ??
  15. ????????????//?TODO?Auto-generated?catch?block[java]
  16. package?com.iwtxokhtd.other;??
  17. ??
  18. import?java.io.IOException;??
  19. ??
  20. public?class?ProcessTest?{??
  21. ??
  22. ????public?static?void?main(String[]?args)?{??
  23. ????????try?{??
  24. ??????????????
  25. ????????????String?exeFullPathName="C:/Program?Files/Internet?Explorer/IEXPLORE.EXE";??
  26. ????????????String?message="www.google.com";??
  27. ????????????String?[]cmd={exeFullPathName,message};??
  28. ????????????Process?proc=Runtime.getRuntime().exec(cmd);??
  29. ????????}?catch?(IOException?e)?{??
  30. ????????????//?TODO?Auto-generated?catch?block??
  31. ????????????e.printStackTrace();??
  32. ????????}??
  33. ??
  34. ????}??
  35. ??
  36. }??

?

执行上述命令可以打开Google网站

(3)列出系统正在运行的所有进程信息

??

Java代码
  1. package?com.iwtxokhtd.other; ??
  2. ??
  3. import?java.io.BufferedReader; ??
  4. import?java.io.IOException; ??
  5. import?java.io.InputStreamReader; ??
  6. ??
  7. public?class?ListAllProcessTest?{ ??
  8. ??
  9. ????//列出所有的进程信息public?static?void?main(String[]?args)?{ ??
  10. ????????BufferedReader?br=null; ??
  11. ????????try?{ ??
  12. ????????????Process?proc=Runtime.getRuntime().exec("tasklist"); ??
  13. ????????????br=new?BufferedReader(new?InputStreamReader(proc.getInputStream())); ??
  14. ????????????@SuppressWarnings("unused") ??
  15. ????????????String?line=null; ??
  16. ????????????System.out.println("打印所有正在运行的进程信息"); ??
  17. ????????????while((line=br.readLine())!=null){ ??
  18. ????????????????System.out.println(br.readLine()); ??
  19. ????????????} ??
  20. ????????}?catch?(IOException?e)?{ ??
  21. ????????????e.printStackTrace(); ??
  22. ????????}finally{ ??
  23. ????????????if(br!=null){ ??
  24. ????????????????try?{ ??
  25. ????????????????????br.close(); ??
  26. ????????????????}?catch?(Exception?e)?{ ??
  27. ????????????????????e.printStackTrace(); ??
  28. ????????????????} ??
  29. ????????????} ??
  30. ????????} ??
  31. ???????? ??
  32. ??
  33. ????} ??
  34. ??
  35. }??
[java]
  • package?com.iwtxokhtd.other;??
  • ??
  • import?java.io.BufferedReader;??
  • import?java.io.IOException;??
  • import?java.io.InputStreamReader;??
  • ??
  • public?class?ListAllProcessTest?{??
  • ??
  • ????//列出所有的进程信息??
  • ????public?static?void?main(String[]?args)?{??
  • ????????BufferedReader?br=null;??
  • ????????try?{??
  • ????????????Process?proc=Runtime.getRuntime().exec("tasklist");??
  • ????????????br=new?BufferedReader(new?InputStreamReader(proc.getInputStream()));??
  • ????????????@SuppressWarnings("unused")??
  • ????????????String?line=null;??
  • ????????????System.out.println("打印所有正在运行的进程信息");??
  • ????????????while((line=br.readLine())!=null){??
  • ????????????????System.out.println(br.readLine());??
  • ????????????}??
  • ????????}?catch?(IOException?e)?{??
  • ????????????e.printStackTrace();??
  • ????????}finally{??
  • ????????????if(br!=null){??
  • ????????????????try?{??
  • ????????????????????br.close();??
  • ????????????????}?catch?(Exception?e)?{??
  • ????????????????????e.printStackTrace();??
  • ????????????????}??
  • ????????????}??
  • ????????}??
  • ??????????
  • ??
  • ????}??
  • ??
  • }??

    ?

    (4)判断一个具体的进程是否正在运行,如下例:

    Java代码
    1. package?com.iwtxokhtd.other; ??
    2. import?java.io.BufferedReader; ??
    3. import?java.io.InputStreamReader; ??
    4. public?class?FindProcessExeTest ??
    5. { ??
    6. ????public?static?void?main(String?[]args){ ??
    7. ???????? ??
    8. ????????if(findProcess("QQ.exe")){ ??
    9. ????????????System.out.println("------判断指定的进程是否在运行------"); ??
    10. ????????????System.out.println("QQ.exe该进程正在运行!"); ??
    11. ????????}else{ ??
    12. ????????????System.out.println("------判断指定的进程是否在运行------"); ??
    13. ????????????System.out.println("QQ.exe该进程没有在运行!"); ??
    14. ????????} ??
    15. ??
    16. ????} ??
    17. ????public?static?boolean?findProcess(String?processName){ ??
    18. ????????BufferedReader?br=null; ??
    19. ????????try{ ??
    20. ??????????? ??
    21. ????????????//下面这句是列出含有processName的进程图像名"tasklist?/FI?/"IMAGENAME?eq?"+processName+"/""); ??
    22. ????????????br=new?BufferedReader(new?InputStreamReader(proc.getInputStream())); ??
    23. ????????????String?line=null; ??
    24. ????????????while((line=br.readLine())!=null){ ??
    25. ????????????????//判断指定的进程是否在运行if(line.contains(processName)){ ??
    26. ????????????????????return?true; ??
    27. ????????????????} ??
    28. ????????????} ??
    29. ???????????? ??
    30. ????????????return?false; ??
    31. ????????}catch(Exception?e){ ??
    32. ????????????e.printStackTrace(); ??
    33. ????????????return?false; ??
    34. ????????}finally{ ??
    35. ????????????if(br!=null){ ??
    36. ????????????????try{ ??
    37. ????????????????????br.close(); ??
    38. ????????????????}catch(Exception?ex){ ??
    39. ????????????????} ??
    40. ????????????} ??
    41. ???????????? ??
    42. ????????} ??
    43. ????} ??
    44. }??
    [java]
  • package?com.iwtxokhtd.other;??
  • import?java.io.BufferedReader;??
  • import?java.io.InputStreamReader;??
  • public?class?FindProcessExeTest??
  • {??
  • ????public?static?void?main(String?[]args){??
  • ??????????
  • ????????if(findProcess("QQ.exe")){??
  • ????????????System.out.println("------判断指定的进程是否在运行------");??
  • ????????????System.out.println("QQ.exe该进程正在运行!");??
  • ????????}else{??
  • ????????????System.out.println("------判断指定的进程是否在运行------");??
  • ????????????System.out.println("QQ.exe该进程没有在运行!");??
  • ????????}??
  • ??
  • ????}??
  • ????public?static?boolean?findProcess(String?processName){??
  • ????????BufferedReader?br=null;??
  • ????????try{??
  • ?????????????
  • ????????????//下面这句是列出含有processName的进程图像名??
  • ????????????Process?proc=Runtime.getRuntime().exec("tasklist?/FI?/"IMAGENAME?eq?"+processName+"/"");??
  • ????????????br=new?BufferedReader(new?InputStreamReader(proc.getInputStream()));??
  • ????????????String?line=null;??
  • ????????????while((line=br.readLine())!=null){??
  • ????????????????//判断指定的进程是否在运行??
  • ????????????????if(line.contains(processName)){??
  • ????????????????????return?true;??
  • ????????????????}??
  • ????????????}??
  • ??????????????
  • ????????????return?false;??
  • ????????}catch(Exception?e){??
  • ????????????e.printStackTrace();??
  • ????????????return?false;??
  • ????????}finally{??
  • ????????????if(br!=null){??
  • ????????????????try{??
  • ????????????????????br.close();??
  • ????????????????}catch(Exception?ex){??
  • ????????????????}??
  • ????????????}??
  • ??????????????
  • ????????}??
  • ????}??
  • }??

    ?

    ?其它的用法可以参考JDK文档,这里就不一一举例,毕竟它用得不多。

    ?

    转自http://blog.csdn.net/turkeyzhou/article/details/2323269

  • 读书人网 >编程

    热点推荐