Java调用系统工具

 
 
  1. windows:

    [java]  view plain  copy
    1. Runtime.getRuntime().exec("explorer 文件路径“);  

    Linux:

    [java]  view plain  copy
    1. Runtime.getRuntime().exec("nautilus 文件路径“);  
    第二种(windows和Linux同一):

    [java]  view plain  copy
    1. Desktop.getDesktop().open(new File("文件路径"));  



  2. Java调用cmd命令,并输出显示信息:  
  3. package com.anxin.cmd.test;   
  4.     
  5. import java.io.BufferedReader;   
  6. import java.io.InputStreamReader;   
  7.    
  8. public class Command {   
  9.     
  10.  public static void main(String[] args) {   
  11.   try {   
  12.    Runtime rt = Runtime.getRuntime();   
  13.    Process pr = rt.exec("cmd /c dir"); // cmd /c calc   
  14.    // Process pr = rt.exec("D:\\xunlei\\project.aspx");   
  15.     
  16.    BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream(), "GBK"));   
  17.     
  18.    String line = null;   
  19.     
  20.    while ((line = input.readLine()) != null) {   
  21.     System.out.println(line);   
  22.    }   
  23.     
  24.    int exitVal = pr.waitFor();   
  25.    System.out.println("Exited with error code " + exitVal);   
  26.     
  27.   } catch (Exception e) {   
  28.    System.out.println(e.toString());   
  29.    e.printStackTrace();   
  30.   }   
  31.  }   
  32. }   
  33. //Java启动本机应用程序EXE的三种方式:  
  34. //第一种方式:利用cmd方式   
  35. /**   
  36.  * 执行cmd命令   
  37.  *   
  38.  * @param command   
  39.  * @throws IOException   
  40.  */  
  41. public static String executeCmd(String command) throws IOException {   
  42.  log.info("Execute command : " + command);   
  43.  Runtime runtime = Runtime.getRuntime();   
  44.  Process process = runtime.exec("cmd /c " + command);   
  45.  BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream(), "UTF-8"));   
  46.  String line = null;   
  47.  StringBuilder build = new StringBuilder();   
  48.  while ((line = br.readLine()) != null) {   
  49.   log.info(line);   
  50.   build.append(line);   
  51.  }   
  52.  return build.toString();   
  53. }   
  54.     
  55. executeCmd(start "AXAdWebBrowser" "D:\AXAdsBrowser\AXAdWebBrowser.exe");   
  56. //第二种方式:利用ProcessBuilder调用cmd方式  
  57. /**   
  58.  * 启动应用程序   
  59.  *   
  60.  * @param programName   
  61.  * @return   
  62.  * @throws IOException   
  63.  */  
  64. public static void startProgram(String programPath) throws IOException {   
  65.  log.info("启动应用程序:" + programPath);   
  66.  if (StringUtils.isNotBlank(programPath)) {   
  67.   try {   
  68.    String programName = programPath.substring(programPath.lastIndexOf("/") + 1, programPath.lastIndexOf("."));   
  69.    List<String> list = new ArrayList<String>();   
  70.    list.add("cmd.exe");   
  71.    list.add("/c");   
  72.    list.add("start");   
  73.    list.add("\"" + programName + "\"");   
  74.    list.add("\"" + programPath + "\"");   
  75.    ProcessBuilder pBuilder = new ProcessBuilder(list);   
  76.    pBuilder.start();   
  77.   } catch (Exception e) {   
  78.    e.printStackTrace();   
  79.    log.error("应用程序:" + programPath + "不存在!");   
  80.   }   
  81.  }   
  82. }   
  83. //第三种方式:使用Desktop启动应用程序    
  84. /**   
  85.  * 启动应用程序   
  86.  *   
  87.  * @param programName   
  88.  * @return   
  89.  * @throws IOException   
  90.  */  
  91. public static void startProgram(String programPath) throws IOException {   
  92.  log.info("启动应用程序:" + programPath);   
  93.  if (StringUtils.isNotBlank(programPath)) {   
  94.   try {   
  95.    Desktop.getDesktop().open(new File(programPath));   
  96.   } catch (Exception e) {   
  97.    e.printStackTrace();   
  98.    log.error("应用程序:" + programPath + "不存在!");   
  99.   }   
  100.  }   
  101. }  

猜你喜欢

转载自blog.csdn.net/qq3892997/article/details/80652781