为了账号安全,请及时绑定邮箱和手机立即绑定

无法使用Java中的STDIN和STDERR读取程序输出

无法使用Java中的STDIN和STDERR读取程序输出

繁花如伊 2019-04-10 14:11:04
我正在尝试将sam-ba.exe(通过USB将代码加载到ARM微控制器)的输出读入我的Java程序。在命令提示符下编写此命令时:"sam-ba_3.2.1\sam-ba.exe" -p usb -d SAME70 -a internalflash结果是:Error: No serial ports found但是,在以下Java代码中执行此命令时,标准输出或标准错误流都不会返回任何内容(请参阅以下内容):Command executed: "sam-ba_3.2.1\sam-ba.exe" -p usb -d SAME70 -a internalflashHere is the standard output of the command: Here is the standard error of the command (if any):令它更烦人的是在Java代码中替换此命令将返回stderr流中的信息:"sam-ba_3.2.1\sam-ba.exe" --help结果:Here is the standard output of the command:Here is the standard error of the command (if any):SAM-BA Command Line Tool v3.2.1Copyright  2015-2017 ATMEL CorporationUsage: sam-ba_3.2.1\sam-ba.exe [options]Options:   -v, --version                          Displays version information.   -h, --help                             Displays this help.   -t, --tracelevel <trace_level>         Set trace level to <trace_level>.   -x, --execute <script.qml>             Execute script <script-file>.   -p, --port <port[:options:...]>        Communicate with device using <port>.在控制台中,我尝试将第一个(非工作)命令的输出重定向到文本文件,文本文件显示预期的错误无串行端口,但仅在使用以下命令时:"sam-ba_3.2.1\sam-ba.exe" -p usb -d SAME70 -a internalflash > output.txt 2>&1这让我相信消息来自stderr,因为如果我省略“2>&1”,则文本文件为空。但那么为什么Java程序中的stderr是空白的呢?这是我的Java代码:import java.io.IOException;import java.io.InputStreamReader;import java.io.InputStream;import java.io.BufferedReader; public class ProcessDemo {    public static void main(String[] args) {         try         {             Runtime r = Runtime.getRuntime();             //String command = "\"sam-ba_3.2.1\\sam-ba.exe\" --help";             String command = "\"sam-ba_3.2.1\\sam-ba.exe\" -p usb -d SAME70 -a internalflash";             System.out.println("Command executed: " + command);             Process proc = r.exec(command);             BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream()));             BufferedReader stdError = new BufferedReader(new InputStreamReader(proc.getErrorStream()));任何帮助都会很棒,
查看完整描述

2 回答

?
收到一只叮咚

TA贡献1821条经验 获得超4个赞

proc.waitFor(); 此语句应出现在BufferedReader语句之前,并在输入流阅读器中传递。注意:Process.waitFor()方法暂停当前线程,直到您的进程有机会完成。然后使用BufferReader可以读取输出。

参考:在JAVA中捕获外部程序的输出


查看完整回答
反对 回复 2019-05-15
?
慕尼黑的夜晚无繁华

TA贡献1864条经验 获得超6个赞

当我在你的程序的进程命令行args中使用mysqld start时,问题似乎是你传递的命令。我能够看到ouptut。所以问题在于您传递的CLI命令。建议手动运行它并查看打印输出。


String[] command = {"C:\\Users\\userid\\Downloads\\mysql-5.7.13-winx64\\mysql-5.7.13-winx64\\bin\\mysqld.exe", "--console"};  

样本输出:


    C:\Users\userid\other\stk-overflow>java ProcessDemo




   2019-05-13T00:57:04.191518Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).

    2019-05-13T00:57:04.191518Z 0 [Warning] 'NO_AUTO_CREATE_USER' sql mode was not set.

    2019-05-13T00:57:04.191518Z 0 [Warning] Insecure configuration for --secure-file-priv: Current value does not restrict location of generated files. Consider setting it to a valid, non-empty path.

    2019-05-13T00:57:04.191518Z 0 [Note] C:\Users\userid\Downloads\mysql-5.7.13-winx64\mysql-5.7.13-winx64\bin\mysqld.exe (mysqld 5.7.13) starting as process 21604 ...

    2019-05-13T00:57:04.201520Z 0 [Note] InnoDB: Mutexes and rw_locks use Windows interlocked functions

    2019-05-13T00:57:04.202742Z 0 [Note] InnoDB: Uses event mutexes

    2019-05-13T00:57:04.203516Z 0 [Note] InnoDB: _mm_lfence() and _mm_sfence() are used for memory barrier

    2019-05-13T00:57:04.203516Z 0 [Note] InnoDB: Compressed tables use zlib 1.2.3

    2019-05-13T00:57:04.204517Z 0 [Note] InnoDB: Number of pools: 1

    2019-05-13T00:57:04.205519Z 0 [Note] InnoDB: Not using CPU crc32 instructions

    2019-05-13T00:57:04.209518Z 0 [Note] InnoDB: Initializing buffer pool, total size = 128M, instances = 1, chunk size = 128M


查看完整回答
反对 回复 2019-05-15
  • 2 回答
  • 0 关注
  • 717 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号