简体   繁体   English

将Java传送到Grep:为什么不起作用?

[英]Pipe Java to Grep: Why not working?

I am trying to run this dreadfully simple command in Bash 我正在尝试在Bash中运行这个非常简单的命令

java -cp nasa-top-secret.jar gov.nasa.RocketToMoon | grep -v codehaus

but grep is not working (it does not filter out my string). 但是grep无法正常工作(它无法过滤掉我的字符串)。 How can I filter my java output using grep ? 如何使用grep过滤java输出?

输出可能在STDERR上,请尝试以下操作:

java -cp nasa-top-secret.jar gov.nasa.RocketToMoon 2>&1 | grep -v codehaus

possible scenario 可能的情况

  1. you actually have all the lines with "codehaus", so grep -v gives you nothing. 实际上,所有行都带有“ codehaus”,因此grep -v不会给您任何帮助。 I assume you know what -v stands for. 我假设您知道-v代表什么。
  2. your java program did not print anything to stdout. 您的Java程序未将任何内容输出到stdout。 check your source and make sure your program spits out to stdout. 检查您的源,并确保您的程序退出标准输出。 Otherwise, check if its stderr that your program is spitting out. 否则,检查其stderr是否表明您的程序已吐出。

possible troubleshooting step: 可能的故障排除步骤:

  1. remove the pipe to grep, run only the java program and make sure your program has output. 删除到grep的管道,仅运行Java程序并确保您的程序已输出。
  2. put 2>&1 at the end of the command and try again with grep 2>&1放在命令末尾,然后使用grep再试一次

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

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