This discussion is archived
7 Replies Latest reply: May 7, 2008 4:07 AM by masijade RSS

scProcess = Runtime.getRuntime().exec( "Telnet 147.27.23.1");

807591 Newbie
Currently Being Moderated
Hello All,
I am trying to run telnet process from windows platform as follows

scProcess = Runtime.+getRuntime+().exec( "telnet 147.27.23.1");
Objective:*
My intention is to create a telnet connection to remote server, get the Input, Output and Error Stream of the process and run many unix commands on the session opened.
This all I want to do through code itself and then get the output of the command from streams.
Tried:*
However whenever I tried above, I always get process Exit value = -1.

I tried doing


scProcess = Runtime.+getRuntime+().exec( "cmd /c telnet 147.27.23.1");


which is also failing.
And
scProcess = Runtime.+getRuntime+().exec( "cmd /c start telnet 147.27.23.1"); which actually opens interactive telnet window, which is not my objective is.

Any suggestions on my objective? Is there any other way to do telnet from code?

Thanks in advance.

With Best Regards,
Aditya.

  • 1. Re: scProcess = Runtime.getRuntime().exec( "Telnet 147.27.23.1");
    masijade Explorer
    Currently Being Moderated
    Google for a Java Telnet API (Apache has one, but there are other's as well), as there is no need to
    attempt to execute telnet using Runtime.exec and all of it's associated problems.
  • 2. Re: scProcess = Runtime.getRuntime().exec( "Telnet 147.27.23.1");
    807591 Newbie
    Currently Being Moderated
    Hello masijade,
    Thanks a lot for the reply.
    However I have Apache API and plink command line utility as well to do telnet. But whenever I try to do telnet to AS400 server, I always get bunch of garbage Strings on command prompt which makes me difficult to handle.
    But when I tried same using "telnet servername" from windows, I never had this issue.
    Thats why I want to opt for this solution.

    Regards,
    Aditya.
  • 3. Re: scProcess = Runtime.getRuntime().exec( "Telnet 147.27.23.1");
    masijade Explorer
    Currently Being Moderated
    That probably has to do with character encoding. Read the API documentation for that module and see if there is something you can do about it.

    Aside from that, all I can do is point you to here, and say to use ProcessBuilder, not Runtime.exec, but most of the items in the Article still apply.
  • 4. Re: scProcess = Runtime.getRuntime().exec( "Telnet 147.27.23.1");
    807591 Newbie
    Currently Being Moderated
    masijade. wrote:
    and say to use ProcessBuilder, not Runtime.exec, but most of the items in the Article still apply.
    When ProcessBuilder was introduced (1.5 ?) the Runtime.exec code was re-factored so that it became a thin wrapper of ProcessBuilder so it makes no real difference whether one uses ProcessBuilder or Runtime.exec.
  • 5. Re: scProcess = Runtime.getRuntime().exec( "Telnet 147.27.23.1");
    masijade Explorer
    Currently Being Moderated
    sabre150 wrote:
    masijade. wrote:
    and say to use ProcessBuilder, not Runtime.exec, but most of the items in the Article still apply.
    When ProcessBuilder was introduced (1.5 ?) the Runtime.exec code was re-factored so that it became a thin wrapper of ProcessBuilder so it makes no real difference whether one uses ProcessBuilder or Runtime.exec.
    Thanks. Didn't really look into that.

    Did they take the time to interpret "quoted-strings" properly when a single-string command is entered (since that is normally done by the shell, and since exec doesn't have one, they weren't interpreted right)? Just interested if you know. ;-)
  • 6. Re: scProcess = Runtime.getRuntime().exec( "Telnet 147.27.23.1");
    807591 Newbie
    Currently Being Moderated
    masijade. wrote:
    sabre150 wrote:
    masijade. wrote:
    and say to use ProcessBuilder, not Runtime.exec, but most of the items in the Article still apply.
    When ProcessBuilder was introduced (1.5 ?) the Runtime.exec code was re-factored so that it became a thin wrapper of ProcessBuilder so it makes no real difference whether one uses ProcessBuilder or Runtime.exec.
    Thanks. Didn't really look into that.

    Did they take the time to interpret "quoted-strings" properly when a single-string command is entered (since that is normally done by the shell, and since exec doesn't have one, they weren't interpreted right)? Just interested if you know. ;-)
    I don't have a real problems with "quoted-strings" since I usually use a String array to define the command. If I need shell features such as using environment variables or handling quotes then I use a shell. For the most part, ProcessBuilder is a wrapper round the 'C' exec() functions so if bugs exist in C functions there is a good chance that the bugs exist in the Java.
  • 7. Re: scProcess = Runtime.getRuntime().exec( "Telnet 147.27.23.1");
    masijade Explorer
    Currently Being Moderated
    sabre150 wrote:
    I don't have a real problems with "quoted-strings" since I usually use a String array to define the command. If I need shell features such as using environment variables or handling quotes then I use a shell. For the most part, ProcessBuilder is a wrapper round the 'C' exec() functions so if bugs exist in C functions there is a good chance that the bugs exist in the Java.
    I use the array version, too, so I don't have a problem with it either, but it's one of the questions I see fairly often, so I was just interested if it had been "fixed". ;-)

    Edit: Without trying it, that is. That interested, I'm not. ;-)