Search code examples
javamacosjava-7resource-filesapp-bundle

How can I access resource files from within a Java .app bundle running on OS X, Java 7?


Note that I am talking about Java 7, since the info.plist specification for a Java .app bundle seems to have changed a bit since Java 6.

Currently my code looks like this:

  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() );
  }

In the Java subfolder in the app bundle, I have a "documentation" subfolder. I have tried multiple things, to no avail:

  • In the info.plist, setting the working directory to the Java folder (with a -Duser.dir JVMArgument property) - the file seemingly has the right path, but file.exists() returns false.
  • Trying to set the ClassPath to the Java folder. (getClass().getResource() still returns null)

Solution

  • If you're prepared to use the com.apple extensions, com.apple.eio.FileManager.getPathToApplicationBundle() will give you the base path to your bundle, and you can then create a File relative to that.