Taeky Taeky - 4 months ago 8
Linux Question

How to manipulate the output of a .jar file and print it in a file or PHP variable

I'm trying to manipulate the output of

jmxcmd.jar


/usr/bin/java -jar /home/rpteira/jmxcmd.jar - my_machine:myport mybeanname > out.txt


This prints in the
out.txt
file a list with all the available attributes and commands of
mybeanname
but when I want to execute an special command of
mybeanname
I see the result in the console but not in the
out.txt
file. For example:

/usr/bin/java -jar /home/rpteira/jmxcmd.jar - my_machine:myport mybeanname getSomeStuff > out.txt

The shell output is something similar to this, but nothing in the file.

dd/mm/yyyy hh:mm:ss -0400 de.layereight.jmxcmd.Client getSomeStuff: 0


I want the 0 displayed above. To go further I would like to manipulate the result from php using
shell_exec()
but I can't find the output in my variable in this second case so I think this issue might be correlated.

What can I do?

Answer

Change your command line pattern from

cmd > out.txt

to

cmd &> out.txt

This will output both, standard output and errors, into out.txt.