我正在尝试获取并销毁我在FXML应用程序中通过ProcessBuilder创建的外部进程,但它无效。这是基于有用的建议Sergey Grinev gave me here。
我尝试使用/不使用“// myController.setApp(this);”运行并使用“// super.stop();”在子类顶部和底部(请参阅MyApp中该行的注释/输入),但没有组合可用。
这可能与FXML或JavaFX无关,但我认为这是在JavaFX上开发应用程序的常见模式。我想我要求一个Java最佳实践来关闭像这样的基于UI的应用程序中的依赖进程(在这种情况下:基于FXML / JavaFX),其中有一个控制器类和一个应用程序类。
你能解释一下我做错了什么吗?或者更好:建议我应该做什么呢?感谢。
在我的应用程序中,我这样做:
public class MyApp extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
FXMLLoader fxmlLoader = new FXMLLoader();
Scene scene = (Scene)FXMLLoader.load(getClass().getResource("MyApp.fxml"));
MyAppController myController = (MyAppController)fxmlLoader.getController();
primaryStage.setScene(scene);
primaryStage.show();
// myController.setApp(this);
}
@Override
public void stop() throws Exception {
// super.stop();
// this is called on fx app close, you may call it in an action handler too
if (MyAppController.getScriptProcess() != null) {
MyAppController.getScriptProcess().destroy();
}
super.stop();
}
public static void main(String[] args) {
launch(args);
}
}
在我的控制器中,我这样做:
public class MyAppController implements Initializable {
private Application app;
private static Process scriptProcess;
public void setApp(Application a) {
app = a;
}
public static Process getScriptProcess() {
return scriptProcess;
}
}
当我使用“注释掉的setApp()”而不是注释掉(即,在start方法中保留)时,结果是以下,在启动后立即(主场景)闪烁,然后消失,然后出现此对话框:
"JavaFX Launcher Error:
Exception while running Application"
它还在控制台中提供了“应用程序启动方法中的异常”。
当我在上面的MyApp中省略“已注释掉的代码”(即从start方法中删除“setApp()”)时的结果是,我的应用程序确实关闭,但在关闭时会出现此错误:
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1440)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:69)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:217)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:170)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:38)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:37)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:53)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:28)
at javafx.event.Event.fireEvent(Event.java:171)
at javafx.scene.Node.fireEvent(Node.java:6863)
at javafx.scene.control.Button.fire(Button.java:179)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:193)
at com.sun.javafx.scene.control.skin.SkinBase$4.handle(SkinBase.java:336)
at com.sun.javafx.scene.control.skin.SkinBase$4.handle(SkinBase.java:329)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:64)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:217)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:170)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:38)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:37)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:53)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:33)
at javafx.event.Event.fireEvent(Event.java:171)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3324)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3164)
at javafx.scene.Scene$MouseHandler.access$1900(Scene.java:3119)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1559)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2261)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:228)
at com.sun.glass.ui.View.handleMouseEvent(View.java:528)
at com.sun.glass.ui.View.notifyMouse(View.java:922)
at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at com.sun.glass.ui.gtk.GtkApplication$3$1.run(GtkApplication.java:82)
at java.lang.Thread.run(Thread.java:722)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1435)
... 44 more
Caused by: java.lang.NullPointerException
at mypackage.MyController.handleCancel(MyController.java:300)
... 49 more
Clean up...
答案 0 :(得分:9)
方法是对的,但问题很少。
您已创建名为FXMLLoader
的{{1}},但之后您调用fxmlLoader
这是静态方法,并且与您之前创建的实例无关。
使用静态方法进行相互通信也不是很好(想象一下你会想要几个进程)。最好将FXMLLoader.load()
存储到字段中,然后使用myController
方法调用它。
更好的方法是将相应的实用工具方法添加到Controller并从主应用程序中调用它,因为主应用程序似乎并没有使用stop()
本身。
这里有一个简短的应用程序来演示所有描述:
Process
控制器:
public class DoTextAreaLog extends Application {
private LoggController controller;
@Override
public void start(Stage stage) throws IOException{
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("logg.fxml"));
VBox root = (VBox)fxmlLoader.load();
controller = (LoggController) fxmlLoader.getController();
stage.setScene(new Scene(root, 400, 300));
stage.show();
}
@Override
public void stop() throws Exception {
super.stop();
controller.destroy();
}
public static void main(String[] args) { launch(); }
}
logg.fxml:
public class LoggController implements Initializable {
@FXML private TextArea textarea;
@FXML private void onAction(ActionEvent event) {
destroy();
}
private Process p;
public void destroy() {
if (p != null) {
p.destroy();
}
}
@Override
public void initialize(URL url, ResourceBundle rb) {
try {
p = new ProcessBuilder("ping", "stackoverflow.com", "-n", "100").start();
new Thread(new Runnable() {
@Override
public void run() {
try {
try (BufferedReader bri = new BufferedReader(new InputStreamReader(p.getInputStream()))) {
String line;
while ((line = bri.readLine()) != null) {
log(line);
}
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}).start();
} catch (IOException ex) {
ex.printStackTrace();
}
}
private void log(final String st) {
Platform.runLater(new Runnable() {
@Override
public void run() {
textarea.setText(st + "\n" + textarea.getText());
}
});
}
}