如何为跨平台的Jar调用OS X特定的方法? [英] How can I call an OS X-specific method for my cross-platform Jar?

查看:79
本文介绍了如何为跨平台的Jar调用OS X特定的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个已编译的可执行JAR文件,该文件在Windows平台上失败.

I have a compiled executable JAR file that fails on Windows platforms.

之所以这样做,是因为我想正确地集成某些特定于OS X的属性,例如关于"窗口.

The reason for this is because I want to properly integrate certain OS X-specific properties — such as the About window.

即使我使用条件条件专门关闭了代码,但JAR在执行的第一行仍然崩溃并显示NoClassDefFoundError.

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());
}

是否可以在我的JAR文件中包含此代码,以便我可以拥有一个一致的代码库?

Is it possible to include this code in my JAR file so I can have one consistent codebase?

推荐答案

您是否尝试过使用

这样,您可以在一个类中引用所有特定于Apple的类,然后将其动态加载到您的isOSX()分支中.您必须这样做,因为您无法加载引用其他不可用类的类-您必须确定您是否在OSX中,然后 加载所有内容指仅OSX的类.

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.

如果您有更多特定于操作系统的要求,则可扩展的方法是在操作系统之后命名您的类,然后根据检测到的操作系统加载一个类名.例如调用您的类WindowsExtensionsOSXExtensionsLinuxExtensions等(您必须查找适当的名称-我只是提供示例)

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, LinuxExtensions etc. (you will have to look up the appropriate names - I'm just providing examples)

例如这是用法示例:

String className = ""java.util.ArrayList";
Class cls = Class.forName(className);
List list = (List)cls.newInstance();

这篇关于如何为跨平台的Jar调用OS X特定的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆