简体   繁体   English

可执行Jar的安装路径

[英]Executable Jar installation path

For an executable Jar with a Main Class in the Manifest: 对于清单中具有Main Class的可执行Jar:

When I launch it using java -jar myjar.jar , how can I find out the installation directory of this jar at runtime? 使用java -jar myjar.jar启动它时, 如何在运行时查找此jar的安装目录?

What I want to do is develop a command-line client for Flyway . 我要做的是为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? flyway.jar如何解决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();

以下将为您提供起始目录:

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. 执行安装,然后编辑系统类路径属性以添加此jar和任何相关的jar。
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. 每次您决定运行安装目录时,都无需在运行时查找安装目录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM