我正在开发将在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