可执行Jar安装路径

发布于 2024-10-11 22:47:08 字数 516 浏览 2 评论 0原文

对于清单中包含主类的可执行 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)

flyway.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 directory 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

葮薆情 2024-10-18 22:47:08

您可以尝试:

// 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();

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();
星軌x 2024-10-18 22:47:08

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

new File(".").getAbsolutePath()

the following will give you the starting directory:

new File(".").getAbsolutePath()
再见回来 2024-10-18 22:47:08

执行此操作的首选方法是:
进行安装,然后编辑系统类路径属性以添加此 jar 和任何依赖的 jar。
之后,您可以从任何目录运行它。
当然,这样做的好处是,您只需在安装时设置一次类路径。每次决定运行它时,无需在运行时查找安装目录。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文