[英]How to capture the inputstream while executing cmd through java?
I have return a small java code to execute the SC command on Remote Server and I am using LSRunas to give the admin credentials. 我返回了一个小的Java代码来在远程服务器上执行SC命令,并且我正在使用LSRunas提供管理员凭据。
String[] command = new String [3];
command[0] = 'cmd';
command[1] = "/c";
command[2] = "lsrunas /user:username /password:password /domain:domain /command:"sc \\servername queryex" /runpath:c\\"
ProcessBulder buider = new ProcessBuilder(command);
Process p = buider.start();
BufferedReader reader = new BufferedReader(new(InputStreamReader(p.getInputStrem()));
String line = reader.readLine();
while(line != null) {
System.out.println(line);
line = reader.readLine();
}
The program works fine and it execute the SC command with provided credentials on remote server, but the problem is that it opens a new cmd windows to print the result (The common scenario while using RUNAS), But i want to capture the result and read it in my java program. 该程序运行良好,并在远程服务器上使用提供的凭据执行SC命令,但问题是它打开了一个新的cmd窗口以打印结果(使用RUNAS时的常见情况),但是我想捕获结果并阅读它在我的Java程序中。 Is there is any way i can capture the result from newly opened cmd window.
有什么办法可以从新打开的cmd窗口捕获结果。
String line = reader.readLine();
while(line != null) {
System.out.println(line);
}
This will just keep printing out the same line. 这只会继续打印同一行。 Try:
尝试:
String line;
while ((line = reader.readLine()) != null)
{
System.out.println(line);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.