Java 在Windows/Mac上支持SWT&;32位/64位

Java 在Windows/Mac上支持SWT&;32位/64位,java,cross-platform,swt,Java,Cross Platform,Swt,我目前正在使用将浏览器放入我的JavaSwing应用程序中。DJProject使用SWT运行,我对SWT的经验很少 我想同时支持32位和64位的Windows和Mac。我知道每个平台都有一个swt.jar文件。我将所有4个swt.jar库作为主应用程序的库添加到我的类路径中 我的问题是,当我尝试在Mac上运行应用程序时,例如,我遇到了以下错误: Exception in thread "main" java.lang.UnsatisfiedLinkError: Cannot load 32-bi

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

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

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

Exception in thread "main" java.lang.UnsatisfiedLinkError: Cannot load 32-bit SWT libraries on 64-bit JVM
我将如何在运行时自动告诉Java加载SWT库的适当变体

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

你没有。您创建了4个jar文件,每台机器(Windows和Mac)和操作系统(32位和64位)各一个


每个jar文件都包含适用于一台机器和一个操作系统的SWT jar库

您可以检测操作系统和Java版本,并动态加载相应的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);
    }
}

问题是我有所有的4个jar文件,但Java似乎加载了错误的一个,我有所有的4个,它在Win64上工作,但当我在Mac64上测试它不工作时,我得到了那个错误。也许我不清楚。Mac 64位jar仅适用于Mac 64位机器。其他3个JAR不接近Mac 64位机器。其他3个罐子也是一样。我明白,但我不想对我的应用程序做出4种不同的倾斜。我想发布一个可以同时在所有4个平台上运行的应用程序。所以我想问的是,在运行时,是否有可能根据应用程序运行的平台选择要使用的swt jar。不,你不能总是得到你想要的。但是如果你什么时候尝试一下,你可能会发现你得到了你所需要的