开发者

Detecting your application's install path in Java?

I have made a small application in Java and I would like to make a windows installer for it using the Nullsoft Scriptable Install System (http://nsis.sourceforge.net/Main_Page).

The application I made needs to save user preferences somewhere and it currently saves it in the user's home directory (e.g. c:\Users\danny or /home/users/danny). However if the windows installer installs the application to e.g. c:\Program Files\whatever\ I should probably save the preferences file there too, right?

How would I detect that directory path in Java? What would be a good cross-platform approach to this without losing the benefits of a windows uninstaller for windows users e.g. start menu icons, installer option, etc?

Should I just continue saving my 开发者_运维百科preferences in the user's home path and clutter it up?

Thanks very much,


the best place for saving user's preference is user home directory. Also it is good from security point of view.

To get the path from java:

System.getProperty("user.home");

It is cross platform. (/home/username in Linux (or like this depends on OS configuration) C:\Documents and Seetings\username in windows)

More properties: http://www.mindspring.com/~mgrand/java-system-properties.htm

Also, make sure that you app has enough privileges (for applet it should be signed)


I would stick with the user's home directory for saving preferences.

Alternatively, you can use the Preferences API.

With the preferences API, you can store both application and user preferences. The downside is that the implementation is in control of where those files get stored. By default, in Windows it uses the registry.

import java.util.prefs.Preferences;

// System node
Preferences systemPrefs = Preferences.systemNodeForPackage(YourClass.class);
// User node
Preferences userPrefs = Preferences.userNodeForPackage(YourClass.class);

systemPrefs.put("My Preference", "A string");
String myPreference = systemPrefs.get("My Preference");
userPrefs.putInt("A number", 1);
int aNumber = userPrefs.getInt("A number");


However if the windows installer installs the application to e.g. c:\Program Files\whatever\ I should probably save the preferences file there too, right?

No, you absolutely should not. Computers can have more than one user, and a normal user account does not have write acces to program installation directories. Shortsighted programmers who write code assuming that a computer has only one user who can do everything are the reason why there are so many crappy apps that made it impossible to run Windows XP in a properly secure configuration and require Vista and Windows 7 to do weird workarounds.

The user's home directory is exactly the right place for preferences files - that way, they'll also be backed up (nobody wants to back up program binaries). Additionally, consider using the Java preferences API instead of wasting time implementing your own scheme.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜