我正在尝试在远程Windows机器上从java执行c ++代码。为了处理远程部分,我创建了一个Web服务,使用Runtime.exec()运行实际命令。不直接从java代码调用c ++ exe。我有一个最终调用exe的批处理文件。
问题是,java和c ++进程都挂起了。服务器端的java代码确实处理输出流和错误流。此外,c ++代码将所有内容记录在Windows上的文件中。奇怪的是,当我删除WS调用并在服务器端运行java代码作为独立的java程序时,它会成功。这是java代码:
public class RunCPlusPlusExecutable {
public int runExecutable() {
int exitValue = 0;
try {
Process p = null;
Runtime rt = Runtime.getRuntime();
System.out.println("About to execute" + this + rt);
p = rt.exec("c:/temp/execcplusplus.bat");
System.out.println("Process HashCode=" + p.hashCode());
StreamProcessor errorHandler = new StreamProcessor(p.getErrorStream(), "Error");
StreamProcessor outputHandler = new StreamProcessor(p.getInputStream(), "Output");
errorHandler.start();
outputHandler.start();
exitValue = p.waitFor();
System.out.println("Exit value : " + exitValue);
if (exitValue == 0)
System.out.println("SUCCESS");
else
System.out.println("FAILURE");
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (Exception e) {
}
return exitValue;
}
class StreamProcessor extends Thread {
private InputStream is = null;
private String type = null;
private InputStreamReader isr = null;
private BufferedReader br = null;
private FileWriter writer = null;
private BufferedWriter out = null;
StreamProcessor(InputStream is, String type) {
this.is = is;
this.type = type;
}
public void run() {
try {
isr = new InputStreamReader(is);
br = new BufferedReader(isr);
writer = new FileWriter("*******path to log file********");
out = new BufferedWriter(writer);
String line = null;
while ((line = br.readLine()) != null) {
Date date = new Date();
out.write("[" + type + "]: " + date + " : " + line);
out.newLine();
}
writer.flush();
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
if (br != null)
br.close();
if (isr != null)
isr.close();
if (out != null)
out.close();
if (writer != null)
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
知道导致问题的原因以及如何调试它?请注意,我将无法调试c ++代码。
由于
更新1: 这里有一些更多细节...... WS服务器是从某个管理员用户运行的。我一直在运行其他用户的独立java程序。 * 似乎c ++可执行文件在从WS调用执行时给出了引用的内存错误。弹出窗口通过“确定”和“取消”按钮引用错误。 *
更新2: 部署WS的tomcat服务器作为Windows NT服务运行。这可能是错误的原因吗?如果是,如何解决这个问题?