Javafx getHostServices().getCodeBase()返回一个空字符串

Javafx getHostServices().getCodeBase()返回一个空字符串,javafx,Javafx,我在javafx应用程序的start方法中调用getHostServices().getCodeBase(),但它返回一个空字符串????。 来自应用程序构造函数和来自start方法的调用都返回空字符串 public class MyApplication extends Application { private HostServices hostService; private MyController controller ; priva

我在javafx应用程序的start方法中调用getHostServices().getCodeBase(),但它返回一个空字符串????。 来自应用程序构造函数和来自start方法的调用都返回空字符串

 public class MyApplication extends Application {

        private HostServices hostService;
        private MyController controller ;
        private Parent root;
        private HostServices hostService;

    public MyApplication() throws IOException  {

        hostService = getHostServices();
        System.out.println("Codebase : "+hostService.getCodeBase() ); // return empty string 

    }

        @Override
        public void start(Stage primaryStage) {

            this.primaryStage = primaryStage;

            try {

                FXMLLoader fxmlLoader = new FXMLLoader(this.getClass().getResource("/mypackage/my.fxml") ); 
                controller = new MyController();
                fxmlLoader.setController(controller);
                fxmlLoader.load();
                root = fxmlLoader.getRoot();

                Scene scene = new Scene(root,1200,1000); 
                primaryStage.setScene(scene);
                primaryStage.show();
                 System.out.println("Codebase : "+getHostServices().getCodeBase() ); // return empty string 

            } catch(Exception e) {
                e.printStackTrace();
            }
        }  
这是一种非常奇怪的情况,因为应用程序部署到web上,因此获取代码库非常重要。

从javadoc:

如果应用程序未打包在jar文件中,此方法将返回空字符串

 public class MyApplication extends Application {

        private HostServices hostService;
        private MyController controller ;
        private Parent root;
        private HostServices hostService;

    public MyApplication() throws IOException  {

        hostService = getHostServices();
        System.out.println("Codebase : "+hostService.getCodeBase() ); // return empty string 

    }

        @Override
        public void start(Stage primaryStage) {

            this.primaryStage = primaryStage;

            try {

                FXMLLoader fxmlLoader = new FXMLLoader(this.getClass().getResource("/mypackage/my.fxml") ); 
                controller = new MyController();
                fxmlLoader.setController(controller);
                fxmlLoader.load();
                root = fxmlLoader.getRoot();

                Scene scene = new Scene(root,1200,1000); 
                primaryStage.setScene(scene);
                primaryStage.show();
                 System.out.println("Codebase : "+getHostServices().getCodeBase() ); // return empty string 

            } catch(Exception e) {
                e.printStackTrace();
            }
        }  
此外:

如果应用程序是通过JNLP文件启动的,则此方法返回JNLP文件中指定的codebase参数

对于
hostServices.getCodeBase()
,要返回预期值,必须根据上述准则对应用程序进行适当打包


根据所使用的打包工具,打包应用程序的方式会有所不同,因此请参阅工具供应商的文档,以了解有关使用所选开发环境打包应用程序的信息。

我使用eclipse进行开发和测试运行,然后进行部署,因此当我只运行测试时,会得到空字符串,您能解释一下吗?谢谢。好的,即使在start方法中,相同的结果也是空字符串??打包和部署后,返回正确的主机名我认为eclipse在测试应用程序时存在问题,我是说使用run命令。