进程未在 java(运行时)中正确终止



我正在通过创建.exe文件来执行 java 进程的 CSharp 程序,但该文件的进程没有响应 0exitCode.错误是 Empty.In 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())); 

以上是空字符串的错误。

代码在这里请看一下。

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

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

这个问题的共同症状是一个永远不会返回的过程,使整体似乎陷入僵局。

有几种方法可以纠正此问题,最简单的方法如下所述: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 上输出错误,但您正在使用错误流。

当我尝试在命令提示符下对演示文件执行相同操作时,存在.dll链接错误。通过将.dll文件和.exe文件保留在同一目录中,它解决了我的目的程序以正确的退出代码(0)完美运行。 所以在这个路径中保留了其他.dll文件D:apache-tomcat-7.0.64temp.而且很好。

经验法则说.dll.exe应该在同一个目录中

相关内容

最新更新