Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

process InputStream handling woes

807589Aug 25 2008 — edited Aug 26 2008
Hi all,

First let me preface by saying that I've done a number of searches on this forum and the web and I've read the Runtime exec traps article in JavaWorld and I have been unable to find any posts dealing with this specific problem.
I hope if I've overlooked the solution that I'll be forgiven. ;-)

In addition to the ProcessBuilder process starting mechanism I've used below I have also tried Runtime.exec() with the same results.

I am running the ftp program as an experiment in dealing with an external interactive cli process. When I resolve this issue I hope it will resolve the issue I am having with the real target process (which is a shell script initiated unix app).

With the windows ftp process the output I experience is listed following the code section below.

Essentially I do not get the full response to the ftp help command(s) until the bye command is sent to the external ftp process. Prior to this I just receive the first line 'Commands may be abbreviated. Commands are:'.

When I invoke the ftp process on solaris I don't even get the first "Commands may be abbreviated. Commands are:" line in response to the help command. I get all of the 'help' response printout(s) content in response to the bye command.

FYI - When I adapt the program to call the real target script I get a an unhelpful application generated error text immediately after the application starts. ie. 'localExec2 is printed and then the error text.

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;

public class localExec2 implements Runnable
{
    static Process process = null;

    /***
     * Main for the localExec2.
     ***/
    public static void main(String[] args) throws IOException
    {
        process = new ProcessBuilder("cmd.exe","/C","ftp").redirectErrorStream(true).start();

            boolean end_loop = false;
            try
            {
                Thread reader = new Thread (new localExec2());
                System.out.println("localExec2");

                reader.start();
                OutputStream outstr = process.getOutputStream();

                BufferedOutputStream bw = new BufferedOutputStream(outstr);
                
                byte[] buff = new byte[1024];
                int ret_read = 0;


                do
                {
                    try
                    {
                        ret_read = System.in.read(buff);
                  System.out.println("Successfully read from console " +ret_read+" bytes");
                        if(ret_read > 0)
                        {
                                try
                                {
                                        bw.write(buff, 0 , ret_read);
                                        bw.flush();
                                }
                                catch (Exception e)
                                {
                                     System.out.println("Get here when eg. ftp process is shutdown with bye command ");
                                        end_loop = true;
                                }
                        }
                    }
                    catch (Exception e)
                    {
                        System.out.println("Exception while reading keyboard:" + e.getMessage());
                        end_loop = true;
                    }
                }
                while((ret_read > 0) && (end_loop == false));

                System.out.println("Thread which reads from console and writes to external process is finished.");


            }
            catch (Exception e)
            {
                    System.err.println("Exception while connecting:" + e.getMessage());
                    System.exit(1);
            }
        }



    /***
     * Reader thread.
     * Reads lines from the running process and echoes them
     * on the screen.
     ***/
    public void run()
    {
        InputStream instr = process.getInputStream();

        BufferedInputStream br = new BufferedInputStream(instr);

         try
         {
             byte[] buff = new byte[1024];
             int ret_read = 0;
             
                  do{
                   ret_read = br.read(buff);
                   System.out.println("Successfully read " +ret_read+" bytes");
                      System.out.flush();
                   if (ret_read > 0)
                   {
                       System.out.write(buff, 0, ret_read);
                       System.out.flush();
                   }
                  } while(ret_read>=0);
                  System.out.println("Thread which reads from external process and echos to console is finished.");

         }
         catch (Exception e)
         {
             System.err.println("Exception while reading socket:" + e.getMessage());
         }

    }
}
Output:
localExec2
help
Successfully read from console 6 bytes
Successfully read 49 bytes
Commands may be abbreviated.  Commands are:



help
Successfully read from console 6 bytes
Successfully read 49 bytes
Commands may be abbreviated.  Commands are:



bye
Successfully read from console 5 bytes
Successfully read 548 bytes
!		delete		literal		prompt		send
?		debug		ls		put		status
append		dir		mdelete		pwd		trace
ascii		disconnect	mdir		quit		type
bell		get		mget		quote		user
binary		glob		mkdir		recv		verbose
bye		hash		mls		remotehelp
cd		help		mput		rename
close		lcd		open		rmdir
!		delete		literal		prompt		send
?		debug		ls		put		status
append		dir		mdelete		pwd		trace
ascii		disconnect	mdir		quit		type
bell		get		mget		quote		user
binary		glob		mkdir		recv		verbose
bye		hash		mls		remotehelp
cd		help		mput		rename
close		lcd		open		rmdir
Successfully read -1 bytes
Thread which reads from external process and echos to console is finished.

Successfully read from console 2 bytes
Get here when eg. ftp process is shutdown with bye command 
Thread which reads from console and writes to external process is finished.
Thanks in advance for any assistance.

/kelger
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 23 2008
Added on Aug 25 2008
2 comments
225 views