可执行Jar安装路径

Axe*_*ine 1 java installation classpath executable-jar

对于Manifest中具有Main Class的可执行Jar:

当我使用java -jar myjar.jar启动它时,如何在运行时找到此jar的安装目录?

我想要做的是为Flyway开发一个命令行客户端.

该工具将安装以下文件夹结构:

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

flyway.jar如何解析INSTALLATION_PATH?

Jé *_*eue 6

你可以试试 :

// 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();
Run Code Online (Sandbox Code Playgroud)