I am writing a program in Java that needs to use terminal command to work.
My function basically looks like this :
public void sendLoginCommand() throws IOException
{
System.out.println("\n------------Sending Login Command------------\n");
String cmd="qskdjqhsdqsd";
Runtime rt = Runtime.getRuntime();
Process p=rt.exec(cmd);
}
public Process sendPassword(String password) throws IOException
{
System.out.println("\n------------Sending Password------------\n");
String cmd=password;
Runtime rt = Runtime.getRuntime();
Process p=rt.exec(cmd);
return p;
}
public void login(String password) throws IOException
{
sendLoginCommand();
Process p = sendPassword(password);
System.out.println("\n------------Reading Terminal Output------------\n");
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);
System.out.println(s);
if(s.equals("Password invalid.")) loggedIn=false;
else loggedIn=true;
}
Here, the program sends correctly th p4 login command, but then, the terminal asks for a password.
When I use the same lines that with the sendLoginCommand(), the program returns an error.
Apparently, we can send only standard commands throught Process.
I was hoping that someone knew how to send a normal string to the terminal
Thank you in advance
I have found the answer to my question.
The problem was that the second response of the terminal was in fact in the first one, and the password had to be sent in the middle of it.
Here is the code (I agree, my explanation is a little vague) :