简体   繁体   English

与命令行过程进行交互

[英]Interacting with a command line process

I need to interact with a command line process, eg diskpart on windows. 我需要与命令行进程进行交互,例如Windows上的diskpart。 The problem: input.readLine() in the following sample leads to a blocking while. 问题:以下示例中的input.readLine()导致while阻塞。

public static void main(String[] args) throws IOException 
{
    ProcessBuilder processBuilder = new ProcessBuilder("C:\\Windows\\system32\\diskpart.exe");                      
    processBuilder.redirectErrorStream(true);
    Process process = processBuilder.start();

    input = new BufferedReader(new InputStreamReader(process.getInputStream()));
    output = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));

    // read #1 code position
    String line = null;
    while((line = input.readLine())!= null)
        System.out.println(line);

    // code position #2
    System.out.println("This line is never executed");   

    output.write("list disk" + System.lineSeparator());
    output.flush(); // important
}

The output (from read #1 code position) is 输出(从读取的#1代码位置开始)为

Microsoft DiskPart-Version 6.1.7601
Copyright (C) 1999-2008 Microsoft Corporation.
Auf Computer: MYPC

This is correct, however after that nothing happens, eg code position #2 这是正确的,但是之后什么也没发生,例如代码位置2

System.out.println("This line is never executed"); 

is never reached. 永远不会到达。 Can anyone tell me, why and how to fix this? 谁能告诉我,为什么以及如何解决? Thanks! 谢谢!

Update: 更新:

Trying to read byte by byte also seems not to work? 尝试逐字节读取似乎也不起作用? ): ):

    InputStreamReader input = new InputStreamReader(process.getInputStream());
    int mychar = -1;

    while((mychar = input.read()) != -1)
        System.out.println(mychar);

    System.out.println("This line is never executed"); 

Because the next thing Diskpart does is show the prompt, which doesn't include a newline: 因为Diskpart接下来要做的是显示提示,其中不包含换行符:

Microsoft DiskPart version 6.1.7601
Copyright (C) 1999-2008 Microsoft Corporation.
On computer: PCNAME

DISKPART> _

So your code sits there waiting for the newline, which never appears. 因此,您的代码坐在那里等待换行符,该行永远不会出现。

You need to change your code to send the "list disk" command at the right time. 您需要更改代码以在正确的时间发送“列表磁盘”命令。

Diskpart has an interactive console that requires input from the user. Diskpart具有一个交互式控制台,需要用户输入。 Attempting to read its output like this: 尝试像这样读取其输出:

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

will cause you to wait indefinitely as the application itself requires input. 将导致您无限期地等待,因为应用程序本身需要输入。

You need to wait for input first from the windows command so you need to add CMD /C to your command. 您需要首先等待Windows命令的输入,因此需要将CMD /C添加到命令中。

As diskpart is interactive, you could try running your list command as a script, so you would have instead: 由于diskpart是交互式的,因此您可以尝试将list命令作为脚本运行,因此,您需要:

String[] command = {"CMD", "/C", "C:\\Windows\\system32\\diskpart.exe", "/s", "diskpart.txt"};
ProcessBuilder processBuilder = new ProcessBuilder(command); 

with diskpart.txt containing: diskpart.txt包含:

list disk

I recommend you getting this working in a standard batch file first though to check that the output is correct. 我建议您先在标准的批处理文件中进行此操作,但要检查输出是否正确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM