64位兼容32位java swt,支持Windows / Mac上的SWT& 32位/ 64位

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.

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.

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

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

解决方案

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

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Java EE是一种用于开发企业级应用程序的Java平台。而SWT(Standard Widget Toolkit)是一种用于构建图形用户界面(GUI)的工具。在64位环境下,Java EE支持SWT是Eclipse SWT。 Eclipse SWT是一款由Eclipse基金会开发的GUI库。与Java的另一个GUI库Swing相比,SWT更接近于操作系统的本地GUI,因此可以提供更好的性能和外观体验。它支持在不同操作系统上开发图形界面,并且能够与操作系统的本地API进行交互。 在Java EE中,可以使用SWT来构建富有交互性和样式的用户界面。它提供了丰富的组件库,如按钮、文本框、表格等,以及各种布局管理器,使开发人员可以方便地创建复杂的界面。 对于64位环境,使用64位Java虚拟机(JVM)来运行Java程序。而Eclipse SWT也提供了64位版本的,以确保在64位环境下能够正常运行和使用SWT库。 总之,Java EE在64位环境下支持使用Eclipse SWT作为GUI工具SWT提供了丰富的组件库和布局管理器,以帮助开发人员构建高性能、具有良好外观的图形界面。 ### 回答2: Java EE 平台并没有针对 64 位系统提供特定的 SWT SWT(Standard Widget Toolkit)是 Eclipse 开发的一套窗口化工具集,用于创建跨平台的 Java 程序图形界面。它为 Java 应用程序提供了丰富的用户界面组件,可以与操作系统的本地窗口系统交互。 SWT 运行时是与操作系统密切相关的,因此在 64 位系统上运行 SWT 程序时,需要使用与操作系统对应的 SWT 。具体说,如果你是在一个 64 位的 Windows 系统上运行 Java EE 程序,并且想要使用 SWT 的话,你需要下载并使用 64 位的 SWT 。同样的,如果你是在一个 64 位的 Linux 或 macOS 系统上运行 Java EE 程序,并想要使用 SWT,也需要下载并使用相应的 64 位 SWT 。 总的来说,Java EE 平台本身不提供特定的 64 位 SWT ,而是需要根据操作系统的所在架构选择相应的 SWT ,以便程序能够在 64 位系统上正确运行。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值