我正在開發將在Linux下運行的JavaFx應用程序,兩者都支持GUI和不支持GUI的環境。 這意味着,如果我連接到應用程序啓動GUI時應用程序將以「ssh -X」運行的機器,並且如果我使用「ssh」(不帶-X)連接,則應啓動應用程序版本的應用程序。JavaFX2 - 用於在沒有GUI的情況下啓動應用程序的選項
如何在使用JavaFx時實現此目的?
我嘗試以下列方式:
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
FXMLLoader loader = new FXMLLoader(getClass().getClassLoader().getResource("MainGui.fxml"));
SplitPane page = null;
try {
page = (SplitPane) loader.load();
} catch (IOException e) {
System.exit(1);
}
Scene scene = new Scene(page);
primaryStage.setScene(scene);
primaryStage.setResizable(false);
primaryStage.show();
}
public static void main(String[] args) {
if (args.length == 1 && args[0].equals("nogui")) {
System.out.println("NOGUI SELECTED");
} else {
launch(args);
}
}
}
但它沒有工作,當我試圖通過SSH連接到另一臺機器沒有-X選項,我仍然收到錯誤:
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.UnsupportedOperationException: Unable to open DISPLAY
at com.sun.glass.ui.gtk.GtkApplication.<init>(GtkApplication.java:68)
at com.sun.glass.ui.gtk.GtkPlatformFactory.createApplication(GtkPlatformFactory.java:41)
at com.sun.glass.ui.Application.run(Application.java:146)
at com.sun.javafx.tk.quantum.QuantumToolkit.startup(QuantumToolkit.java:257)
at com.sun.javafx.application.PlatformImpl.startup(PlatformImpl.java:211)
at com.sun.javafx.application.LauncherImpl.startToolkit(LauncherImpl.java:675)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:337)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
... 5 more
我也注意到,如果我在GUI環境下運行應用程序,給出「nogui」命令行選項,我會收到打印輸出「NOGUI SELECTED」,但應用程序不會結束它的執行,而只會掛在那裏。
你能幫我怎麼做到嗎?
您是如何創建JAR文件的?裏面的Manifest.MF文件是什麼?它可能被配置爲啓動JavaFX'Application'類而不調用'main'方法。 編輯:其實,這可能只適用於使用JavaFX Packager,我不確定。 – Itai
我在Manifest.MF中看到我有主要方法的路徑。 – Xyster
你如何運行你的應用程序? – Itai