【发布时间】:2019-05-12 21:45:40
【问题描述】:
我正在尝试将 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 internalflash
Here 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.1
Copyright 2015-2017 ATMEL Corporation
Usage: 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>.
-d, --device <device[:options:...]> Connected device is <device>.
-b, --board <board[:options:...]> Connected board is <board>.
-m, --monitor <command[:options:...]> Run monitor command <command>.
-a, --applet <applet[:options:...]> Load and initialize applet <applet>.
-c, --command <command[:args:...]> Run command <command>.
在控制台中,我尝试将第一个(非工作)命令的输出重定向到一个文本文件,该文本文件显示预期的 Error no serial ports,但前提是使用以下命令:
"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()));
// read the output from the command
System.out.println("Here is the standard output of the command:\n");
String s = null;
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
// read any errors from the attempted command
System.out.println("Here is the standard error of the command (if any):\n");
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
任何帮助都会很棒,
贾德
更新:正如建议我现在使用 ProcessBuilder 并删除了引号,但结果完全相同。
建议添加Redirect.INHERIT,如图所示,但它会产生以下输出。好像只是绕过输入流直接进入终端?
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.BufferedReader;
public class ProcessDemo {
public static void main(String[] args) {
try
{
// 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", "-c", "write:\"GCCBoardProject1.bin\""};
ProcessBuilder pb = new ProcessBuilder(command);
pb.redirectOutput(ProcessBuilder.Redirect.INHERIT);
pb.redirectError(ProcessBuilder.Redirect.INHERIT);
Process proc = pb.start();
proc.waitFor();
BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
System.out.println("Here is the standard output of the command:\n");
// read the output from the command
String s = null;
while ((s = stdInput.readLine()) != null)
{
System.out.println(s);
}
// read any errors from the attempted command
System.out.println("Here is the standard error of the command (if any):\n");
while ((s = stdError.readLine()) != null)
{
System.out.println(s);
}
System.out.println("return value: " + proc.exitValue());
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
输出:
Error: No serial ports found
Here is the standard output of the command:
Here is the standard error of the command (if any):
return value: -1
【问题讨论】:
-
也许this 会有所帮助?
标签: java inputstream