简体   繁体   English

Java运行时进程执行

[英]java runtime process exec

i need to do a mainframe wrapper, the mainframe runs Music SP and it can be access by TN3270 terminal. 我需要做一个主机包装,该主机运行Music SP,并且可以通过TN3270终端进行访问。

i use the terminal emulator x3270 and i need to connect with it in java. 我使用终端仿真器x3270,并且需要在Java中与其连接。 I tried with java run time: 我尝试使用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;
    }
}

But when i execute i get: 但是当我执行我得到:

> 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)

Where and what is the problem? 问题出在哪里?

Other question, when i do a query, how i can wait to do other query? 另一个问题,当我进行查询时,如何等待其他查询? i need to wait between querys 我需要在两次查询之间等待

thanks!! 谢谢!!

IOException can be the cause of these: IOException可能是这些原因:

  • Reading a network file and got disconnected. 读取网络文件并断开连接。
  • Reading a local file that was no longer available. 读取不再可用的本地文件。
  • Using some stream to read data and some other process closed the stream. 使用某些流读取数据,并使用其他一些进程关闭该流。
  • Trying to read/write a file but don't have permission. 尝试读取/写入文件,但没有权限。
  • Trying to write to a file but disk space was no longer available. 尝试写入文件,但磁盘空间不再可用。
  • There are many more examples, but these are the most common, in my experience. 在我的经验中,还有更多示例,但是这些是最常见的示例。

In your case, as stated above, stream was closed. 如上所述,在您的情况下,流已关闭。

InputStreamReader(InputStream in, String charsetName) InputStreamReader(InputStream in,字符串charsetName)

instead of using default charset, try specifying it 而不是使用默认字符集,请尝试指定它

also see Screen scraping with Java and X3270 另请参阅使用Java和X3270进行屏幕抓取

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM