如何获取用户安装Java应用程序的路径?

时间:2021-11-01 20:14:48

I want to bring up a file dialog in Java that defaults to the application installation directory.

我想在Java中打开一个默认为应用程序安装目录的文件对话框。

What's the best way to get that information programmatically?

以编程方式获取该信息的最佳方法是什么?

2 个解决方案

#1


7  

System.getProperty("user.dir") 

gets the directory the Java VM was started from.

获取Java VM启动的目录。

#2


4  

System.getProperty("user.dir");

The above method gets the user's working directory when the application was launched. This is fine if the application is launched by a script or shortcut that ensures that this is the case.

启动应用程序时,上述方法获取用户的工作目录。如果应用程序是由脚本或快捷方式启动的,那么这很好,可以确保这种情况。

However, if the app is launched from somewhere else (entirely possible if the command line is used), then the return value will be wherever the user was when they launched the app.

但是,如果应用程序是从其他地方启动的(如果使用命令行则完全可能),那么返回值将是用户启动应用程序时的任何位置。

A more reliable method is to work out the application install directory using ClassLoaders.

更可靠的方法是使用ClassLoaders计算应用程序安装目录。

#1


7  

System.getProperty("user.dir") 

gets the directory the Java VM was started from.

获取Java VM启动的目录。

#2


4  

System.getProperty("user.dir");

The above method gets the user's working directory when the application was launched. This is fine if the application is launched by a script or shortcut that ensures that this is the case.

启动应用程序时,上述方法获取用户的工作目录。如果应用程序是由脚本或快捷方式启动的,那么这很好,可以确保这种情况。

However, if the app is launched from somewhere else (entirely possible if the command line is used), then the return value will be wherever the user was when they launched the app.

但是,如果应用程序是从其他地方启动的(如果使用命令行则完全可能),那么返回值将是用户启动应用程序时的任何位置。

A more reliable method is to work out the application install directory using ClassLoaders.

更可靠的方法是使用ClassLoaders计算应用程序安装目录。