【发布时间】:2015-08-08 07:54:00
【问题描述】:
我正在使用 Java 作为我正在编写的国际象棋 AI 的前端。 Java 处理所有图形,然后使用一些命令行参数执行一些 C。有时 C 永远不会完成,也不会回到 Java。我发现了发生这种情况的案例,并仅使用 .exe 而没有 java 对其进行了测试。当我取出 java 时,这些情况每次都有效。我不知道从这里去哪里。这是一些我认为相关的代码,整个项目位于https://github.com/AndyGrant/JChess
try{
Process engine = Runtime.getRuntime().exec(buildCommandLineExecuteString(lastMove));
engine.waitFor();
int AImoveIndex = engine.exitValue();
String line;
BufferedReader input = new BufferedReader(new InputStreamReader(engine.getInputStream()));
while ((line = input.readLine()) != null)
System.out.println(line);
input.close();
if (AImoveIndex == -1){
activeGame = false;
System.out.println("Fatal Error");
while (true){
}
}
else{
JMove AIMove = JChessEngine.getAllValid(types,colors,moved,lastMove,!gameTurn).get(AImoveIndex);
AIMove.makeMove(types,colors,moved);
lastMove = AIMove;
validMoves = JChessEngine.getAllValid(types,colors,moved,lastMove,gameTurn);
}
waitingOnComputer = false;
parent.repaint();
}
catch(Exception e){
e.printStackTrace();
}
【问题讨论】:
-
看不出您发布的内容。不愿意通读那个项目。
-
我正在寻找使用 java 中的 RunTime 执行 C 程序时可能出现的错误。
-
如果“C 永远不会完成”,你能证明你的 C 代码没有在某个地方陷入无限循环吗?如果您在 C 程序中的
main函数返回之前放置一个print语句,它会打印吗?仅仅因为您手动为 C 程序提供了正确的输入并且它终止并不一定意味着 C 程序在与 Java 程序集成时应该终止。例如,您的 Java 程序可能错误地格式化输入。我不会马上说你在运行时犯了一些奇怪的错误。