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:


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.