0

I'm trying to compile a Java application into a Mac OS X app bundle. I add the following setting to set the current working directory:

<bundleapp...>
    ...
    <option value="-Duser.dir=$APP_ROOT/Contents/Resources"/>
</bundleapp>

In Contents/Resources/ there is a config directory.

When executing it, I get this strange behaviour :

new File("config/").exists() returns false

new File("config/").getAbsolutePath() returns /path/to/bundled/app/MyApp.app/Contents/Resources/config

new File("config/").getAbsoluteFile().exists() returns true

I don't know why this happens and I would like to prevent adding getAbsoluteFile() everywhere in my code.

Any thoughts on this?

(Note: I'm using Oracle JDK 8)

Béatrice Cassistat
  • 1,048
  • 12
  • 37

1 Answers1

1

getAbsolutePath resolves relative paths against current user.dir property. From javadocs:

On UNIX systems, a relative pathname is made absolute by resolving it against the current user directory.

Although javadocs states that

By default the classes in the java.io package always resolve relative pathnames against the current user directory. This directory is named by the system property user.dir, and is typically the directory in which the Java virtual machine was invoked.

this bug report states that "simpler" getters (not "absolute") for relative paths will resolve against the path the virtual machine was invoked.

Jean Waghetti
  • 4,711
  • 1
  • 18
  • 28
  • Would you know if there is another way to do this? If I add: WorkingDirectory$APP_ROOT/Contents/Resources, it does not seems to help. I would like to prevent adding getAbsoluteFile() everywhere since the problem only occurs with the Mac application bundle... – Béatrice Cassistat Aug 12 '14 at 17:15
  • Maybe the way to go is to set a final static field with the absolute path to the "Resources" folder. Then create `File` objects with the absolute path `File config = new File(ResourceFolder+"config")`. – Jean Waghetti Aug 12 '14 at 17:35
  • You may be right, but it would have been simpler to change the current working directory... But thank you for your suggestion! – Béatrice Cassistat Aug 12 '14 at 18:08