[英]java runtime process exec
我需要做一個主機包裝,該主機運行Music SP,並且可以通過TN3270終端進行訪問。
我使用終端仿真器x3270,並且需要在Java中與其連接。 我嘗試使用Java運行時:
public void init() {
Process p = null;
byte[] buffer = new byte[2048];
Scanner s = new Scanner(System.in);
System.out.println("> Starting server...");
try {
// Execution of mainframe
p = Runtime.getRuntime().exec(
"C:\\Program Files\\wc3270\\wc3270.exe", null);
// Capture I/O
in = new BufferedReader(
new InputStreamReader(p.getInputStream()));
out = new BufferedWriter(
new OutputStreamWriter(p.getOutputStream()));
err = new BufferedReader(
new InputStreamReader(p.getInputStream()));
System.out.println("> OK!");
// Connect/login/enter/tareas.c
System.out.println("> Connecting to Music SP...");
execute("connect XXX.XXX.XXX.XXX:XXX");
enter();
//p.waitFor();
System.out.println("> OK!");
enter();
//p.waitFor();
System.out.println("> Login...");
execute("String(login)");
enter();
execute("String(pass)");
enter();
//p.waitFor();
enter();
System.out.println("> OK!");
System.out.println("> Start tareas.c...");
execute("String(tareas.c)");
enter();
//p.waitFor();
System.out.println("> OK!");
} catch (Exception e) {
e.printStackTrace();
p.destroy();
}
}
public void execute(String query) throws IOException {
if (query != null) {
out.write(query);
out.flush();
}
if (!checkOK()) {
System.out.println(">>> FAIL QUERY");
}
}
public void enter() throws IOException {
out.write("enter");
out.flush();
if (!checkOK()) {
System.out.println(">>> FAIL ENTER");
}
}
public boolean checkOK() {
try {
in.readLine();
return in.readLine().contains("ok");
} catch (Exception e) {
return false;
}
}
但是當我執行我得到:
> Starting server...
OK!
Connecting to Music SP...
FAIL QUERY
FAIL ENTER
OK!
FAIL ENTER
Login...
FAIL QUERY
java.io.IOException: Stream Closed
at java.io.FileOutputStream.writeBytes(Native Method)
at java.io.FileOutputStream.write(Unknown Source)
at java.io.BufferedOutputStream.flushBuffer(Unknown Source)
at java.io.BufferedOutputStream.flush(Unknown Source)
at sun.nio.cs.StreamEncoder.implFlush(Unknown Source)
at sun.nio.cs.StreamEncoder.flush(Unknown Source)
at java.io.OutputStreamWriter.flush(Unknown Source)
at java.io.BufferedWriter.flush(Unknown Source)
at Connection.enter(Connection.java:86)
at Connection.init(Connection.java:57)
at Connection.<init>(Connection.java:20)
at Connection.main(Connection.java:102)
問題出在哪里?
另一個問題,當我進行查詢時,如何等待其他查詢? 我需要在兩次查詢之間等待
謝謝!!
IOException可能是這些原因:
如上所述,在您的情況下,流已關閉。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.