Page 1 of 2 12 LastLast
Results 1 to 20 of 26
  1. #1
    mtrompe is offline Member
    Join Date
    Apr 2012
    Posts
    12
    Rep Power
    0

    Default How to send non-command data to the terminal from a JAVA program?

    Hello,

    I am working on a project where at one point, I have to login to an external program from my java program.
    To do so, I have implemented the following function:

    Java Code:
    public void login(String password) throws IOException   
        {       
            //Send command to terminal   
            Process p = Runtime.getRuntime().exec("p4 login");   
               
            //Send password to terminal   
            p.getOutputStream().flush();   
            new PrintWriter(p.getOutputStream()).println(password);    
               
            //Read terminal output   
            Reader in = new InputStreamReader(p.getInputStream());   
            in = new BufferedReader(in);   
            char[] buffer = new char[20];   
            int len = in.read(buffer);   
            String s = new String(buffer, 0, len);   
               
            //Print for test   
            System.out.println(s);   
      
            //Test to know if user is logged in   
            if(s.startsWith("User")) loggedIn=true;   
            else loggedIn=false;   
            loggedIn=true;   
        }
    This function successfully sends the "p4 login" function to the terminal, but apparently, the next command where I send the password does not work.
    I know this because of the output I receive after the System.out.println(); Indeed, the output is always "Enter password", which is the anwser of the first command.

    If anyone knows how to send a non-command data to the terminal, it would be great.

    Thank you

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: How to send non-command data to the terminal from a JAVA program?

    how to send a non-command data to the terminal
    Use the stream provided by the Process class.

    Your code get two instances of the outputstream, try changing it to only get one and use that it the two places.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    mtrompe is offline Member
    Join Date
    Apr 2012
    Posts
    12
    Rep Power
    0

    Default Re: How to send non-command data to the terminal from a JAVA program?

    Quote Originally Posted by Norm View Post
    Use the stream provided by the Process class.

    Your code get two instances of the outputstream, try changing it to only get one and use that it the two places.
    I don't understand, I thought I already used the stream provided by the Process class with
    new PrintWriter(p.getOutputStream()).println(password)

    and also I don't understand why you say that I have two instances of the output stream because I only use it once (except for the flush)

    I'm sorry there is not much that I understood in your response, don't hit too hard on me :p

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: How to send non-command data to the terminal from a JAVA program?

    Are there any messages on the error stream?
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    mtrompe is offline Member
    Join Date
    Apr 2012
    Posts
    12
    Rep Power
    0

    Default Re: How to send non-command data to the terminal from a JAVA program?

    That I can not tell, when I try reading the error stream, the program starts loading and never stops

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: How to send non-command data to the terminal from a JAVA program?

    Sorry, I don't understand what "never stops" means. What program does that?
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    mtrompe is offline Member
    Join Date
    Apr 2012
    Posts
    12
    Rep Power
    0

    Default Re: How to send non-command data to the terminal from a JAVA program?

    What I mean is that the program seems the be doing some stuff when I ask him to read the input, but he never sends anything

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: How to send non-command data to the terminal from a JAVA program?

    Are you sending the response before the process has asked for it?
    Read the request first and then send the response.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    mtrompe is offline Member
    Join Date
    Apr 2012
    Posts
    12
    Rep Power
    0

    Default Re: How to send non-command data to the terminal from a JAVA program?

    I updated my code to:

    public void login(String password) throws IOException
    {
    //Send command to terminal
    Process p = Runtime.getRuntime().exec("p4 login");

    //Read terminal output
    Reader in = new InputStreamReader(p.getInputStream());
    in = new BufferedReader(in);
    char[] buffer = new char[4];
    int len = in.read(buffer);
    String s = new String(buffer, 0, len);
    //Print for test
    System.out.println(s);

    //Send password to terminal
    new PrintWriter(p.getOutputStream(),true).println(pass word);

    //Read terminal output

    in = new BufferedReader(in);
    char[] buffer2 = new char[204];
    int len2 = in.read();
    String s2 = new String(buffer2, 0, len2);

    //Print for test
    if(s2!=null) System.out.println(s2);

    //Test to know if user is logged in
    if(s.startsWith("User")) loggedIn=true;
    else loggedIn=false;
    loggedIn=true;
    }

    so I read the output before sending the second request

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: How to send non-command data to the terminal from a JAVA program?

    Does it work now?
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    mtrompe is offline Member
    Join Date
    Apr 2012
    Posts
    12
    Rep Power
    0

    Default Re: How to send non-command data to the terminal from a JAVA program?

    No, it prints "Enter password:" which is the response of the first request, but then it doesn't print anything, as if I never sent a second request

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: How to send non-command data to the terminal from a JAVA program?

    Why is the buffer only 4 char long?
    What prints out for s and s2?
    Try flushing what you write.
    Try adding a newline at the end of what you write.
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    mtrompe is offline Member
    Join Date
    Apr 2012
    Posts
    12
    Rep Power
    0

    Default Re: How to send non-command data to the terminal from a JAVA program?

    The size of the buffer does matter much. True, it is too small but doesn't affect the program.
    s is the string in which I store the terminal output of the first request.
    s2 is the string in which I store the terminal ouput of the second request.
    I already tried adding a new line at the end of the password, but it doesn't change anything.
    And I also flushed what I write, doesn't change anything.

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: How to send non-command data to the terminal from a JAVA program?

    Try a bigger buffer to make sure all is read from stream.
    What prints out for s and s2? Post the output.
    If you don't understand my response, don't ignore it, ask a question.

  15. #15
    mtrompe is offline Member
    Join Date
    Apr 2012
    Posts
    12
    Rep Power
    0

    Default Re: How to send non-command data to the terminal from a JAVA program?

    s = "Enter password:"
    s2 = nothing

  16. #16
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: How to send non-command data to the terminal from a JAVA program?

    You ought to show us the code with the flush after you have sent the password.
    The lack of a flush there would concern me that it is simply stuck in the buffer.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  17. #17
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: How to send non-command data to the terminal from a JAVA program?

    The size of the buffer does matter much.
    How is s longer than 4 characters? Your code shows a 4 char buffer???
    If you don't understand my response, don't ignore it, ask a question.

  18. #18
    mtrompe is offline Member
    Join Date
    Apr 2012
    Posts
    12
    Rep Power
    0

    Default Re: How to send non-command data to the terminal from a JAVA program?

    Sorry, I put 4 in the buffer just to test something.
    It is actually at 20

  19. #19
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: How to send non-command data to the terminal from a JAVA program?

    Please post the code that you are now working with.
    If you don't understand my response, don't ignore it, ask a question.

  20. #20
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: How to send non-command data to the terminal from a JAVA program?

    Quote Originally Posted by mtrompe View Post
    I updated my code to:
    BB Code List - Java Programming Forum

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 2
    Last Post: 04-10-2012, 12:58 PM
  2. Replies: 3
    Last Post: 03-02-2012, 11:14 PM
  3. Replies: 0
    Last Post: 06-24-2011, 03:22 PM
  4. send at command
    By peiphb02 in forum CLDC and MIDP
    Replies: 0
    Last Post: 10-05-2009, 10:28 PM
  5. Replies: 2
    Last Post: 02-02-2009, 04:27 PM

Tags for this Thread

Posting Permissions

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