Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to exec commands?

55 views
Skip to first unread message

Nils O. Selåsdal

unread,
Jan 30, 2001, 5:51:51 AM1/30/01
to
How can i execute something like this in java
tail -f /var/log messages | grep ftp
?
Using Runtime.exec("/usr/bin/tail -f /var/log messages | /bin/grep ftp ");
...
Doesnt do much...

Gordon Beaton

unread,
Jan 30, 2001, 7:28:55 AM1/30/01
to

If you want shell features (e.g. pipes), use a shell:

String[] cmd = {
"/bin/sh",
"-c",

"/usr/bin/tail -f /var/log messages | /bin/grep ftp "
}

Runtime.getRuntime().exec(cmd);

/gordon

--
[ do not send me private copies of your followups ]
g o r d o n . b e a t o n @ e r i c s s o n . c o m
ericsson research
stockholm, sweden

Steven C. Job

unread,
Jan 31, 2001, 10:41:48 PM1/31/01
to
How would you be able to send a sequence of commands. Such as:
->nslookup
->searchthisdomain.com

Gordon Beaton

unread,
Feb 2, 2001, 10:52:27 AM2/2/01
to
On Wed, 31 Jan 2001 22:41:48 -0500, Steven C. Job wrote:
> How would you be able to send a sequence of commands. Such as:
> ->nslookup
> ->searchthisdomain.com

Use Runtime.exec() to run nsloolup, and use the InputStream and
OutputStream to feed it commands and read the result:

Process p = Runtime.getRuntime().exec("nslookup");

BufferedReader br =
new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedWriter bw =
new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));;

bw.write("foo.com");
bw.newLine();
bw.flush();
bw.close();

String reply;
while ((reply = br.readLine()) != null) {
System.out.println(reply);
}

br.close();
p.waitFor();

If there is a lot of data, or you want to alternate between feeding it
commands and reading the reply, it might be a good idea to use
separate threads for reading and writing in order to avoid deadlock.

It's not always so easy to see when the output that results from a
command is finished (in fact I cheated in my example by closing the
output stream, so that nslookup would terminate after printing the
reply). It gets even more interesting if you want to read from stderr
at the same time.

In this particular case though, you could simply provide the domain on
the command line to nslookup...

Gary Leeson

unread,
Feb 5, 2001, 5:02:29 AM2/5/01
to
Hi,

Javaworld (http://www.javaworld.com) has a good article describing the
problems and solutions to executing commands from within Java. The article
is at : http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html

Hope this is of some use.

Gary.

"Gordon Beaton" <not.fo...@see.signature> wrote in message
news:95el3r$jj6$1...@news.du.uab.ericsson.se...

Roedy Green

unread,
Feb 5, 2001, 4:17:03 PM2/5/01
to
On 2 Feb 2001 15:52:27 GMT, not.fo...@see.signature (Gordon
Beaton) wrote or quoted :

> Process p = Runtime.getRuntime().exec("nslookup");
>

See "exec" in the Java glossary for hints on how to use exec.

--
Answers to the four most frequent questions:
Please consult the Java glossary:
1) for conversion problems, see "conversion". .
2) for I/O problems, see the "File I/O Amanuensis" and "File".
3) if you want to create a *.exe, see "native compiler".
4) if you are new to Java, see "getting started"
For the JAVA GLOSSARY see http://mindprod.com/jgloss.html
or http://209.153.246.39/jgloss.html
--
Roedy Green, Canadian Mind Products
Custom computer programming since 1963

Steven C. Job

unread,
Feb 6, 2001, 12:50:10 AM2/6/01
to
That did work.... But I can't get nsupdate to work....
Any idea why the following code does nothing?

String[] cmd = {
"/bin/nsupdate",
"-d"};

Process p = Runtime.getRuntime().exec(cmd);

BufferedReader br =
new BufferedReader(new
InputStreamReader(p.getInputStream()));
BufferedWriter bw =
new BufferedWriter(new
OutputStreamWriter(p.getOutputStream()));;

bw.write("server 192.168.1.4");
bw.write("update delete junk.test.com A");
bw.write("update add junk.test.com 86400 A 14.14.14.19");
bw.newLine();
bw.flush();
bw.close();

String reply;
while ((reply = br.readLine()) != null) {
System.out.println(reply);
}

br.close();
p.waitFor();


Thanks for any help....

Gordon Beaton

unread,
Feb 6, 2001, 2:22:27 AM2/6/01
to
On Tue, 06 Feb 2001 00:50:10 -0500, Steven C. Job wrote:
> That did work.... But I can't get nsupdate to work....
> Any idea why the following code does nothing?
>
[...]

> bw.write("server 192.168.1.4");
> bw.write("update delete junk.test.com A");
> bw.write("update add junk.test.com 86400 A 14.14.14.19");
> bw.newLine();
> bw.flush();
> bw.close();

Probably the lack of newlines after each command. Either add them to
the strings themselves ("server...\n") or use newLine().

Gordon Beaton

unread,
Feb 6, 2001, 2:38:37 AM2/6/01
to
On Mon, 05 Feb 2001 21:17:03 GMT, Roedy Green wrote:
> On 2 Feb 2001 15:52:27 GMT, not.fo...@see.signature (Gordon
> Beaton) wrote or quoted :
>
> > Process p = Runtime.getRuntime().exec("nslookup");
> >
> See "exec" in the Java glossary for hints on how to use exec.
>

Not sure if your comment was intended for me or the previous poster (I
can't see anything wrong with the line you've quoted)... anyway I did
look up exec in the glossary and thought I'd point out that the
following comment is incorrect:

"Similarly for Unix you must spawn the program that can
process the script, e.g. bash."

Normally scripts begin with the magic letters #! followed by the name
of the interpreter, and they are marked as executable. If that's the
case, then they can be run just as any binary executable without
specifying the name of the interpreter on the command line. This is
true regardless of whether you run them from an interactive shell or
from Runtime.exec(), i.e. the caller doesn't need to know that it's a
script.

You can however run scripts that aren't executable (the x mode bit
isn't set) by running the (correct) interpreter itself, and providing
the name of the script as an argument.

You do need to use a shell if you want shell features such as pipes or
redirection, just as in windows.

0 new messages