Operating System - HP-UX
1752319 Members
6120 Online
108786 Solutions
New Discussion юеВ

How to read piped commands with JAVA

 
SOLVED
Go to solution
JUAN VIDAL_1
Advisor

How to read piped commands with JAVA

PA-RISC RP3410
java6

I'd like to perform the following unix command from Java:

lsof -P|grep TCP

I'm using:
Process process = Runtime.getRuntime().exec( "lsof -P|grep TCP" );

but I get no output, only if I remove grep.

TIA for your help.

Juan.
7 REPLIES 7
Steven Schweda
Honored Contributor

Re: How to read piped commands with JAVA

I know nothing, but have you tried anything
like this:?

[...]exec( "sh -c 'lsof -P|grep TCP'" );

That may need to look more like this:

[...]exec( { "sh", "-c", "'lsof -P|grep TCP'" } );
James R. Ferguson
Acclaimed Contributor

Re: How to read piped commands with JAVA

Hi Juan:

I don't do Java (as we say). However, in other languages, the lvalue of the system() call would return the status of the call, not the results.

Perhaps this helps:

http://www.devdaily.com/java/edu/pj/pj010016/pj010016.shtml

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: How to read piped commands with JAVA

>JRF: the value of the system() call would return the status of the call, not the results.

Right. The results would go to stdout/stderr or to a redirected file.
Steven Schweda
Honored Contributor

Re: How to read piped commands with JAVA

> I don't do Java (as we say). [...]

A long time ago, I looked at some Java code
for several minutes, so I still know nothing.

My theory is that the pipeline ("|") symbol
means something to a shell, not to lsof.
Therefore, if Runtime.getRuntime().exec uses
something in the execl()/execv() category,
then I'd expect all this stuff to be passed
into lsof, which might become confused and/or
unhappy. Java syntax aside, my plan is to
run a shell, to whom the pipeline ("|") makes
some sense, and pass it the (shell) command
which works so well when used interactively
(that is, when you give that command to a
shell).

One advantage to all these new programming
languages is that they make it easy to write
code while having no idea what the computer
is actually doing. Starting on a Univac 422
is obviously a superior method, which I
recommend to one and all. (There's very
little magic in a system with 512 15-bit
words of core memory.)
JUAN VIDAL_1
Advisor

Re: How to read piped commands with JAVA

exec( "sh -c 'lsof -P|grep TCP'" )
gets no output.

Guess java VM just process one proc at shell...
Mel Burslan
Honored Contributor
Solution

Re: How to read piped commands with JAVA

did you try doing it the poor man's pipe way, i.e., creating intermediary files yourself, instead of relying on the OS ?

exec( "sh -c 'lsof -P >/tmp/some_temp_file'" )
exec( "sh -c 'grep TCP /tmp/some_temp_file'" )
exec( "sh -c 'rm /tmp/some_temp_file'")

I know it is not the most elegant way of doing it or most efficient, I can even guarantee it is a resource hog by launching series exec calls, but if you are not going to execute your java code continuously, you can absorb the performance penalty.

Just a suggestion, otherwise, I am not a java programmer neither. Quoting JRF "I don't do anything with word JAVA in it" other than drinking copious amounts of Java everyday :)
________________________________
UNIX because I majored in cryptology...
Dennis Handly
Acclaimed Contributor

Re: How to read piped commands with JAVA

>Mel: did you try doing it the poor man's pipe way, i.e., creating intermediary files yourself, instead of relying on the OS?

(This doesn't use the OS, but the shell.)

>exec("sh -c 'lsof -P >/tmp/some_temp_file'")

If you invoke sh, you can use a pipe.