【发布时间】:2017-11-24 12:06:37
【问题描述】:
我正在通过创建 .exe 文件使用 java 进程执行 CSharp 程序,但该文件的进程没有响应 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,它是空字符串。
代码在这里,请看。
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;
}
【问题讨论】:
-
你没有消费流...
-
好像你没有关闭 InputStreamReader,女巫正在创建一个
BufferedReaderctor -
我遇到了类似的问题,通过添加对 Environment.Exit(1); 的调用解决了这个问题。在 c# main 方法的末尾。现在它将 1 作为返回码返回给 java。
-
Java 流..no
-
是的,它返回退出代码为 1 在正常情况下应该是 0