问题
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
回答1:
Your immediate problem is that you are starting separate processes for each of the 'commands' you are invoking. The password 'command' is being issued to a process that is totally unaware of the previous 'login' command.
Having said that, your more dire problem is a serious misunderstanding of what the Process class is used for and how to interact with external programs from within Java.
Here's one tutorial that may help further your education on the topic, I would advise Googling for others.
回答2:
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) :
String s="";
Process p = Runtime.getRuntime().exec("p4 login");
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
char a=(char)in.read();
while(a>0 && a<256)
{
a=(char)in.read();
if(nb==14) new PrintWriter(p.getOutputStream(),true).println(password);
if(nb>16) s=s+a;
nb++;
}
if(s.startsWith("User")) loggedIn=true;
else loggedIn=false;
回答3:
In order to communicate with a process, you don't start another process but write to its stdin. There's actually code in there that tries to do such a thing. Have sendLoginCommand
return the created process; delete all code in sendPassword
, in fact delete the whole method. In your main code write your password to the process's output stream. So what I'm saying is
new PrintWriter(sendLoginCommand().getOutputStream()).println(password);
回答4:
As a side note, i strongly advice to use a pre-packaged library that already encapsulates all the technical stuff about the process communication in Java.
A good library about that is commons-exec (http://commons.apache.org/exec/.
It comes with command line helper, background thread to read the sysout/syserr output, an optional watchdog to kill the process after a given amount of time, and so on (it works of course on nearly all os).
来源:https://stackoverflow.com/questions/10315880/how-to-send-a-string-to-the-terminal-without-having-it-to-be-a-standard-command