macosunixexecutable-jar.app

Double clicking Unix Executable file is different from running it from terminal


I'm trying to create .app package that contains jdk, jar and shell script, basically I'm trying to run jar inside .app.

My contents inside .app looks like this (app has same name as unix executable):

.
├── MacOS
│   └── AppName
├── app
│   └── myapp.jar
└── jdk

AppName, if we give it .sh extension and look into it, looks like this

#!/bin/zsh

../jdk/bin/java -jar ../app/myapp.jar

If I run this file executable file from terminal with ./AppName it works without any problems. But if I just double click it, it doesn't work saying no such file or directory: ../jdk/bin/java.

I believe it should work with double clicking in order to work with .app, because other .app-s work like this.


Solution

  • Relative path names like ../jdk/bin/java are relative to the current working directory, not to the location of the script.

    You can reproduce the problem in the terminal when your working directory is not MacOS but, e.g., the parent directory, and you start the application as MacOS/AppName.

    A possible solution might be

    #!/bin/zsh
    
    SCRIPTDIR=$(dirname $0)
    
    "$SCRIPTDIR"/../jdk/bin/java -jar "$SCRIPTDIR"/../app/myapp.jar