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