I have a compiled executable JAR file that fails on Windows platforms.
The reason for this is because I want to properly integrate certain OS X-specific properties — such as the About window.
Even though I specifically cordoned off the code using a conditional, the JAR is still crashing with a NoClassDefFoundError on the very first line of execution.
if (isOSX()) {
com.apple.eawt.Application application = com.apple.eawt.Application.getApplication();
application.setAboutHandler(new com.apple.eawt.AboutHandler() {
@Override
public void handleAbout(com.apple.eawt.AppEvent.AboutEvent ae) {
new AboutWindow();
}
});
application.setDefaultMenuBar(MenuSystem.createMenu());
}
Is it possible to include this code in my JAR file so I can have one consistent codebase?
Have you tried loading the surrounding class dynamically using Class.forName ?
This way you can refer to all your Apple-specific classes inside one class, and dynamically load it in your
isOSX()branch. You have to do it this way since you’re not able to load a class that refers to other unavailable classes – you’ll have to determine if you’re in OSX, and only then load up anything referring to OSX-only classes.An extensible way to do this if you have more OS-specific requirements is to name your class after the OS and then load a classname based upon the detected OS. e.g. call your classes
WindowsExtensions,OSXExtensions,LinuxExtensionsetc. (you will have to look up the appropriate names – I’m just providing examples)e.g. here’s an example of usage: