Reputation: 329
I am running a java program which throws an exception. I am piping the run's output to a grep command where I am checking for a pattern which does not exist, however grep keeps returning a match.
0,2,468.000000
1,2,305.000000
2,5,2702.000000
3,3,1672.000000
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
at java.util.ArrayList.rangeCheck(ArrayList.java:653)
at java.util.ArrayList.get(ArrayList.java:429)
at TestConverter.processPayments(TestConverter.java:113)
at TestConverter.processFile(TestConverter.java:131)
at TestConverter.main(TestConverter.java:142)
I am running the following command:
java -classpath TestConverter.jar TestConverter test_xml.xml | grep "stringthatdoesnotmatch*"
I get the following output:
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
at java.util.ArrayList.rangeCheck(ArrayList.java:653)
at java.util.ArrayList.get(ArrayList.java:429)
at TestConverter.processPayments(TestConverter.java:113)
at TestConverter.processFile(TestConverter.java:131)
at TestConverter.main(TestConverter.java:142)
I am not sure why this is happening. Furthermore, my end goal is to check if the output contains the following pattern:
*java.lang.IndexOutOfBoundsException*ArrayList.java:653*TestConverter.java:142*
Upvotes: 3
Views: 159
Reputation: 881323
You're actually not getting a match. What you have to understand is the difference between standard output and standard error.
The Java program is writing its normal output to the former and exceptions to the latter, but both those streams are initially attached to your terminal device.
So, when you add a pipeline:
program | grep xyzzy
the |
character will divert the standard output of program
to the standard input of grep
. That will then filter out any lines not containing xyzzy
, and pass the rest to its standard output, the terminal in this case.
The standard error of program
is still connected directly to the terminal so will appear there regardless of what grep
does.
If you want the grep
program to capture both output and error of `program, combine them first:
program 2>&1 | grep xyzzy
That 2>&1
means take what was destined for stream 2 (standard error) and instead send it to stream 1 (standard output). Then the |
can take all of the output/error coming from program
.
Upvotes: 3