title: How to bundle a Java application to a Mac OS X *.app bundle?
body: <p>I am trying to bundle my Java application to an Mac OS X .app bundle. I am currently using <a href="https://java.net/downloads/appbundler/appbundler.html" rel="nofollow"><strong>appbundler</strong></a> and I followed <a href="http://docs.oracle.com/javase/7/docs/technotes/guides/jweb/packagingAppsForMac.html" rel="nofollow">Oracle's guide</a>. The Ant task works fine in Netbeans and the corresponding .app package is built correctly (on Windows). But when trying to execute the .app package to my MacBookPro (OS X 10.7.5), I get the following error in the log:</p>
  
  <pre><code>[0x0-0x72072].ch.lawsuite.core.Servitus: Error: Unable to access jarfile LawSuiteSE.jar
  </code></pre>
  
  <p>I already checked the permissions for the .jar file and the execution bits are set for the main-jar and all the jars in the lib-folder.</p>
  
  <p>The application just runs fine when typing the following to the terminal:</p>
  
  <pre><code>java -jar /Applications/Servitus.app/Contents/Java/LawSuiteSE.jar --installer
  </code></pre>
  
  <p>For clarification, here's the final folder structure:</p>
  
  <pre><code>Servitus.app
      |_Contents
          |_Java
              |_LawSuiteSE.jar
              |_lib
                  |_lib1.jar
                  |_lib2.jar
                      |_subfolder1
                          |_lib3.jar
                      |_subfolder2
                          |_lib4.jar
                  |_etc...
              |_MacOS
                  |_JavaAppLauncher
              |_PlugIns
              |_Resources
                  |_en.lproj
                      |_Localizable.strings
                  |_kplus.icns
          |_Info.plist
          |_PkgInfo
  </code></pre>
  
  <p>By the way, my application has lots of external dependencies, all located in the "lib" folder which are properly linked by the class-path property within the MANIFEST.MF file of the executable main-jar file "LawSuiteSE.jar". Do I need to copy these libs within the appbundle-ant-task or is it okay to copy them subsequently by hand?</p>
  
  <p>I also tried to build the application and the corresponding .app package on the MacBook Pro, but the same error message.</p>
  
  <p>Here's my Ant-task:</p>
  
  <pre><code>&lt;!-- Define the appbundler task --&gt;
  &lt;taskdef name="bundleapp" classname="com.oracle.appbundler.AppBundlerTask"/&gt;
  &lt;!-- Create the app bundle --&gt;
  &lt;target name="bundle-macosx"&gt;
      &lt;bundleapp outputdirectory="macosx"
          name="Servitus"
          displayname="Servitus"
          identifier="ch.lawsuite.core.Servitus"
          shortversion="1.0"
          applicationCategory="public.app-category.business"
          icon="src/ch/lawsuite/data/icons/gmbh/osx/kplus.icns"
          mainclassname="ch.lawsuite.core.Servitus"&gt;
          &lt;classpath file="dist/LawSuiteSE.jar"/&gt;
          &lt;argument value="--installer"/&gt;
      &lt;/bundleapp&gt;
  &lt;/target&gt;
  </code></pre>
  
  <p>EDIT: I meanwhile replaced the JavaAppLauncher in Contents/MacOS with a small run script (start.sh):</p>
  
  <pre><code>#!/bin/bash
  java -Xms256m -XX:PermSize=128m -jar /Users/salocinx/Workspace/LawSuiteSE.app/Contents/Java/LawSuiteSE.jar --installer
  </code></pre>
  
  <p>Then I also replaced the value for the key <em>CFBundleExecutable</em> in <em>Info.plist</em> from <em>JavaAppLauncher</em> to <em>start.sh</em>.</p>
  
  <p>Now it works fine, but how can I use a relative path to the jar-executable? Something like "../Java/LawSuiteSE.jar" doesn't work and gives me the same error as before. Is there any constant pointing to the current app-folder, something like "$APP_ROOT" so that the script could look like:</p>
  
  <pre><code>#!/bin/bash
  java -Xms256m -XX:PermSize=128m -jar $APP_ROOT/Contents/Java/LawSuiteSE.jar --installer
  </code></pre>
  
  <p>Any ideas?</p>