java - Ant Mac OS X Appbundler 当前目录路径问题

标签 java macos ant app-bundle working-directory

我正在尝试将 Java 应用程序编译到 Mac OS X 应用程序包中。我添加以下设置来设置当前工作目录:

<bundleapp...>
    ...
    <option value="-Duser.dir=$APP_ROOT/Contents/Resources"/>
</bundleapp>

在Contents/Resources/中有一个config目录。

执行时,我得到了这个奇怪的行为:

new File("config/").exists() 返回 false

new File("config/").getAbsolutePath() 返回/path/to/bundled/app/MyApp.app/Contents/Resources/config

new File("config/").getAbsoluteFile().exists() 返回 true

我不知道为什么会发生这种情况,我想防止在代码中的任何地方添加 getAbsoluteFile() 。

对此有什么想法吗?

(注意:我使用的是 Oracle JDK 8)

最佳答案

getAbsolutePath 根据当前 user.dir 属性解析相对路径。来自 javadocs :

On UNIX systems, a relative pathname is made absolute by resolving it against the current user directory.

虽然javadocs指出

By default the classes in the java.io package always resolve relative pathnames against the current user directory. This directory is named by the system property user.dir, and is typically the directory in which the Java virtual machine was invoked.

this bug report声明“更简单”的 setter/getter (不是“绝对”) 对于相对路径将解决 虚拟机被调用的路径。

关于java - Ant Mac OS X Appbundler 当前目录路径问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25267364/

相关文章:

java - 我们可以在网络中运行 JavaFX 桌面应用程序吗?

java - 动态测试与 JUnit 5 中的参数化测试有何不同?

Bash =~ 在 OS X cmd 提示符下丢失 BASH_REMATCH 内容

regex - Apache Ant : Can I search all files for a specific regex and then print the matches to a file?

java - 单个规则级别的异常处理 - Drools

java - 从字符串中删除指定字符

objective-c - 线程1 :EXC_BAd_InSTRUCTION(code =EXC_1386_INVOP,子代码)

linux - Mac OSx PKG 预安装脚本问题

java - 在 ant 构建期间排除依赖项目的 jar

linux - ant scp 任务在 Linux 上挂起