Running SWT based, cross-platform jar properly on a Mac

Yes, you will definitely need -XstartOnFirstThread to get this working on Mac OS X. Since it’s a VM parameter, you can only specify it when launching your application, so detecting the OS from you code and setting it if it’s Mac OS X is not possible. The solution on the Eclipse site creates a proper Mac OS X My Application.app, which is platform specific and, again, not feasible in your case.

However, I just tried running an Eclipse RCP application on Windows XP with the -XstartOnFirstThread argument specified, and it didn’t complain at all. This means that you can specify this argument in your JNLP file and presumably it will be ignored on all other platforms and picked up on Mac OS X.

UPDATE: If for some reason -XstartOnFirstThread causes trouble on any platform, or you just want to Do The Right Thing, there is another possible solution. You could detect the user’s OS in the browser — assuming that the application is launched from a web page —, and supply a different JNLP for Mac OS X and other platforms.

UPDATE 2: As pointed out in the comments, there is a tutorial on deploying SWT applications with Java Web Start. I simply launched the JNLP on Mac OS X (10.6.x), and it worked. Looking at the example JNPL I found the following:

<?xml version="1.0" encoding="utf-8"?>
<jnlp spec="1.0+"
    codebase="http://www.eclipse.org/swt/jws/"
    href="https://stackoverflow.com/questions/3976342/controlexample.jnlp">
<information>
      <title>Control Example</title>
      <vendor>eclipse.org</vendor>
      <homepage href="http://www.eclipse.org/swt/jws/" />
      <description>A demonstration of SWT Widgets</description>
      <description>Control Example</description>
</information>

<security>
    <all-permissions />
</security>

<resources>
    <extension href="https://stackoverflow.com/questions/3976342/swt.jnlp"/>
    <jar href="controlexample.jar" />
</resources>

<application-desc main-class="org.eclipse.swt.examples.controlexample.ControlExample" />
</jnlp>

Note the <extension href="https://stackoverflow.com/questions/3976342/swt.jnlp"/> line towards the end, pointing to the platform-specific SWT JNLP file (some parts omitted here):

<?xml version="1.0" encoding="utf-8"?>
<jnlp spec="1.0+"
    codebase="http://www.eclipse.org/swt/jws/"
    href="https://stackoverflow.com/questions/3976342/swt.jnlp">
<information>
      <title>SWT</title>
      <vendor>eclipse.org</vendor>
      <homepage href="http://www.eclipse.org/swt/jws/" />
      <description>SWT</description>
</information>

<security>
    <all-permissions />
</security>

<resources os="Windows" arch="x86">
    <j2se version="1.4+" />
    <jar href="swt-win32-windows-x86.jar" />
</resources>

...

<resources os="Mac\ OS\ X">
    <j2se version="1.5*" java-vm-args="-XstartOnFirstThread"/>
    <jar href="swt-carbon-osx-universal.jar" />
</resources>

<component-desc/>
</jnlp>

There it is towards the end of the file: the Mac OS X specific -XstartOnFirstThread argument.

Leave a Comment