There's actually a way to programmatically run SWT's readAndDispatch
on the main thread without needing to use a command line option. It's not often mentioned in general and mentioned even less when talking about SWT. I just found out about this and it's been a life saver.
com.apple.concurrent.Dispatch
exists on Mac only and can produce an Executor
which allows submitting tasks onto the main thread. Using this it's possible to create a cross-platform SWT application which always opens on double click.
Of course, because the class doesn't exist on Windows/Linux, you can't simply directly call the methods required. If you do your application will fail on Windows/Linux from ClassNotFoundException
. Instead, you'll need to use some reflection to run readAndDispatch
on the main thread only if the current OS is Mac.
You'll want to check whether the OS is Mac. To do that you can use the code below.
if (System.getProperty("os.name").toLowerCase().contains("mac"))
After that, you can use reflection to call the methods necessary.
Executor executor;
try {
Class<?> dispatchClass = Class.forName("com.apple.concurrent.Dispatch");
Object dispatchInstance = dispatchClass.getMethod("getInstance").invoke(null);
executor = (Executor) dispatchClass.getMethod("getNonBlockingMainQueueExecutor").invoke(dispatchInstance);
} catch (Throwable throwable) {
throw new RuntimeException("Could not reflectively access Dispatch", throwable);
}
Finally, you can execute your runnable in the executor, and cry tears of joy because your application is cross-platform again.