gpt4 book ai didi

java - 如何从运行在 OS X、Java 7 上的 Java .app 包中访问资源文件?

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:51:23 27 4
gpt4 key购买 nike

请注意,我说的是 Java 7,因为自 Java 6 以来,Java .app 包的 info.plist 规范似乎发生了一些变化。

目前我的代码是这样的:

  File file = new File( "documentation/index.html" );
if (file.exists()) {
// opens the URI in the browser
GUIUtils.openURI( file.toURI() );
} else {
// opens the URI in the browser
GUIUtils.openURI( getClass().getResource( "/documentation/index.html" ).toURI() );
}

在应用程序包的 Java 子文件夹中,我有一个“文档”子文件夹。我尝试了很多东西,但无济于事:

  • 在 info.plist 中,将工作目录设置为 Java 文件夹(使用 -Duser.dir JVMArgument 属性)- 文件看似具有正确的路径,但 file.exists () 返回 false。
  • 尝试将类路径设置为 Java 文件夹。 (getClass().getResource() 仍然返回 null)

最佳答案

如果您准备使用 com.apple 扩展,com.apple.eio.FileManager.getPathToApplicationBundle() 将为您提供包的基本路径,然后您可以创建一个与之相关的 File

关于java - 如何从运行在 OS X、Java 7 上的 Java .app 包中访问资源文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12276795/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com