外部程序关闭时的条件
Condition for when outside program closes
我有一个程序,当出现提示时,它会打开一个外部编辑器(目前硬编码为 sublime)。然后用户将在编辑器中输入他们正在输入的任何内容,保存临时文件,然后关闭编辑器。当用户关闭编辑器时,我希望在程序中显示临时文件的内容。我主要关心的是创建一个条件来判断编辑器何时关闭。 WindowListener 可以用于引用正在启动的外部程序吗?到目前为止,这是我的代码:(注意:由于与 Desktop 和我当前版本的 Gnome 的兼容性问题,我使用运行时。这只会是 运行 on Linux。)
private CachedTextInfo cti;
private File temp = File.createTempFile("tempfile",".tmp");
try{
theText.setText(cti.initialText);
String currentText = theText.getText();
BufferedWriter bw = new BufferedWriter(new FileWriter(temp));
bw.write(currentText);
bw.close();
Runtime.getRuntime().exec("subl "+ temp.getAbsolutePath());
//When editor closes, display tmp contents
}catch(IOException e) {
e.printStackTrace();
}
谢谢,如果您需要任何其他信息,请告诉我。
Runtime.exec()
returns a Process
instance, which has a waitFor()
方法。
所以你可以做
Process p = Runtime.getRuntime().exec("subl "+ temp.getAbsolutePath());
try {
p.waitFor();
// display tmp contents...
} catch (InterruptedException exc) {
// thread was interrupted waiting for process to complete...
}
我有一个程序,当出现提示时,它会打开一个外部编辑器(目前硬编码为 sublime)。然后用户将在编辑器中输入他们正在输入的任何内容,保存临时文件,然后关闭编辑器。当用户关闭编辑器时,我希望在程序中显示临时文件的内容。我主要关心的是创建一个条件来判断编辑器何时关闭。 WindowListener 可以用于引用正在启动的外部程序吗?到目前为止,这是我的代码:(注意:由于与 Desktop 和我当前版本的 Gnome 的兼容性问题,我使用运行时。这只会是 运行 on Linux。)
private CachedTextInfo cti;
private File temp = File.createTempFile("tempfile",".tmp");
try{
theText.setText(cti.initialText);
String currentText = theText.getText();
BufferedWriter bw = new BufferedWriter(new FileWriter(temp));
bw.write(currentText);
bw.close();
Runtime.getRuntime().exec("subl "+ temp.getAbsolutePath());
//When editor closes, display tmp contents
}catch(IOException e) {
e.printStackTrace();
}
谢谢,如果您需要任何其他信息,请告诉我。
Runtime.exec()
returns a Process
instance, which has a waitFor()
方法。
所以你可以做
Process p = Runtime.getRuntime().exec("subl "+ temp.getAbsolutePath());
try {
p.waitFor();
// display tmp contents...
} catch (InterruptedException exc) {
// thread was interrupted waiting for process to complete...
}