开发者

Executable Jar installation path

For an executable Jar with a Main Class in the Manifest:

When I launch it using java -jar myjar.jar , how can I find out the installation di开发者_如何学Gorectory of this jar at runtime?

What I want to do is develop a command-line client for Flyway.

The tool will be installed with the following folder structure:

INSTALLATION_PATH
|
-- bin
|   |
|   --start.sh (launches flyway.jar)
|
-- lib
|   |
|   --flyway.jar (contains Main class, loads flyway.properties)
|
-- conf
    |
    --flyway.properties (configuration)

How can flyway.jar resolve INSTALLATION_PATH?


You can try :

// this generally returns the PWD
String pwd = System.getProperties().getProperty("user.dir"); 

// or you can get the location of the URL
// from the .jar file in which your class was loaded
// you may want to then simply back it up a level with ".."
URL yourJar = getClass().getProtectionDomain().getCodeSource().getLocation();


the following will give you the starting directory:

new File(".").getAbsolutePath()


A preferred way of doing this instead of trying to find out the installation directory at runtime is this:
Do the install, and then edit the system classpath properties to add this jar and any dependant jars.
After that, you can run it from any directory.
The benefit is of course that this way , you set the classpath only once at installation time. No need to find installation directory at runtime every time you decide to run it.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜