简体   繁体   English

如何从Swing代码获取任何应用程序的根安装目录?

[英]How to get root installation directory of any application from swing code?

I am designing a patch installer for an application. 我正在为应用程序设计补丁程序安装程序。 The first page (after welcome page) of the installer asks for the root directory of the installed application and a Browse button is provided for the user to choose the root directory. 安装程序的第一页(欢迎页面之后)要求输入已安装应用程序的根目录,并提供了一个“浏览”按钮供用户选择根目录。 However, to facilitate the process I want the installer to intelligently find the root installer directory and set it as default in the text field rather than keeping it blank (As I have seen in many of the installers). 但是,为了简化此过程,我希望安装程序智能地找到根安装程序目录,并将其设置为文本字段中的默认目录,而不是将其保留为空白 (正如我在许多安装程序中所看到的)。 What is the best way to accomplish the same? 达到相同目的的最佳方法是什么?

private JButton getBrowseButton(JPanel panel) {
    final JButton browseBtn = new JButton(rm.getMessage("button.browse"));
    browseBtn.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent ae) {

            JFileChooser jfChooser = new JFileChooser(); 
            jfChooser.setCurrentDirectory(new java.io.File("."));
            jfChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            jfChooser.setAcceptAllFileFilterUsed(false);
            guiUtil.removeFilesOfTypeFromJFileChooser(jfChooser);     
            jfChooser.showOpenDialog(panel);
       }
    });
    return browseBtn;
}

I thought of using environment variables but the application does not set any environment variables which I could just System.getenv(String path) and get directly. 我考虑使用环境变量,但是应用程序未设置任何环境变量,而我只能通过System.getenv(String path)直接获取。

Any help would be appreciated. 任何帮助,将不胜感激。

Setting aside that it is probably a BAD IDEA to try to implement this yourself (see the Question comments) .... 撇开尝试自己实现它可能是一个糟糕的想法(请参阅问题注释)...。

There is no way that is going to work for all applications, because installation directories are a convention, not a requirement. 不可能对所有应用程序都起作用,因为安装目录是约定,而不是要求。 Rather, it is necessary to implement this on a per-application / per-platform basis. 相反,有必要在每个应用程序/每个平台的基础上实现此功能。 Here are some ideas. 这里有一些想法。

  • Wire the installation directory path into the wrapper script used to launch the application, and pass it as a system property, command line parameter or environment variable. 将安装目录路径连接到用于启动应用程序的包装器脚本中,并将其作为系统属性,命令行参数或环境变量进行传递。
  • Store the installation directory path in an application configuration file. 将安装目录路径存储在应用程序配置文件中。
  • Infer the installation directory path from location of the wrapper script. 从包装程序脚本的位置推断安装目录路径。
  • Infer the installation directory path from the classpath. 从类路径推断安装目录路径。
  • Store the installation directory path in the Windows registry, or similar. 将安装目录路径存储在Windows注册表中或类似内容中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM