This discussion is archived
9 Replies Latest reply: Oct 30, 2012 12:22 AM by 945083 RSS

Problem running putty.exe in a seperate thread

945083 Newbie
Currently Being Moderated
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......
  • 1. Re: Problem running putty.exe in a seperate thread
    Kayaman Guru
    Currently Being Moderated
    This is the standard response to questions about Runtime.exec: http://www.javaworld.com/jw-12-2000/jw-1229-traps.html

    You might want to check out ProcessBuilder as well, it's the newer fancier way.
  • 2. Re: Problem running putty.exe in a seperate thread
    945083 Newbie
    Currently Being Moderated
    Hi Kayaman - I have read the article previously and I have implemented both the runtime.exec and processbuilder methods to test, both the runtime.exec and also processbuilder ways of running putty work when not in a new thread

    I think (hope..) my issue is with threading and not the way I call the process to run. After doing some testing I have found that when I specify my arguments within the class I created (called MyPutty) for IP Address, user and pwd the session stays open until I am done with it - however when I pass the IP, user and pwd from another class the session closes straight away so it looks like my problem is something to do with starting the new thread in my main class and then passing it correctly to the MyPutty class, I think I need to somehow monitor the thread and inform it when it needs to close but I don't really know how to do this

    I will keep looking, thanks for your reply!
  • 3. Re: Problem running putty.exe in a seperate thread
    945083 Newbie
    Currently Being Moderated
    Worked it out in the end!

    As suspected the problem lay between the keyboard and the chair :)

    I eventually worked out that the IP I was passing was in fact incorrect, the address didn't support SSH and so the session closed immediately. Now I pass the correct address all is working and sessions stay open (in a new thread)

    facepalm

    BTW - can Putty only have maximum 1 session open? It only seems to work with 1 instance open which is a bit of a problem; as I usually can have up to 4/5 SSH sessions open at the same time (in different windows) the issue I have is that each session needs to be in a interactive window, and not just a plain SSH session where I can send commands to.......I have messed with Jsch and Ganymed for opening and using SSH sessions and they work but there is no interactive window shells available with those implementations, and unfortunately I require a window based interactive session......does anyone know of a good terminal emulator for Java in Swing??

    I used the WRQ Reflections control for .NET and it does everything I need and more but theres nothing available for Java so have been trying Putty......if anyone has a good Swing based control I would like to have a look. The Reflections control is ActiveX based so is there a way to use the DLLs in Java?
  • 4. Re: Problem running putty.exe in a seperate thread
    Kayaman Guru
    Currently Being Moderated
    Matt_C wrote:
    BTW - can Putty only have maximum 1 session open?
    What happens if you create more than one putty process?
  • 5. Re: Problem running putty.exe in a seperate thread
    gimbal2 Guru
    Currently Being Moderated
    You're asking questions you should be asking Google dude, the response is a lot quicker that way. If you can't find it through Google, its usually pretty safe to assume that it doesn't exist.

    When I search for "java ssh terminal" I get plenty of results to investigate for example.
  • 6. Re: Problem running putty.exe in a seperate thread
    945083 Newbie
    Currently Being Moderated
    Hi Gimbal2 - google is my friend (other search engines are available.....) and I have looked at loads of results but none of them gets close to what i'm looking for, nearly all the telnet/ssh libraries don't do interactive window stuff (although I admit I do want the moon on a stick....)

    The best I looked at so far is Gritty (seems to be base on JSch terminal) but it just can't handle telnet or ANSI formatting and doesn't do VT100 emulation, although the plain SSH terminal is OK and usable

    The rest are SSH/Telnet libraries and I will need to write a terminal emulator and some other stuff to make them do what I need....TBH i'm not good enough a programmer to do that. I just make small apps that make my life at work easier (and also my work mates) i'm not a professional programmer and nothing is for commercial reasons

    I use the Reflections .NET control in my old .NET app and it has absolutely everything built in, all functions of Reflections are callable via native code so it makes life so easy - I was hoping for that kind of solution in Java, because basically i'm lazy!! The reason is that the equipment I connect to uses Telnet and SSH but they aren't 'normal' servers as such, I work in 'comms and each vendor has their own language that you need to use over the Telnet/SSH session so I need a plain terminal emulator that handles XTERM/VT100/VT102, ANSI etc. Reflections did this for me and had a great way to send commands in just about any format and then store results etc

    Anyway - apologies if i've been spamming the forum with stupid questions, I do try to work through google answers before posting here....

    BTW, I tried the google search with SSH terminal and got way more/better results than searching for "Java Telnet" or "Java Terminal Emulator" so am going to have a look at some of those as well, thanks for your help!

    Edited by: Matt_C on Oct 25, 2012 6:07 AM
  • 7. Re: Problem running putty.exe in a seperate thread
    945083 Newbie
    Currently Being Moderated
    Hi Kayaman - yep, I tried and I can have more than 1 session open like that! Thanks......

    If I find a nice all-in-one Telnet/SSH/Emulator for Java (or if I ever get good enough to make one) I will be sure to post it here :)
  • 8. Re: Problem running putty.exe in a seperate thread
    EJP Guru
    Currently Being Moderated
    There are several. I've been using a combination of JTA and Ganymed for a couple of years with success. You just have to write a very simple JTA plugin that calls Ganymede to do the SSH-2 part. (JTA only supports SSH-1).
  • 9. Re: Problem running putty.exe in a seperate thread
    945083 Newbie
    Currently Being Moderated
    OK - after some searching and investigating and quite a few trials I found this:

    http://tech.cryptzone.com/mindterm/

    It's a great library with just about everything I was looking for! There are good examples for SSH and Telnet clients and they are fairly easy to use (even for me!) and the sessions/terminals are easily customisable, all the common terminals are emulated in a very good way

    I tried some of the other suggestions for JTa and JTerm and also commons.net but they didn't tick too many boxes as stand-alone solutions, this one has great emulation and also the terminal properties are easily set from either a properties file or via code (which was a big plus for my requirements) and there are good options available to set just about anything you would want to

    I have incorporated this into my app and it is really useful - just though I would pass on my findings as this is a great set of tools and certainly the best and easiest of the libraries I have tried.....In my humble opinion......

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points