java 執行 exe 文件


 https://blog.csdn.net/u013256816/article/details/54603910

主要點:

1. Runtime.getRuntime().exec(cmd); 這個方法最終調用的是 ProcessBuilder 的方法執行的命令, 所以 ProcessBuilder 比 Process 提供了更多的方法用於對進程的控制

2. cmd 命令執行 .exe 如何傳遞參數

3. 執行.exe 文件, 但是cmd 窗口隱藏,不顯示

4. 如何監聽cmd命令執行 .exe 文件結束

 

package com.example.hostinfo.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class CalabashUtil {
public static Logger logger = LoggerFactory.getLogger(CalabashUtil.class);

public static String path = "tools\\Client\\Debug\\";

private static String program = "Calabash.Plugin.ConsoleApp.exe";

private static String server = "tools\\Server\\Calabash.Business.Daemon.Host.exe";

public static boolean getInfo(String platform, String roomNum) throws IOException {
    try {
        String programName = program.substring(program.lastIndexOf("/") + 1, program.lastIndexOf("."));
        List<String> list = new ArrayList<String>();
        list.add("cmd.exe");
        list.add("/c");
        list.add("start");
        list.add("\"" + programName + "\"");
        list.add(program);

        //參數 platform
        list.add(platform);

        //參數 roomNum
        list.add(roomNum);

        ProcessBuilder pBuilder = new ProcessBuilder(list);

        //類似 cd path
        pBuilder.directory(new File(path));
        pBuilder.start();
        return true;
    } catch (Exception e) {
        return false;
    }
}

public static void main(String[] args) throws IOException, InterruptedException {
    server();
    Thread.sleep(1000*5);
    getInfo("Douyu", "5720533");

// getInfo("Douyu", "1011");
// server();
}

public static boolean server() throws IOException {
    try {
        String programName = program.substring(program.lastIndexOf("/") + 1, program.lastIndexOf("."));
        List<String> list = new ArrayList<String>();
        list.add("cmd.exe");
        list.add("/c");
        list.add("start");
        list.add("\"" + programName + "\"");
        list.add(server);
        ProcessBuilder pBuilder = new ProcessBuilder(list);
        pBuilder.start();
        return true;
    } catch (Exception e) {
        return false;
    }
}

}

封裝java調用命令行, 多命令

    public static List<String> executeNewFlow(List<String> commands) {
        List<String> rspList = new ArrayList<String>();
        Runtime run = Runtime.getRuntime();
        try {
            Process proc = run.exec("/bin/bash", null, null);
            BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
            PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(proc.getOutputStream())), true);
            for (String line : commands) {
                out.println(line);
            }
            out.println("exit");// 這個命令必須執行,否則in流不結束。
            String rspLine = "";
            while ((rspLine = in.readLine()) != null) {
                System.out.println(rspLine);
                rspList.add(rspLine);
            }
            proc.waitFor();
            in.close();
            out.close();
            proc.destroy();
        } catch (IOException e1) {
            e1.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return rspList;
    }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM