Results 1 to 20 of 26
- 04-27-2012, 10:27 AM #1
Member
- Join Date
- Apr 2012
- Posts
- 12
- Rep Power
- 0
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:
This function successfully sends the "p4 login" function to the terminal, but apparently, the next command where I send the password does not work.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; }
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
- 04-27-2012, 01:20 PM #2
Re: How to send non-command data to the terminal from a JAVA program?
Use the stream provided by the Process class.how to send a non-command data to the terminal
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.
- 04-27-2012, 01:29 PM #3
Member
- Join Date
- Apr 2012
- Posts
- 12
- Rep Power
- 0
Re: How to send non-command data to the terminal from a JAVA program?
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
- 04-27-2012, 01:32 PM #4
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.
- 04-27-2012, 01:42 PM #5
Member
- Join Date
- Apr 2012
- Posts
- 12
- Rep Power
- 0
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
- 04-27-2012, 01:44 PM #6
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.
- 04-27-2012, 01:47 PM #7
Member
- Join Date
- Apr 2012
- Posts
- 12
- Rep Power
- 0
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
- 04-27-2012, 01:53 PM #8
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.
- 04-27-2012, 01:56 PM #9
Member
- Join Date
- Apr 2012
- Posts
- 12
- Rep Power
- 0
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
- 04-27-2012, 01:57 PM #10
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.
- 04-27-2012, 02:06 PM #11
Member
- Join Date
- Apr 2012
- Posts
- 12
- Rep Power
- 0
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
- 04-27-2012, 02:28 PM #12
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.
- 04-27-2012, 02:34 PM #13
Member
- Join Date
- Apr 2012
- Posts
- 12
- Rep Power
- 0
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.
- 04-27-2012, 02:36 PM #14
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.
- 04-27-2012, 02:39 PM #15
Member
- Join Date
- Apr 2012
- Posts
- 12
- Rep Power
- 0
Re: How to send non-command data to the terminal from a JAVA program?
s = "Enter password:"
s2 = nothing
- 04-27-2012, 02:39 PM #16
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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.
- 04-27-2012, 02:41 PM #17
Re: How to send non-command data to the terminal from a JAVA program?
How is s longer than 4 characters? Your code shows a 4 char buffer???The size of the buffer does matter much.If you don't understand my response, don't ignore it, ask a question.
- 04-27-2012, 02:43 PM #18
Member
- Join Date
- Apr 2012
- Posts
- 12
- Rep Power
- 0
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
- 04-27-2012, 02:50 PM #19
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.
- 04-27-2012, 03:21 PM #20
Re: How to send non-command data to the terminal from a JAVA program?
Why do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
how to run the perl script on mac terminal using java program
By abhijitz in forum New To JavaReplies: 2Last Post: 04-10-2012, 11:58 AM -
To develop a program which will copy the data from database and send it via bluetooth
By adiotrox in forum New To JavaReplies: 3Last Post: 03-02-2012, 10:14 PM -
Is there any free SMS gateways to send SMS through java program or through jsp
By vivekjustthink in forum Advanced JavaReplies: 0Last Post: 06-24-2011, 02:22 PM -
send at command
By peiphb02 in forum CLDC and MIDPReplies: 0Last Post: 10-05-2009, 09:28 PM -
To send and recieve data stored in fileusing TCP program
By rosemary in forum NetworkingReplies: 2Last Post: 02-02-2009, 03:27 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks