java - 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?似乎它只是为每个命令做新的东西。
我最后一句话的意思是我使用第一个命令登录,然后它转到另一个启动守护进程等。但它似乎对每个调用都是全新的(也就是没有记录在触发第二个命令时,等等......)
解决方案
推荐阅读
- python - 使用python从网站下载书籍
- laravel - 在新的 Laravel/Livewire 安装中,得到一个 RuntimeException :尚未设置外观根
- vue.js - 如何在 Nuxt 中定义全局变量以在任何地方访问
- amazon-s3 - Shopify 订单图片上传到 S3
- python - 是否可以通过 ros/python 使凉亭中的模型不可见?
- java - 我在哪里可以找到负责 HyperSQL 引擎级别 SQL 查询执行的代码?
- c++ - 我有一个关于 c++ 的问题
图书馆? - ios - 在 IOS 中使用 Floaty 创建浮动操作按钮 (FAB)
- android - java.lang.ClassCastException androidx.viewpager.widget.Viewpager 无法转换为 CustomPager
- javascript - 在 discord.js 上嵌入 ping 命令