Results 1 to 5 of 5
  1. #1
    Smokey. is offline Member
    Join Date
    Mar 2011
    Location
    Alphen aan den Rijn, The Netherlands
    Posts
    3
    Rep Power
    0

    Default Letting my program command the command prompt

    Hi all,

    I need to create a program that can send commands to the command prompt, and be able to control the telnet client. How can i accomplish this?

    EDIT: btw, I'm using a Swing GUI.
    Last edited by Smokey.; 03-26-2011 at 04:19 PM.

  2. #2
    Smokey. is offline Member
    Join Date
    Mar 2011
    Location
    Alphen aan den Rijn, The Netherlands
    Posts
    3
    Rep Power
    0

    Default

    Java Code:
     try {
                Runtime rt = Runtime.getRuntime();
                String command = "telnet 192.168.2.254";
                Process pr = rt.exec(command);
    
                BufferedReader processOutput = new BufferedReader(new InputStreamReader(pr.getInputStream()));
                BufferedWriter processInput = new BufferedWriter(new OutputStreamWriter(pr.getOutputStream()));
    
                String commandToSend = "";
                processInput.write(commandToSend);
                processInput.flush();
    
                int lineCounter = 0;
                while (true)
                {
                    String line = processOutput.readLine();
                    if (line == null)
                    {
                        break;
                    }
                    System.out.println(++lineCounter + ": " + line);
                }
    
                processInput.close();
                processOutput.close();
                pr.waitFor();
            } catch (Exception x) {
                x.printStackTrace();
            }
        }
    I know the coding isnt complete, but when i try to lauch it i get an exception:

    java.io.IOException: Cannot run program "telnet": CreateProcess error=2, Het systeem kan het opgegeven bestand niet vinden (system cant find file)
    at java.lang.ProcessBuilder.start(ProcessBuilder.java :460)
    at java.lang.Runtime.exec(Runtime.java:593)
    at java.lang.Runtime.exec(Runtime.java:431)
    at java.lang.Runtime.exec(Runtime.java:328)
    at testcmdsender.Main.main(Main.java:12)

    help? =D

  3. #3
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    It appears that 'telnet' is not in the path, since the exception is complaining that it can't find it. You might try giving a full path to telnet, or ensuring your environment is properly set up from the account launching it.

  4. #4
    Smokey. is offline Member
    Join Date
    Mar 2011
    Location
    Alphen aan den Rijn, The Netherlands
    Posts
    3
    Rep Power
    0

    Default

    Thnx, I'll try that. On the other hand, i have no clue how to get my envirement "ready". Could you spare some tips? :) Im totally new to threading within java.

  5. #5
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    How you set up your environment path completely depends on your OS. The path has nothing to do with java or threads, it's an OS issue.

Similar Threads

  1. java program for running the command prompt commands
    By leoanto in forum Advanced Java
    Replies: 2
    Last Post: 03-26-2011, 03:57 PM
  2. New Command prompt
    By jmga9 in forum New To Java
    Replies: 4
    Last Post: 11-10-2010, 01:53 AM
  3. log message from command prompt
    By tedy2808 in forum New To Java
    Replies: 5
    Last Post: 08-25-2010, 11:04 AM
  4. help me!!!! about command prompt..
    By kureikougaiji in forum New To Java
    Replies: 2
    Last Post: 11-13-2008, 06:15 PM
  5. Replies: 2
    Last Post: 08-06-2007, 09:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •