java运行时进程exec

时间:2017-01-20 17:14:50

标签: java runtime mainframe

我需要做一个大型机包装器,大型机运行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)

问题出在哪里?有什么问题?

其他问题,当我查询时,如何等待其他查询?我需要在查询之间等待

谢谢!

2 个答案:

答案 0 :(得分:0)

IOException可能是这些的原因:

  • 读取网络文件并断开连接。
  • 读取不再可用的本地文件。
  • 使用某些流来读取数据,其他一些进程关闭了流。
  • 尝试读取/写入文件但未获得许可。
  • 尝试写入文件,但磁盘空间不再可用。
  • 还有更多的例子,但根据我的经验,这些是最常见的。

在您的情况下,如上所述,流已关闭。

答案 1 :(得分:0)

InputStreamReader(InputStream in,String charsetName)

而不是使用默认字符集,请尝试指定

另见Screen scraping with Java and X3270

相关问题