我希望能夠在我的Java程序中啓動VI,並在繼續之前等待用戶退出VI。下面的代碼片段,我目前:如何在commons-exec下從Java內部啓動VI?
...
String previewFileName="test.txt"; // the file to edit
CommandLine cmdLine = new CommandLine("/usr/bin/vi");
cmdLine.addArgument(previewFileName);
cmdLine.addArgument(">/dev/tty");
cmdLine.addArgument("</dev/tty");
Executor executor = new DefaultExecutor();
try
{
DefaultExecuteResultHandler resultHandler = new ResetProcessResultHandler(cmdLine);
executor.execute(cmdLine, resultHandler);
} catch (IOException e)
{
throw new Error("Cannot execute command: /usr/bin/vi " + previewFileName, e);
}
log.info("waiting...");
cmdLine.wait();
log.info("...done");
...
private class ResetProcessResultHandler extends DefaultExecuteResultHandler
{
private final CommandLine mCommandLine;
public ResetProcessResultHandler(CommandLine pCommandLine)
{
mCommandLine = pCommandLine;
}
public void onProcessComplete(int exitValue)
{
log.info("Command complete rc(" + exitValue + ")");
if (exitValue != 0)
{
throw new RuntimeException("VI command error [rc=" + exitValue + "] ");
}
mCommandLine.notify();
}
public void onProcessFailed(ExecuteException e)
{
if (e.getExitValue() != 0)
{
log.error("launch VI error " + e.toString());
throw new RuntimeException("VI command failed [" + e.getCause() + "] ");
}
else
{
log.info("VI complete rc(" + e.getExitValue() + ")");
}
mCommandLine.notify();
}
}
我收到輸出:
Vim: output is not to a terminal
Vim: input is not from a terminal
但後來我看到畫彷彿VI已經開始屏幕;而VI不會讀取我輸入的字符。
所以...從/ dev/tty重定向並沒有這樣做。
有人必須做到這一點 - 幫助!
感謝,
馬克
這是行不通的,因爲vi沒有被帶到前臺並附加到用戶的tty(這顯然是問題的真正要求)。 – 2013-06-27 20:57:37