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
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...
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...
> 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
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....
Probably the lack of newlines after each command. Either add them to
the strings themselves ("server...\n") or use newLine().
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.