I needed to execute java class third party from shell script. When exception occurs, they are writing error message with System.err as per requirement. Now, when i executed the class in shell, i want to get that error message. I cannot get complete message in standard output and search for this message as its a big script and there are lot of messages. Is there a way to capture that specific System.err printed in java when i executed it in shell. ?Just let me know, if the problem is not clear enough.
Asked
Active
Viewed 874 times
1
-
try looking at Process Builder in java – 0o'-Varun-'o0 Apr 09 '15 at 08:59
-
2I guess you just want to redirect stderr to a file? To do that, add `2>error.out` to your command, to redirect stderr to a file called `error.out`. – Tom Fenech Apr 09 '15 at 08:59
-
Hi the file redirect idea that you have works fine. Can i redirect it to a variable.? i need to execute the command in perl. So, i need to execute shell command that redirects stderr to variable. – Srini Apr 09 '15 at 09:07
-
1Please [edit] your question to make it clear whether you're asking about executing a command in the shell, or from perl. [This section of the Perl docs](http://perldoc.perl.org/perlfaq8.html#How-can-I-capture-STDERR-from-an-external-command%3f) may be useful for you. – Tom Fenech Apr 09 '15 at 09:14
2 Answers
1
To pick your java application (or whatever program for that matter) standard error in a variable while discarding its standard output, you can use this shell construction:
var=$(java MyApplication 2>&1 1>/dev/null)
If you don't want to discard the standard output, you can redirect it to a file instead of /dev/null
, or redirect it to your terminal emulator using /dev/tty
.

jlliagre
- 29,783
- 6
- 61
- 72
0
Please refer this-
How to print an error message to standard error?
I think the best answer to that question answers your question as well.