首页 > 解决方案 > Java ProcessBuilder 每次调用新控制台?

问题描述

嘿,所有人都不确定我的情况是否有解决方案,但这里有。

我正在发送一些 9 cmd.exe 命令:

static String startDaemon_2             = "scm daemon start"; //Start Daemon
static String isRQRunning_3             = "RQAdapter.bat status"; //Check if RQ is running
static String startRQ_4                 = "RQAdapter.bat start"; //Start RQ
...etc etc....

这是我触发命令的主要代码:

static ProcessBuilder processBuilder    = new ProcessBuilder();
static Process process                  = null;
static BufferedReader reader            = null;

public static Object steps(String cmds, String dir_) {
    processBuilder.command("cmd.exe", "/c", cmds);
    processBuilder.directory(new File(dir_));

    try {
        process     = processBuilder.start();
        reader      = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line = "";

        while ((line = reader.readLine()) != null) {
            System.out.println(line);

            if (line.contains("Key:")) {
                return true;
            } else if (line.contains("successfully")) {
                return true;                                
            } else if (line.contains("background")) {
                return true;
            } else if (line.contains("not")) {
                //Load the adapter then
                return "startRQM_4"; 
            } else if (line.contains("sandbox")) {
                return true;
            } else if (line.contains("Misfit")) {
                return true;
            // repair goes here
            } else if (line.contains("Unresolved")) {
                //at this point theres no need to carry on. theres a conflick
                return "fix";
            } 
        }

        int exitCode = process.waitFor();
        System.out.println(line);

        if (exitCode == 0) {
            return true;
        } else {
            return false;
        }
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    } catch (InterruptedException e) {
        e.printStackTrace();
        return false;
    }        
}

我这样称呼这个函数:

result = steps(startDaemon_2, scmDir);
if (result == "false") { System.out.println("not true"); }

result = steps(isRQMRunning_3, scmDir);
etc..... etc......

当我在实际的 cmd 窗口中关闭这些相同的命令时,它工作得很好。做它应该做的。但是,当 java 程序执行完全相同的操作时,即使没有错误,它似乎也无法正常工作。

然后我开始想,也许我需要定义ProcessBuilder, process, reader

但它似乎仍然无法正常工作并且没有错误。

是否有其他类型的方法可以在同一设置中执行一些 cmd.exe?似乎它只是为每个命令做新的东西。

我最后一句话的意思是我使用第一个命令登录,然后它转到另一个启动守护进程等。但它似乎对每个调用都是全新的(也就是没有记录在触发第二个命令时,等等......)

标签: javacmdprocessprocessbuilder

解决方案


推荐阅读