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
andcom.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
apple
andcom.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/