Runtime.getRuntime.exec();

 

杀死Chrome浏览器进程

private static void closeAllChrome() throws IOException{
Runtime.getRuntime().exec("taskkill /F /IM chrome.exe");
Runtime.getRuntime().exec("taskkill /F /IM chromedriver.exe");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

Runtime类中exec方法有很多用处,特此总结一下:

         import java.io.IOException;

         public class Test {

                     public void copyFile() {//拷贝文件
                                 try {
                                          String fromPath="E:\\test";
                                          String toPath="D:\\test";
                                          Runtime.getRuntime().exec("cmd /c start xcopy /s/e \""+fromPath+"\" \""+toPath+"\"");//xcopy:把指定的目录连文件和目录结构一并拷贝,但不能拷贝隐藏文件和系统文件;/s:;/e:复制目录和子目录,包括空的。
                                 } catch (Exception e) {
                                         e.printStackTrace();
                                 }
                       }

 

                       public void removeFile() {//DOS命令的方式删除指定路径的子目录
                                 try {
                                          String path="E:\\test";
                                          Runtime.getRuntime().exec("cmd /c start rd /s /Q \""+path+"\"");//rd:删除指定路径的子目录;/S:除目录本身外,还将删除指定目录下的所有子目录和文件;/Q:安静模式,带 /S 删除目录树时不要求确认。
                                  } catch (Exception e) {
                                          e.printStackTrace();
                                  }
                       }

                         public void openWebPage() {//打开网页
                                  try {
                                          String http="http://www.baidu.com/";
                                          Runtime.getRuntime().exec("cmd /c start "+http);
                                  } catch (Exception e) {
                                          e.printStackTrace();
                                  }
                       }
 
                      public void openExe1() {//命令运行可执行文件(扩展名为.exe的文件)
                                 try {
                                         Runtime.getRuntime().exec("cmd /c start cmd.exe");
                                 } catch (Exception e) {
                                        e.printStackTrace();
                                 }
                      }

                     public void openExe2() {//运行可执行文件(扩展名为.exe的文件)
                                try {
                                       Runtime runtime = Runtime.getRuntime();
                                       runtime.exec("NOTEPAD.EXE");
                                       //或runtime.exec("notepad.exe");
                                       //或runtime.exec("notepad");
                                       //或runtime.exec("NOTEPAD");
                               } catch (IOException e){
                                      e.getMessage();
                               }
                      }
 

                     public void openBat() {//运行批处理文件(扩展名为.bat的文件)
                              try {
                                      String path="C:\\ProgramFiles\\Tomcat\\Tomcat6.0\\bin\\";
                                      String batName="startup";
                                      Runtime.getRuntime().exec("cmd /c start /D \""+path+"\" "+batName+".bat");
                               } catch (Exception e) {
                                      e.printStackTrace();
                               }
                     }

                     public void closeBrowser() {//关闭浏览器
                              try {
                                      Runtime.getRuntime().exec("taskkill /F /IM chrome.exe");
                                      Runtime.getRuntime().exec("taskkill /F /IM iexplore.exe");
                                      Runtime.getRuntime().exec("taskkill /F /IM firefox.exe");
                                      Runtime.getRuntime().exec("taskkill /F /IM safari.exe");
                                      Runtime.getRuntime().exec("taskkill /F /IM opera.exe");
                              } catch (IOException e) {
                                      e.printStackTrace();
                              }
                    }

         }

posted @ 2018-03-19 14:37  小心走火  阅读(831)  评论(0编辑  收藏  举报