支持Windows / Mac上的SWT& 32位/ 64位 [英] Supporting SWT on Windows/Mac & 32bit/64bit

查看:673
本文介绍了支持Windows / Mac上的SWT& 32位/ 64位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用 DJProject 将浏览器放入我的Java Swing中应用。 DJProject使用SWT运行,我对SWT的经验很少。

I'm currently working with the DJProject to put a browser into my Java Swing application. DJProject uses SWT to run and I have very little experience with SWT.

我想支持32位和64位的Windows和Mac。我知道每个平台都有一个swt.jar文件。我将所有4个swt.jar库添加到我的类路径中作为主应用程序的库。

I want to support Windows and Mac both 32bit and 64bit. I understand there is a swt.jar file for each of these platforms. I have all 4 swt.jar libraries added to my classpath as a library to the main application.

我的问题是当我尝试在Mac上运行应用程序时,例如我得到错误:

My problem is when I try running the application on a Mac for example I get the error:

Exception in thread "main" java.lang.UnsatisfiedLinkError: Cannot load 32-bit SWT libraries on 64-bit JVM

我如何在运行时自动告诉Java加载适当的变体SWT库。

how would I go about to automatically tell Java at run-time to load the proper variation of the SWT library.

推荐答案

您可以检测操作系统和Java版本并动态加载相应的jar:

You can detect the OS and Java version and dynamically load the appropriate jar:

private void loadSwtJar() {
    try {
        Class.forName (ORG_ECLIPSE_SWT_WIDGETS_SHELL);
        return;
    } catch (ClassNotFoundException e) {
        System.out.println (" ! Need to add the proper swt jar: "+e.getMessage());
    }

    String osName = System.getProperty("os.name").toLowerCase();
    String osArch = System.getProperty("os.arch").toLowerCase();

    //NOTE - I have not added the mac and *nix swt jars.
    String osPart = 
        osName.contains("win") ? "win" :
        osName.contains("mac") ? "cocoa" :
        osName.contains("linux") || osName.contains("nix") ? "gtk" :
        null;

    if (null == osPart)
        throw new RuntimeException ("Cannot determine correct swt jar from os.name [" + osName + "] and os.arch [" + osArch + "]");

    String archPart = osArch.contains ("64") ? "64" : "32";

    System.out.println ("Architecture and OS == "+archPart+"bit "+osPart);

    String swtFileName = "swt_" +osPart + archPart +".jar";
    String workingDir = System.getProperty("user.dir");
    String libDir = "\\lib\\";
    File file = new File(workingDir.concat(libDir), swtFileName);
    if (!file.exists ())
        System.out.println("Can't locate SWT Jar " + file.getAbsolutePath());

    try {
        URLClassLoader classLoader = (URLClassLoader) getClass().getClassLoader ();
        Method addUrlMethod = URLClassLoader.class.getDeclaredMethod ("addURL", URL.class);
        addUrlMethod.setAccessible (true);

        URL swtFileUrl = file.toURI().toURL();
        //System.out.println("Adding to classpath: " + swtFileUrl);
        addUrlMethod.invoke (classLoader, swtFileUrl);
    }
    catch (Exception e) {
        throw new RuntimeException ("Unable to add the swt jar to the class path: " + file.getAbsoluteFile (), e);
    }
}

这篇关于支持Windows / Mac上的SWT& 32位/ 64位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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