进程没有在java(Runtime)中正确终止

时间:2017-11-24 12:06:38

标签: java c# process mono csc

我正在使用java Process执行CSharp程序,方法是创建.exe文件,但该文件的进程没有响应0 exitCode。错误是空的。在Visual Studio中运行正常但是使用java,它会创建问题。没有输出也没有错误,我被困在这请求帮助。我使用的是Java 7。 我正在使用Csc (inbuild compiler in .Net framework for windows)它给了我dll引用错误。命令正在关注

csc /nologo /r:D:/simulatorConfig/ArrayConversion.dll /out:D:\\apache-tomcat-7.0.64\\temp\\tmp749792186557790590.exe D:\\apache-tomcat-7.0.64\\temp\\tmp749792186557790590.cs

stream = new BufferedReader(new InputStreamReader(proc.getErrorStream())); 

上面是Error,它是空的String。

代码在这里请看一下。

public File compile(File sourceFile, LANGUAGE lang) throws InterruptedException, IOException, CompilerException, ConfigurationException {
    String absolutePath = sourceFile.getCanonicalPath();
    // System.out.println("absolutePath : " + absolutePath);
    String destFile;
    if (OsUtils.isWindows()) {
        destFile = absolutePath.replace(lang.getFileExtension(), EXECUTABLE_FILE_SUFFIX);
    } else {
        destFile = absolutePath.replace(lang.getFileExtension(), "");
    }

    String compileCommand = generateCommand(absolutePath, destFile, lang);
    logger.error("compileCommand : " + compileCommand);
    // Compiles and create exe file for execution
    Process proc = Runtime.getRuntime().exec(compileCommand);

    // Wait for process to complete
    int returnValue = proc.waitFor();

    if (returnValue != 0) {
        String errorMsg = getCompilerMessage(sourceFile, proc, lang);
        throw new CompilerException(errorMsg);
    }

    proc.destroy();

    return new File(destFile);
}

private String getCompilerMessage(File sourceFile, Process proc, LANGUAGE lang) throws IOException {

    StringBuilder message = new StringBuilder();
    BufferedReader stream = null;
    String line = null;

    switch (lang) {
    case C:
    case CPP:
        // GNU C/CPP compiler prints compiler errors in standard errors
        // tream
        stream = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
        break;

    case CSHARP:
        // CSharp compiler prints compiler errors in standard output stream
        stream = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
        break;

    }

    while ((line = stream.readLine()) != null) {
        logger.error(line);
        line = line.substring(line.indexOf(sourceFile.getName()) + (int) sourceFile.getName().length());
        if (message.toString().isEmpty()) {
            message.append(lang == LANGUAGE.CSHARP ? "Line" : "").append(line);
        } else {
            message.append("<br/>Line").append(line);
        }
        // message.append(line).append(SystemUtils.LINE_SEPARATOR);
    }

    stream.close();

    return message.toString();
}

private String generateCommand(String sourceFile, String destFile, LANGUAGE lang) throws ConfigurationException {
    // System.out.println("sourceFile : " + sourceFile + " -- destFile : " +
    // destFile);
    Configuration config = new PropertiesConfiguration("system.properties");
    String cmd = "";
    switch (lang) {
    case C:
    case CPP:
        sourceFile = sourceFile.replace("\\", "\\\\");
        destFile = destFile.replace("\\", "\\\\");
        cmd = "g++ " + sourceFile + " -o " + destFile + " " + config.getString("C_CPP_HEADERFILE").trim();
        break;

    case CSHARP:
        sourceFile = sourceFile.replace("\\", "\\\\");
        destFile = destFile.replace("\\", "\\\\");
        logger.error("Config Path : "+config.getString("MONO_PATH"));
        if (OsUtils.isWindows()) {
            cmd = "csc /nologo /r:" + config.getString("CS_HEADERFILE_WIN") + " /out:" + destFile + " " + sourceFile;
        } else {
            cmd = "/opt/mono/bin/mcs /reference:" + config.getString("CS_HEADERFILE") + " /out:" + destFile + " "
                    + sourceFile;
        }

        break;
    }
    logger.info("Command :" + cmd);
    return cmd;
}

1 个答案:

答案 0 :(得分:0)

使用Runtime#exec启动命令时,必须在每种情况下消耗错误和std流(不仅在返回代码为!= 0时),否则内部缓冲区可能/将会满/ subprocess将无限期地等待,直到有人消耗它们。

这个问题的一个常见症状是一个永不返回的过程,使整个过程似乎陷入僵局。

有几种方法可以解决这个问题,其中一种方法最简单:https://docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.html#redirect-input

此外,这里似乎存在一个问题:

// CSharp compiler prints compiler errors in standard output stream
    stream = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
    break;

在注释中,您告诉编译器在stdout上输出错误,但您正在使用错误流。