ProcessBuilder hangs on Tomcat Webapp

时间:2017-05-16 09:39:40

标签: java jsp tomcat

I have a Webapp written in JSP & Java which has function to execute Java file submitted by user(s). Code below is the method to execute Java file:

private static int execute2(File fileToExecute, File inputFile) {
    try {
        if (OSUtils.getType().equals(OSConstants.WINDOWS)) {
            processBuilder = new ProcessBuilder("cmd.exe", "/c", "java", "-cp", "\"" + getParentAbsolutePath(fileToExecute) + "\"", getMainClassName(fileToExecute), "<", "\"" + inputFile.getAbsolutePath() + "\"");

        } else if (OSUtils.getType().equals(OSConstants.UNIX)) {
            processBuilder = new ProcessBuilder("java", "-cp", getParentAbsolutePath(fileToExecute), getMainClassName(fileToExecute), "<", inputFile.getAbsolutePath());
        }
        process = processBuilder.start();

        inputGobbler = new ProgramStreamGobbler(process.getInputStream());
        inputGobbler.start();

        errorGobbler = new ProgramStreamGobbler(process.getErrorStream());
        errorGobbler.start();

        errorStatus = process.waitFor();
    } catch (IOException | InterruptedException e) {
        e.printStackTrace();
    }

    return errorStatus;
}

The Java file(s) executed perfectly in Windows environment. However, in Linux, the page keeps loading infinitely. How do I prevent this? Please enlighten me on this problem.

0 个答案:

没有答案