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!

Problem running putty.exe in a seperate thread

945083Oct 25 2012 — edited Oct 30 2012
Hi All - I have an issue when using a new thread to run an external .exe file (Putty in this case)

When I don't use a new thread Putty will start and stay open and let me do what I need to as normal, however it then hangs the rest of the application so you can't use it until you are finished with Putty - So I thought I would put it in a new thread. I use this method quite a bit in my application and don't have any issues so i'm wondering what I have done wrong here, I call other external .exe files and also start some external Java processes using a new thread and they work OK (using the same method)

It seems to me that the putty process doesn't seem to give any output and so the thread assumes it has closed and shuts down, or I need to inform the thread to wait and send it a message to clean up when I am finished. If I connect to a host and need to add the RSA key to the cache then Putty will stay open in the new thread until I click OK to save the key, then as soon as I have done that it closes

Can anyone point me in the right direction?

Below is some code for working and not working scenarios, the code below has been altered to run from a button click as in my app it's done from a popup menu and I don't think the issue is with the calling function (but I could be wrong)......

Working:
    private void jbtnTestButtonActionPerformed(java.awt.event.ActionEvent evt) {                                               

        try {
            String myPuttyDir;
            File myFile = new File("C:\\Putty\\putty.exe");
            myPuttyDir = myFile.toString();    
            String myPuttyArgs = " -ssh USERNAME@192.168.2.134 -pw PASSWD";
            String myProcStart = myPuttyDir + myPuttyArgs;
            Process proc = Runtime.getRuntime().exec(myProcStart);
            BufferedReader bri = new BufferedReader(new InputStreamReader(proc.getInputStream()));
            BufferedReader bre = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
            String line;
            while ((line = bri.readLine()) != null) {
                System.out.println(line);
            }
            bri.close();
            while ((line = bre.readLine()) != null) {
                System.out.println(line);
            }
            bre.close();
            proc.waitFor();
            System.out.println("Putty Closed!");          
            
            } 
            catch (Exception ex) 
            {
                System.out.println("Exception error :"+ex.getMessage());
                ex.printStackTrace();
            }
       //no issue with this method, putty stays open until I close it and then it prints the closed message and exits cleanly
    }
And here is the code with a new thread:
private void jbtnTestButtonActionPerformed(java.awt.event.ActionEvent evt) {                                               
        
        final Thread t = new Thread() {

            public void run() {
                try {
                   String myPuttyDir;
                    File myFile = new File("C:\\Putty\\putty.exe");
                    myPuttyDir = myFile.toString();
                    String myPuttyArgs = " -ssh USERNAME@192.168.2.134 -pw PASSWD";
                    String myProcStart = myPuttyDir + myPuttyArgs;
                    Process proc = Runtime.getRuntime().exec(myProcStart);
                    BufferedReader bri = new BufferedReader(new InputStreamReader(proc.getInputStream()));
                    BufferedReader bre = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
                    String line;
                    while ((line = bri.readLine()) != null) {
                        System.out.println(line);
                    }
                    bri.close();
                    while ((line = bre.readLine()) != null) {
                        System.out.println(line);
                    }
                    
                    bre.close();
                    proc.waitFor();
                    System.out.println("Putty Closed!");

                } catch (Exception ex) {
                    System.out.println("Exception error :" + ex.getMessage());
                    ex.printStackTrace();
                }
            } 
        };
        t.start();//start the thread 
        //This method opens Putty for a second, you just see it, and then it closes
}   
Thanks in advance for any help! I'm new to threading in Java having come from a .NET environment and I think I need to listen to the thread somehow but i'm a bit lost......
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 27 2012
Added on Oct 25 2012
9 comments
1,261 views