Jun
9
2005

Bundling SWT Apps for MacOSX

So you’ve written your killer SWT app and you’re keen to bundle it in a nice clickable Mac application bundle for distribution on OSX? Such has been my journey for last few days. I reckon I’ve sunk around 10 hours into just this process over the last week, but I now have it working. First, let me show show off: (it’s good therapy)

PasswordSafeSWT Icon in the Finder

Ok, the resources that you need to read are:

  • First, you need to read an awesome background article at Borderland Consulting
  • Then, you’ll want the very cool Jar Bundler Ant Task which will put together an app bundle for you and copy all your jars into the right directory structure and create an Info.plist file for you
  • Then (and this bit just killed me), you must make sure that you chmod your .jnilib files executable! If you don’t, you’ll end up getting a “Could not load library” error even though the swt library is clearly in your java.library.path!
  • Finally, you must add an entry StartOnMainThread

    to your Info.plist file which resolved some Threading issues with the launcher. If you’re getting weird GUI behaviour with disappearing buttons and bizarre crashes, it will probably be cause you’ve missed this setting.

I’m a Mac newbie, so it took me a while to work out why my app would launch, bounce a few times in the dock, then die. You can find out why via the Console application (Applications/Utilities/Console) – which is where the errors get dumped from the lauching process. That’s when I worked out it couldn’t load the .jnilib files even though they were in java.library.path (since they weren’t chmoded executable).

The other thing that I couldn’t get working was using $JAVAROOT in an Info.plist setting for java.library.path. It would just never get expanded by the launcher. I ended up cutting my losses and just copying the my .jnilib files to the same directory as the rest of my jars.

BTW, I’ve been using the jars from Eclipse 3.1RC1 to get all this happening. Earlier jars required you to use a special java_swt launcher to fix those threading issues I was talking about, but the 3.1 stream just uses the standard JavaApplicationStub launcher which is just easier all round.

Eclipse 3.1 also has this “Export To… Mac App Bundle” option but it just tanked every time I tried it, so that might be one to sleep till the final release.

Have fun! Hopefully all this will save you some pain!

About the Author: Glen Smith

5 Comments + Add Comment

  • Does this somehow help the double dock icon? If the eclipse icon is already in the dock, clicking on it will create another icon.

    AFAIK, this is just an SWT-ism.

  • I think the reason Eclipse gets two icons in the dock is something to do with an extra laucher thread is spawns to being able to reload itself in some circumstances (when you switch workspaces?). Anyways, that behaviour is an eclipse thing, not an SWT thing.

    This certainly doesn’t affect your own SWT apps. When I launch PasswordSafeSWT from the Dock there is no “extra” icon. The select icon bounces, then runs.

  • Thanks so much for posting this info. :-) I’ve been curious about how to do exactly this.

  • Hi!
    First of all, this post is very interesting! I am writing and Ans script do bundle the application and decided to also use the XMLTask (http://www.oopsconsultancy.com/software/xmltask) to add the key “StartOnMainThread” to the Info.plist file. After declaring the task definition, all I need is this:




    < ![CDATA[StartOnMainThread
    ]]>


    You don’t need the “entity” tag, I just used in order to change it from Windows.

    Hope it is useful.
    Once again, congratulations for such a great article!

    Nuno Fernandes.

  • Thanks for writing up all your efforts – it’s helped me a lot in creating my first ever Java application bundle!

    I found that I could simplify things by putting the jnilib files into the MacOS folder using the nested execfileset element. This automatically sets its files to be executable and I think that the MacOS folder is a more appropriate place to put native dlls anyway. To get them added into the library path, you have to specify a javaproperty – you can’t use -Djava.library.path in the vmoptions!

    To summarize, here’s my jarbundler task call:

    <jarbundler
    	dir="${dist.mac.dir}"
    	name="${app.name}"
    	mainclass="${main.class}"
    	stubfile="${java.application.stub}"
    	workingdirectory="$APP_PACKAGE/Contents/Resources"
    >
    	
    	<!-- Include the SWT native jnilibs -->
    	<javaproperty name="java.library.path" value="$APP_PACKAGE/Contents/MacOS"/>
    	<execfileset dir="${mac.dir}/dll">
    		<include name="*.*"/>
    	</execfileset>
    	<jarfileset dir="${mac.dir}">
    		<include name="*.jar"/>
    	</jarfileset>
    	<jarfileset file="${main.jar}"/>
    </jarbundler>
    
    <!-- Hack!  Write the StartOnMainThread key into Info.plist in order to get SWT working on OS X.
    	Info.plist will always contain a ClassPath key in the right place. -->
    <replace file="${dist.mac.dir}/${app.name}.app/Contents/Info.plist" token="<key>ClassPath" value="<key>StartOnMainThread</key><true/> <key>ClassPath" />
    

    I’ve also included your method for adding the StartOnMainThread key (taken from SWTSafe’s build.xml – thanks for that tip!

Leave a comment

Glen Smith

About Glen

Co-author Grails in Action