Liệu chương trình của bạn không được trang bị tùy chọn --password? Thông thường, tất cả các chương trình dựa trên dòng lệnh đều làm chủ yếu cho các tập lệnh.
Runtime.getRuntime().exec(new String[]{"your-program", "--password="+pwd, "some-more-options"});
Hoặc cách phức tạp hơn và nhiều hơn nữa dễ bị lỗi:
try {
final Process process = Runtime.getRuntime().exec(
new String[] { "your-program", "some-more-parameters" });
if (process != null) {
new Thread(new Runnable() {
@Override
public void run() {
try {
DataInputStream in = new DataInputStream(
process.getInputStream());
BufferedReader br = new BufferedReader(
new InputStreamReader(in));
String line;
while ((line = br.readLine()) != null) {
// handle input here ... ->
// if(line.equals("Enter Password:")) { ... }
}
in.close();
} catch (Exception e) {
// handle exception here ...
}
}
}).start();
}
process.waitFor();
if (process.exitValue() == 0) {
// process exited ...
} else {
// process failed ...
}
} catch (Exception ex) {
// handle exception
}
mẫu này mở ra một chủ đề mới (lưu ý đồng thời và đồng bộ hóa) đó là sẽ đọc đầu ra của quá trình của bạn. Tương tự, bạn có thể cấp dữ liệu cho quy trình của mình với đầu vào miễn là nó chưa chấm dứt:
if (process != null) {
new Thread(new Runnable() {
@Override
public void run() {
try {
DataOutputStream out = new DataOutputStream(
process.getOutputStream());
BufferedWriter bw = new BufferedWriter(
new OutputStreamWriter(out));
bw.write("feed your process with data ...");
bw.write("feed your process with data ...");
out.close();
} catch (Exception e) {
// handle exception here ...
}
}
}).start();
}
Hy vọng điều này sẽ hữu ích.
tôi đã cố gắng BufferedWriter mới (OutputStreamWriter mới (signingProcess.getOutputStream())). Viết ("123456") nhưng không làm việc. –