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

我正在尝试将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          Set trace level to .

-x, --execute              Execute script .

-p, --port         Communicate with device using .

在控制台中,我尝试将第一个(非工作)命令的输出重定向到文本文件,文本文件显示预期的错误无串行端口,但仅在使用以下命令时:"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()));

任何帮助都会很棒,

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值