I'm developing JavaFx application that will be run under Linux, both in environments that support GUI and environments that do not support GUI. Meaning, if I connect to machine where application will be run with "ssh -X" when application is started GUI should open, and if I connect using just "ssh" (without -X) then console version of application should start.
How can I achieve this when using JavaFx?
I tried it in the following way:
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);
}
}
}
But it didn't work, and when I tried to connect via SSH to another machine without -X option, I still receive error:
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
I also noticed if I run application in environment with GUI, giving "nogui" command line option, I would receive printout "NOGUI SELECTED", but application would not end it's execution instead it would just hang there.
Can you help me how I can achieve this?