JavaFX2-用于在没有GUI的情况下启动应用程序的选项 [英] JavaFX2 - option for starting application without GUI

查看:107
本文介绍了JavaFX2-用于在没有GUI的情况下启动应用程序的选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发将在Linux下运行的JavaFx应用程序,既可以在支持GUI的环境中也可以在不支持GUI的环境中运行. 意思是,如果我连接到启动应用程序时将使用"ssh -X"运行应用程序的计算机,则应该打开GUI,如果我仅使用"ssh"(不带-X)进行连接,则应启动应用程序的控制台版本.

使用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",但应用程序不会结束其执行,而是会挂在那里.

您能帮我实现这个目标吗?

解决方案

我不确定究竟是什么导致您面临此问题.但是,已知JavaFX存在线程问题,这可能与JavaFX Main Class产生子线程有关.但是我还不足够知识来回答这个问题.

但是我可以做的是,如果您正在寻找一种替代方法,请提供一种替代方法.您可以使用main方法创建一个单独的类(不扩展Application类),然后从那里调用Main类,这样,如果传递 nogui ,则您的应用程序将退出.

public class NewMain {
    public static void main(String[] args) throws Exception {
        if (args.length == 1 && args[0].equals("nogui")) {
            System.out.println("NOGUI SELECTED");
        } else {
            Main.launch(Main.class, args);
        }
    }
}

如果您不想传递参数,则可以简单地使用Main.launch(Main.class)而不是Main.launch(Main.class, args).我尚未对此进行测试,但它应该可以工作.

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?

解决方案

I am not sure what exactly is causing this issue that you are facing. But JavaFX is known to have thread issues, this could be related to JavaFX Main Class spawning child thread(s). But I am not knowledgeable enough to answer that.

But what I can do is, provide an alternative, if you are looking for one. You can create a separate class (Not extending Application class) with the main method and then call the Main class if required from there, this way your application will exit if nogui is passed.

public class NewMain {
    public static void main(String[] args) throws Exception {
        if (args.length == 1 && args[0].equals("nogui")) {
            System.out.println("NOGUI SELECTED");
        } else {
            Main.launch(Main.class, args);
        }
    }
}

If you don't wish to pass arguments, then you can simply use Main.launch(Main.class) instead of Main.launch(Main.class, args). I have not tested this out, but it should work.

这篇关于JavaFX2-用于在没有GUI的情况下启动应用程序的选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆