Search code examples
javac#processmonocsc

Process is not terminating Properly in java (Runtime)


I am executing CSharp Program with java Process by creating the .exe file but the process of that file is not responding with 0 exitCode.Errors are Empty.In Visual Studio it runs fine but with java, it creates problem.No output and no Errors, I am stuck in this Please help. I am using Java 7. I am using Csc (inbuild compiler in .Net framework for windows) It is giving me dll reference error.Command is following

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

above is Error which is empty String.

code is here Please look at it.

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;
}

Solution

  • There was .dll linking error in when I tried to do the same in command prompt with Demo files.By keeping .dll file and .exe file in the same directory it solved my purpose program was running Flawlessly with proper exit code (0). So kept other .dll file in this path D:\apache-tomcat-7.0.64\temp\. and is fine.

    The thumb rule says .dll and .exe should be in the same directoryeory