Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / July 2005

Tip: Looking for answers? Try searching our database.

Another Runtime-Problem

Thread view: 
bernd - 29 Jul 2005 13:07 GMT
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.