zoukankan      html  css  js  c++  java
  • 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();
                                  }
                        }

             }

  • 相关阅读:
    python3中模块初识
    Django 应用程序 + 模型 + 基本数据访问
    Axure文本框验证和外部url的调用
    MATLAB 实时脚本(live-script)使用
    Django MTV 开发模式 + 数据库配置
    Django 模板继承
    Django 修改视图文件(views.py)并加载Django模块 + 利用render_to_response()简化加载模块 +locals()
    Django Context对象 + 过滤器 + 标签
    Axure 页面内多组内容切换的实现 + 利用一个内联框架实现百度地图访问
    MATLAB绘图功能(2) 二维底层绘图修饰
  • 原文地址:https://www.cnblogs.com/nieliangcai/p/8601900.html
Copyright © 2011-2022 走看看