Bill the Lizard
Bill the Lizard

Reputation: 405735

How do I get the path where the user installed my Java application?

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

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

Upvotes: 4

Views: 4609

Answers (2)

McDowell
McDowell

Reputation: 108869

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.

Upvotes: 4

Rich Lawrence
Rich Lawrence

Reputation: 1670

System.getProperty("user.dir") 

gets the directory the Java VM was started from.

Upvotes: 7

Related Questions