我正在尝试将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()));
任何帮助都会很棒,