没有输出到Java Process的标准输出

问题描述 投票:0回答:1

我正在执行java进程的mysql.exe(在Windows 10,JDK 1.8环境中),代码如下:

String command="D:\\SW\\MySQL_8\\bin\\mysql.exe --user=root --password=password";

ProcessBuilder pBuilder=new ProcessBuilder(command.split(" "));
pBuilder=pBuilder.redirectOutput(new File("D:\\workspaces\\cas\\CASCAppliance_Common\\out\\out.txt"));
pBuilder=pBuilder.redirectError(new File("D:\\workspaces\\cas\\CASCAppliance_Common\\out\\err.txt"));

Process p=pBuilder.start();
OutputStream os=p.getOutputStream();
PrintWriter writer=new PrintWriter(os);

如果我从命令行执行mysql.exe,我得到以下输出:

Microsoft Windows [Version 10.0.17134.228]
(c) 2018 Microsoft Corporation. All rights reserved.

D:\SW>cd MySQL_8\bin

D:\SW\MySQL_8\bin>mysql --user=root --password=password
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.11 MySQL Community Server - GPL

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

我试图在错误流重定向后从进程中获取输入流,我只得到以下内容:

mysql: [Warning] Using a password on the command line interface can be insecure.

我也尝试将err和std重定向到文件,仍然错误的文件包含上面的文本,而std out文件什么也没包含。

另一方面,如果我执行cmd.exe并运行命令,比如'dir',我将从进程的InputStream获取所需的目录列表。

我的问题是,我做错了什么,或者mysql.exe的执行方式是不可能从进程的输入流中获取上述输出。

java inputstream processbuilder
1个回答
1
投票

是的,这是关于mysql的。 (编辑是来自@ dave_thompson_085的更新)

如果一个人从交互式shell运行mysql但重定向输入和/或输出,它会抑制横幅,提示,表拳击和'N行(X秒)'预告片。

请注意,即使没有提示,它仍会读取SQL语句并执行它们,并显示任何结果集 - 尽管如果输出是管道,它会缓冲。相反,如果在一个终端上有stdin和stdout的Java进程中,运行mysql继承那些,它具有完整的交互行为。

© www.soinside.com 2019 - 2024. All rights reserved.