java - 如何在 Mac 上获取 Java 9 中的桌面路径

标签 java macos java-9

Java 9 禁止直接使用 com.apple.eio.FileManager。到现在我在Mac上都是用这个来获取用户“桌面”路径的位置,如下

public static File getDesktopFolder() throws FileNotFoundException {
    final int desktopOSType = FileManager.OSTypeToInt("desk");
    final String desktopFolderName = FileManager.findFolder(desktopOSType);
    return new File(desktopFolderName);
}

Java 9 中是否有使用 com.apple.eio.FileManager 查找桌面的替代品?我的临时解决方案是使用 the similar Windows-oriented question here 的答案:
public static File getDesktopFolder() {
    return new File(System.getProperty("user.home"), "Desktop");
}

然而,在某些语言环境或某些奇怪配置的系统上,这似乎很脆弱并且可能存在错误。

最佳答案

Platform-Specific Desktop Features已在 JDK9 中删除。

The APIs in the com.apple.eawt and com.apple.eio packages are encapsulated, so you won’t be able to compile against them in JDK 9. However, they remain accessible at runtime, so existing code that is compiled to old versions continues to run.

Eventually, libraries or applications that use the internal classes in the appleand com.apple packages and their subpackages will need to migrate to the new API.



JEP:272 中建议的用于迁移代码的新 API是 java.awt.Desktop .
虽然我可以看到 edit , open , browse etc stub 在同一个 API 中,但它们都需要一个合法的指定路径名,并且没有一个根据文档创建一个。

此外,我不太确定为什么您会发现现有方法在用户桌面上创建文件时会变得脆弱且有缺陷。
return new File(System.getProperty("user.home"), "Desktop");

在 Java 9 中使用现有代码的另一种方法/临时破解,当您最终可能会遇到类似于

Error:(3, 17) java: package com.apple.eio is not visible   (package
com.apple.eio is declared in module java.desktop, which does not
export it)


是利用 --add-exports as proposed in Module System在编译期间
--add-exports java.desktop/com.apple.eio=ALL-UNNAMED

关于java - 如何在 Mac 上获取 Java 9 中的桌面路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39611304/

相关文章:

java - boot层初始化出错FindException : Module not found

java - Intellij 在编译期间看不到一些非公共(public) JDK 9 类

python - 在 macOS 上为 MoviePy 安装 ffmpeg 失败并出现 SSL 错误

java - 没有选项在 IntelliJ IDEA 中运行 Java 9 项目

java - 当 Single.zip 中的一个来源失败时如何返回 Single.error()?

java - Android:toString() 方法无法与自定义适配器一起正常工作?

Java作业: Print Arrays with stored random numbers in it

java - 是否可以从 RunListener.testFinished 获取 JUnit 中的测试状态?

python - 无法在 Mac 上运行 'keyboard.is_pressed'

objective-c - 使用 XIB 文件在 MAC OSX 应用程序中自定义 View