为什么我的ProcessBuilder输入流产生的数据与Linux命令行中的数据不同?

huangapple 未分类评论51阅读模式
英文:

Why does my ProcessBuilder inputStream result in different data than from linux command line

问题

以下是您要翻译的部分:

  1. 当我从我的Ubuntu 18.4命令行运行以下命令时:
  2. sudo ./nuvgpio 47 1 > gpioresult-47.txt
  3. 生成的文件包含以下数据:
  4. 尝试 4E
  5. ChipID_Hi=0xD2ChipID_Lo=0x82
  6. 2GP48,数据=0x00
  7. 当我从Java运行该过程时,结果流中不包含最后一行。
  8. import java.io.*;
  9. /**
  10. * 运行命令行程序(nuvgpio)以读取/写入Jetway SBC上的GPIO引脚的状态。
  11. */
  12. public class JetwayIODriver {
  13. private static int LED_PIN_NUMBER = 40;
  14. private static int COVER_PIN_NUMBER = 42;
  15. public static void main(String args[]) throws InterruptedException {
  16. try {
  17. String directoryStr = "/home/tester2/IdeaProjects/automation-framework/device-common-parent/device-io/src/";
  18. File dir = new File(directoryStr);
  19. ProcessBuilder builder = new ProcessBuilder();
  20. builder.directory(dir);
  21. builder.redirectErrorStream();
  22. int mode = 1; // 1=read, 0=write
  23. builder.command("/bin/bash", "-c", "echo 'tester2' | sudo -S ./nuvgpio", Integer.toString(COVER_PIN_NUMBER), Integer.toString(mode));
  24. Process process = builder.start();
  25. InputStreamReader input = new InputStreamReader(process.getInputStream());
  26. BufferedReader reader = new BufferedReader(input);
  27. String line;
  28. while ((line = reader.readLine()) != null) {
  29. System.out.println ("Stdout: " + line);
  30. }
  31. int exitCode = process.waitFor();
  32. input.close();
  33. System.out.println(String.format("GPIO进程已完成,退出代码:%s", exitCode));
  34. } catch (FileNotFoundException fnf) {
  35. System.out.println(fnf.getMessage());
  36. } catch (Throwable t) {
  37. System.out.println(t.getMessage());
  38. }
  39. }
  40. }
  41. Java输出:
  42. Stdout: 尝试 4E
  43. Stdout: ChipID_Hi=0xD2ChipID_Lo=0x82
  44. GPIO进程已完成,退出代码:0
  45. 值得注意的一件奇怪的事情是:当我从命令行运行并输出到控制台时,第3行文本位于新提示的同一行上:
  46. nuvgpio程序似乎没有在末尾插入换行符。这可能是原因吗?如果是的话,我可以将其注入到进程输出中吗?

希望这对您有所帮助。

英文:

When I run the following command from my Ubuntu 18.4 command line:

  1. sudo ./nuvgpio 47 1 > gpioresult-47.txt

The resulting file has the following data:

  1. Try 4E
  2. ChipID_Hi=0xD2, ChipID_Lo=0x82.
  3. 2, GP48, data=0x00

When I run the process from Java, the result stream does not contain the last line.

  1. import java.io.*;
  2. /**
  3. * Run a command line program (nuvgpio) to read/write the status of GPIO pins on a Jetway SBC.
  4. */
  5. public class JetwayIODriver {
  6. private static int LED_PIN_NUMBER = 40;
  7. private static int COVER_PIN_NUMBER = 42;
  8. public static void main(String args[]) throws InterruptedException {
  9. try {
  10. String directoryStr = "/home/tester2/IdeaProjects/automation-framework/device-common-parent/device-io/src/";
  11. File dir = new File(directoryStr);
  12. ProcessBuilder builder = new ProcessBuilder();
  13. builder.directory(dir);
  14. builder.redirectErrorStream();
  15. int mode = 1; // 1=read, 0=write
  16. builder.command("/bin/bash", "-c", "echo 'tester2' | sudo -S ./nuvgpio", Integer.toString(COVER_PIN_NUMBER), Integer.toString(mode));
  17. Process process = builder.start();
  18. InputStreamReader input = new InputStreamReader(process.getInputStream());
  19. BufferedReader reader = new BufferedReader(input);
  20. String line;
  21. while ((line = reader.readLine()) != null) {
  22. System.out.println ("Stdout: " + line);
  23. }
  24. int exitCode = process.waitFor();
  25. input.close();
  26. System.out.println(String.format("GPIO process finished with exit code : %s", exitCode));
  27. } catch (FileNotFoundException fnf) {
  28. System.out.println(fnf.getMessage());
  29. } catch (Throwable t) {
  30. System.out.println(t.getMessage());
  31. }
  32. }
  33. }

Output from Java:

  1. Stdout: Try 4E
  2. Stdout: ChipID_Hi=0xD2, ChipID_Lo=0x82.
  3. GPIO process finished with exit code : 0

One odd thing of note: When I run from the command line and output to the console, the 3rd line of text is on the same line as the new prompt:
为什么我的ProcessBuilder输入流产生的数据与Linux命令行中的数据不同?

The nuvgpio program seems to be not putting a line break at the end. Could this be the cause and if so, can I inject one into the process output?

huangapple
  • 本文由 发表于 2020年4月6日 04:47:44
  • 转载请务必保留本文链接:https://java.coder-hub.com/61049297.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定