Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 700
Bill the Lizard Avatar asked Dec 17 '22 10:12

Bill the Lizard


2 Answers

System.getProperty("user.dir") 

gets the directory the Java VM was started from.

like image 85
Rich Lawrence Avatar answered Jan 06 '23 15:01

Rich Lawrence


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.

like image 45
McDowell Avatar answered Jan 06 '23 15:01

McDowell