Hello netties,
the following code is a test variant of a class which should later read
in continuously the output of a "tail -f" on an arbitrary file. My
problem lies a little bit before:
import java.io.* ;
public class TailFilter {
public static void main ( String args[] ) {
String filter = args[1] ;
String filetoread = args[0] ;
TailFilter tf = new TailFilter() ;
Process proc = tf.getProcess(filter, filetoread) ;
}
private static Process process ;
private static Process getProcess (String filter, String filetoread)
{
String comline[] ;
int lim = 3;
comline = new String[lim] ;
comline[0] = "/bin/ksh" ;
comline[1] = "-c";
comline[2] = "'/bin/tail /home/back/test.ksh'" ;
int i=0;
while ( i < lim ) {
System.out.print(comline[i]) ;
i++ ;
}
System.out.print("\n") ;
/*
Fire up the command line
*/
try {
Runtime runt = Runtime.getRuntime() ;
process = runt.exec( comline ) ;
try {
int rc = process.waitFor() ;
System.out.println("return code " + String.valueOf(rc)) ;
InputStream instream = process.getInputStream() ;
for ( int ch; (ch = instream.read()) != -1;) {
System.out.write(ch) ;
}
InputStream erstream = process.getErrorStream() ;
for ( int ch; (ch = erstream.read()) != -1;) {
System.out.write(ch) ;
}
... SOME "catch {" etc.
Forget about the command line parameters for TailFilter, they are
purposely not used here at the moment.
The output under DEC OSF/1 V5.1b is as follows:
$> java TailFilter 1 3
/bin/ksh-c'/bin/tail /home/back/test.ksh'
return code 127
/bin/ksh: /bin/tail /home/back/test.ksh: not found
The strings in comline[] copied/pasted to the command line (i.e.
executed directly) work fine.
Obviously Runtime.exec() passes "/bin/tail /home/back/test.ksh" in a
way to the shell that it is interpreted as ONE executable (or am I
wrong ?)
O.k., that lead me to the conclusion that I had to separate "/bin/tail"
from "/home/back/test.ksh". The code changed is as follows:
...
int lim = 4; // CHANGED !
comline = new String[lim] ;
comline[0] = "/bin/ksh" ;
comline[1] = "-c";
comline[2] = "/bin/tail"; // CHANGED !
comline[3] = "/home/back/test.ksh" ; // CHANGED !
...
It did not get better :-(
$> java TailFilter 1 3
/bin/ksh-c/bin/tail/home/back/test.ksh
and nothing more, the program hangs.
Although the weekend is approaching: Has somebody out there an idea ?
Would be greatfully appreciated ;-)
Nice weekend.
Cheers
Bernd
A. Bolmarcich - 29 Jul 2005 19:57 GMT
[snip]
> comline[0] = "/bin/ksh" ;
> comline[1] = "-c";
> comline[2] = "'/bin/tail /home/back/test.ksh'" ;
[snip]
> Obviously Runtime.exec() passes "/bin/tail /home/back/test.ksh" in a
> way to the shell that it is interpreted as ONE executable (or am I
> wrong ?)
Actually, it passes
'/bin/tail /home/back/test.ksh'
The shell does not find a command with that 31-character name that
includes the initial and final apostrophe characters.
> O.k., that lead me to the conclusion that I had to separate "/bin/tail"
> from "/home/back/test.ksh". The code changed is as follows:
[quoted text clipped - 8 lines]
> comline[2] = "/bin/tail"; // CHANGED !
> comline[3] = "/home/back/test.ksh" ; // CHANGED !
[snip]
Now you get the same effect as if you typed the following line into
an interactive shell
/bin/ksh -c /bin/tail /home/back/test.sh
Which runs the command
/bin/tail
and passes
/home/back/test/sh
as an argument to the shell (not to the command the shell runs). To get
what you want use
int lim = 3;
comline = new String[lim] ;
comline[0] = "/bin/ksh" ;
comline[1] = "-c";
comline[2] = "/bin/tail /home/back/test.s ";
You will get the same affect as if you typed the following line into
an interactive shell
/bin/ksh -c '/bin/tail /home/back/test.s'
The interactive shell interprets the apostophes and makes the argument
of the -c option
/bin/tail /home/back/test.s