请注意,我说的是 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/