从java中的Runtime.exec()捕获输入会使系统处于挂起状态

时间:2017-02-13 16:49:33

标签: java algorithm runtime.exec

我正在尝试实现一个程序,它可以帮助我在JAva中使用Runtime.exec()运行Java,Python或C程序。为了限制用户创建非常长的输出,我正在逐位读取输入流,然后将其附加到字符串缓冲区。我的问题是,当我运行一个编写程序的简单测试用例时,只输出一个计数器整数,只要缓冲区未满,我得到输出,但程序在到达bufferCounter并且挂起时不会停止。

The InputReader Module

private String readCompilerOutput(InputStream inputStream) {

        InputStreamReader isr = new InputStreamReader(inputStream);
        BufferedReader br = new BufferedReader(isr);
        StringBuffer strbuff = new StringBuffer();
        int ch = -1;
        try {
            ch = br.read();
            if(ch==-1)
                return null;
            int counter = 0;
            while (ch != -1 && counter < bufferCounter - 1) {
                strbuff.append(Character.toChars(ch));
                ch = br.read();
                counter++;
            }

        } catch (Throwable e) {
            // TODO Auto-generated catch block

            result.setOutputErrorFlag(true);
            result.setOutputError("Oops! IO Exception");
        }

        if (ch != -1)
            strbuff.append(Character.toChars(ch));

        return strbuff.toString();

    }

输入阅读器从中获取IO流的编译和执行模块。

private CompiledResultImpl doExecute(String[] command) {
        try {
            rt = Runtime.getRuntime();
            proc = rt.exec(command);
            String output = readCompilerOutput(proc.getInputStream());
            int exitVal = proc.waitFor();
            if (exitVal != 0) {
                result.setOutputErrorFlag(true);
                result.setOutputError(readCompilerOutput(proc.getErrorStream()));
            }

            result.setOutputResult(output);



        } catch (Throwable t) {

            t.printStackTrace();
            result.setOutputErrorFlag(true);
            result.setOutputError(readCompilerOutput(proc.getErrorStream()));

        }

        return result;

    }

    private CompiledResultImpl doComplie(String[] command) {
        try {

            rt = Runtime.getRuntime();
            proc = rt.exec(command);
            String output = readCompilerOutput(proc.getInputStream());
            int exitVal = proc.waitFor();
            if (exitVal != 0) {
                result.setCompileErrorFlag(true);
                result.setCompileError(readCompilerOutput(proc.getErrorStream()));
            }
            result.setCompileResult(output);

        } 

        catch (Throwable t) {
            t.printStackTrace();
            result.setCompileErrorFlag(true);
            result.setCompileError(readCompilerOutput(proc.getErrorStream()));

        }



        return result;

    }

最后调用方法

public CompiledResultImpl doRun(String snippetPath, int timeout) {

        String parm = "Solution";
        String javapath = "//apps//java/jdk1.7.0_80//bin//";
        String[] java_compile = { javapath + "javac", snippetPath + parm + ".java" };
        String[] java_execute = { javapath + "java","-cp", snippetPath, parm};
        CompiledResultImpl result;
        result = doComplie(java_compile);
        if (!result.isCompileErrorFlag())
            result = doExecute(java_execute);

        return result;
    }

本例程序如下:

public class Solution{

public static void doPrint(){
 for(int i=0;i<50000;i++){
        System.out.println(i);

}

}


public static void main(String args[]){

                doPrint();
        }

}

我得到的输出如果我没有超过我设置的BufferCounter ......

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
.
.
.
.
.
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999

0 个答案:

没有答案