简体   繁体   English

将程序输出重定向为输入-管道不起作用

[英]Redirecting program output as input - pipe not working

I am taking the output of one program (C++) as input for another (Java) using BufferedReader . 我正在使用BufferedReader将一个程序(C ++)的输出作为另一个(Java)的输入。 When I run the Java program in my IDE and enter input in the console the program works as expected, however, when I try to pipe the input from the C++ program it no longer works with no errors given - no output is shown on the terminal window and no data is entered into the database. 当我在IDE中运行Java程序并在控制台中输入输入时,该程序将按预期运行,但是,当我尝试从C ++程序中传递输入时,它将不再起作用且没有给出错误-终端上未显示任何输出窗口,并且没有数据输入数据库。 It is also worth noting that if I try running the jar in a terminal window and entering input the program does not work there either. 还值得注意的是,如果我尝试在终端窗口中运行jar并输入输入,则该程序也将无法运行。

Reader Code: 读者代码:

BufferedReader input;
      try {
        input = new BufferedReader(new InputStreamReader(System.in));
        String outputLine;
        String visionObjectName;
        String visionObjectTimestamp;
        String word = null;
        String timestamp = null;
        String whiteSpace = null;
        // Regex is used to check that the output is an object "name blankSpace timestamp" to avoid random output such as errors being entered.
        while ((outputLine = input.readLine()) != null) {
          System.out.println(outputLine);
          String regEx = "(^[a-zA-Z]*)(\\s+)((?:2|1)\\d{3}(?:-|\\/)(?:(?:0[1-9])|(?:1[0-2]))(?:-|\\/)(?:(?:0[1-9])|(?:[1-2][0-9])|(?:3[0-1]))(?:T|\\s)(?:(?:[0-1][0-9])|(?:2[0-3])):(?:[0-5][0-9]):(?:[0-5][0-9]))";
          Pattern p = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
          Matcher m = p.matcher(outputLine);
          if (m.find()) {
            word = m.group(1);
            whiteSpace = m.group(2);
            timestamp = m.group(3);
          }
          visionObjectName = word + "" + whiteSpace;
          visionObjectTimestamp = timestamp;
          databasePopulation(visionObjectName, visionObjectTimestamp);
        }

I am using the Linux terminals pipe function to pass the information across. 我正在使用Linux终端管道功能来传递信息。

Command: 命令:

./ORBMarkerDetection | java -jar ../layers/out/artifacts/layers_jar/layers.jar

EDIT: For sake of thoroughness I've included the output code from the C++ program, would the method I'm using to print the timestamp be causing problems? 编辑:为彻底起见,我已经包括了C ++程序的输出代码,我用来打印时间戳的方法会引起问题吗?

int timeFound() {
  time_t rawtime;
  struct tm * timeinfo;
  char buffer [80];
  time(&rawtime);
  timeinfo = localtime (&rawtime);
  strftime(buffer,80,"%Y-%m-%d %H:%M:%S",timeinfo);
  puts(buffer);
  return 0;
}

void printFunction(String objectName) {
  if(nameArray[0] != objectName) {
    nameArray[0] = objectName;
    cout << nameArray[0] << " ";
    timeFound();
    cout << flush;
  }
}

As asked in comments, this is an example of the C++ output (from the line Aug 17.... to opengl support available are only printed at program start and are not printed again. 如注释中所要求的,这是C ++输出的示例(从Aug 17....opengl support available仅在程序启动时打印,不再打印。

Aug 17, 2015 11:57:03 AM com.layers.Main main
INFO: Logger Created.
Please enter activity number: 
1.1
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
init done 
opengl support available 
tap 2015-08-17 11:57:07
kitchenDoor 2015-08-17 12:57:07
fridge 2015-08-17 13:57:07

Also if I comment out the line beginning to the end of the scope: 另外,如果我注释掉范围末尾的那一行:

String regEx...
...
}

The Java program outputs as expected to the terminal but I've had to comment out the database upload to test this so I cannot verify if the upload works. Java程序会按预期输出到终端,但是我不得不注释掉数据库上载以进行测试,因此我无法验证上载是否有效。

My guess would be that the your program is blocked in something unrelated to the pipe. 我的猜测是您的程序被阻塞在与管道无关的东西中。 Most likely the database. 最有可能是数据库。 The IDE could be setting a classpath and/or VM options that differ from what you enter at the terminal. IDE可能设置的类路径和/或VM选项与您在终端上输入的不同。

Get the exact command line being run by the IDE, if you don't know how to do this with your IDE you can do it with jps and /proc//cmdline. 获取由IDE运行的确切命令行,如果您不知道如何使用IDE执行此操作,则可以使用jps和/ proc // cmdline进行操作。

eg. 例如。 if I run jps while I am running my program in the IDE, I see: 如果我在IDE中运行程序时运行jps,则会看到:

13923 Jps 11333 MyJFrame 15006 Main 13923 Jps 11333 MyJFrame 15006主

I know my main class is MyJFrame so I know my PID must be 11333. 我知道我的主类是MyJFrame,所以我知道我的PID必须是11333。

If I run cat /proc/11333/cmdline, I get: 如果我运行cat / proc / 11333 / cmdline,则会得到:

/usr/local/jdk1.8.0_25/bin/java-Dfile.encoding=UTF-8-classpath/home/shackle/NetBeansProjects/JavaApplication31/build/classesjavaapplication31.MyJFrame

It is missing the spaces between arguments but it is fairly obvious where they would go. 它缺少参数之间的空格,但是很明显它们将去向何处。 Run that command after carefully inserting the spaces on the terminal. 在终端上小心插入空格后,运行该命令。

Also, it would be a good idea to see exactly which line is executing when you run the jar and it produces no output. 另外,最好是在运行jar时准确查看正在执行的行,并且不会产生任何输出。 Run the original command, use jps to get the pid, use jstack on that pid to get all the stack frames including the line it is blocked on. 运行原始命令,使用jps获取pid,在该pid上使用jstack获取所有堆栈帧,包括被阻塞的行。

I ended up fixing this by removing the defauls jar configuration and re-adding it, instructions below for anyone else using IntelliJ IDEA. 最后,我删除了defauls jar配置并重新添加了它,以解决此问题,下面是使用IntelliJ IDEA的其他用户的说明。

File -> Project Structure -> Artifacts

Then remove the jar settings using the small minus button and re-add using the plus symbol. 然后使用minus小按钮删除jar设置,并使用plus重新添加。

JAR -> From modules with dependencies -> OK

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM