我有一个Windows命令行程序app.exe, 运行后会提示,要求输入密码才能继续, 不能把密码当作单独的参数,一定要先执行,再打入密码...我想用Java调用这个程序,但不知如何输入密码给那个命令行程序还请大家帮忙想想办法,
谢谢大家!
测试代码如下,执行后死机,不会显示"完成":import java.io.*;
import java.util.*;public class Test {
static BufferedReader stdout, stderr;
static Vector outputLines;
public static void main(String args[]) {
try {
BufferedReader br;
br = new BufferedReader(new InputStreamReader(System.in));
StringBuffer s2 = new StringBuffer();
String line;
System.out.println("输入密码:");
line = br.readLine();
if (line != null) {
s2.append(line);
s2.append("\r\n");
}
Runtime r;
r = Runtime.getRuntime();
Process p = r.exec(app.exe); //外部命令行程序
System.out.println("读取输出:");
InputStream is = p.getInputStream();
stdout = new BufferedReader(new InputStreamReader(is));
is = p.getErrorStream();
stderr = new BufferedReader(new InputStreamReader(is));
outputLines = new Vector(); Thread stdoutThread = new Thread() {
public void run() {
try {
int l;
String line;
for(l = 0; (line = stdout.readLine()) != null; ) {
if (line.length() > 0) {
l++;
outputLines.addElement(line);
}
System.out.println(line);
}
System.out.println("\n已读" + l);
stdout.close();
}
catch(IOException ie) {
System.out.println("IO exception: " + ie);
}
}
};
stdoutThread.start(); Thread stderrThread = new Thread() {
public void run() {
try {
int l;
String line;
for(l = 0; (line = stderr.readLine()) != null; ) {
if (line.length() > 0) l++;
System.out.print(",");
}
System.out.println("\n已读" + l);
stderr.close();
}
catch(IOException ie) {
System.out.println("IO exception: " + ie);
}
}
};
stderrThread.start(); OutputStream os = p.getOutputStream();
PrintStream ps2 = new PrintStream(os);
ps2.println(s2.toString());
System.out.println("已输入");
ps2.close();
System.out.println("等待waitfor");
p.waitFor();
System.out.println("完成");
}
catch (Exception e) {
System.out.println("Exception: " + e);
}
}
}
谢谢大家!
测试代码如下,执行后死机,不会显示"完成":import java.io.*;
import java.util.*;public class Test {
static BufferedReader stdout, stderr;
static Vector outputLines;
public static void main(String args[]) {
try {
BufferedReader br;
br = new BufferedReader(new InputStreamReader(System.in));
StringBuffer s2 = new StringBuffer();
String line;
System.out.println("输入密码:");
line = br.readLine();
if (line != null) {
s2.append(line);
s2.append("\r\n");
}
Runtime r;
r = Runtime.getRuntime();
Process p = r.exec(app.exe); //外部命令行程序
System.out.println("读取输出:");
InputStream is = p.getInputStream();
stdout = new BufferedReader(new InputStreamReader(is));
is = p.getErrorStream();
stderr = new BufferedReader(new InputStreamReader(is));
outputLines = new Vector(); Thread stdoutThread = new Thread() {
public void run() {
try {
int l;
String line;
for(l = 0; (line = stdout.readLine()) != null; ) {
if (line.length() > 0) {
l++;
outputLines.addElement(line);
}
System.out.println(line);
}
System.out.println("\n已读" + l);
stdout.close();
}
catch(IOException ie) {
System.out.println("IO exception: " + ie);
}
}
};
stdoutThread.start(); Thread stderrThread = new Thread() {
public void run() {
try {
int l;
String line;
for(l = 0; (line = stderr.readLine()) != null; ) {
if (line.length() > 0) l++;
System.out.print(",");
}
System.out.println("\n已读" + l);
stderr.close();
}
catch(IOException ie) {
System.out.println("IO exception: " + ie);
}
}
};
stderrThread.start(); OutputStream os = p.getOutputStream();
PrintStream ps2 = new PrintStream(os);
ps2.println(s2.toString());
System.out.println("已输入");
ps2.close();
System.out.println("等待waitfor");
p.waitFor();
System.out.println("完成");
}
catch (Exception e) {
System.out.println("Exception: " + e);
}
}
}
String psw = new Scanner(System.in).nextLine();
Runtime.getRuntime().exec("app.exe", {psw});