3

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)
Epaga
  • 38,231
  • 58
  • 157
  • 245

2 Answers2

5

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.

Ian Roberts
  • 120,891
  • 16
  • 170
  • 183
  • Though we're cross-platform, we can call that method via reflection, so it works. Thanks! – Epaga Sep 05 '12 at 11:52
  • In my case, I needed to get the path to a file within the Resources folder of the application bundle. `com.apple.eio.FileManager.getResource("filename.txt")` is what I was looking for, but your answer led me to that. Thanks! – aapierce Jul 07 '15 at 14:55
1

As of Java 9, com.apple is not accessible.

Java 9 uses modules to export (and expose) packages so com.apple is now unusable and deprecated. According to this, java.awt should provide similar API functionalities.

Just wanted to provide this update as I ran into this issue following the accepted answer.

System.getenv("LOCALAPPDATA") can be used as an option to get application environment.

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
saqe hi
  • 181
  • 2
  • 8