简体   繁体   English

如何使用SWT / Eclipse在Mac OS X中执行jar?

[英]How to execute jar in Mac OS X using SWT/Eclipse?

I changed the Java to 32 bit mode, to allow the SWT run, when I debug, it works well. 我将Java更改为32位模式,以允许SWT运行,当我调试时,它运行良好。 But when I use Eclipse to export .jar, it shows this error. 但是当我使用Eclipse导出.jar时,它会显示此错误。

错误对话框

I already added chmod 777. What did I do wrong? 我已经添加了chmod 777.我做错了什么?

Here is the console result: 这是控制台结果:

20/11/2011 12:10:36 AM  [0x0-0x3d89d86].com.apple.JarLauncher[73111]    Exception in thread "main" 
20/11/2011 12:10:36 AM  [0x0-0x3d89d86].com.apple.JarLauncher[73111]    java.lang.UnsatisfiedLinkError: Cannot load 32-bit SWT libraries on 64-bit JVM
20/11/2011 12:10:36 AM  [0x0-0x3d89d86].com.apple.JarLauncher[73111]        at org.eclipse.swt.internal.Library.loadLibrary(Unknown Source)
20/11/2011 12:10:36 AM  [0x0-0x3d89d86].com.apple.JarLauncher[73111]        at org.eclipse.swt.internal.Library.loadLibrary(Unknown Source)
20/11/2011 12:10:36 AM  [0x0-0x3d89d86].com.apple.JarLauncher[73111]        at org.eclipse.swt.internal.C.<clinit>(Unknown Source)
20/11/2011 12:10:36 AM  [0x0-0x3d89d86].com.apple.JarLauncher[73111]        at org.eclipse.swt.widgets.Display.<clinit>(Unknown Source)
20/11/2011 12:10:36 AM  [0x0-0x3d89d86].com.apple.JarLauncher[73111]        at com.testing.SWTApp.main(SWTApp.java:54)

The MANIFEST is like follow: MANIFEST如下:

Manifest-Version: 1.0
Class-Path: .
Main-Class: com.testing.SWTApp 

Try this: 尝试这个:

java -d32 -XstartOnFirstThread -jar myapp.jar

The -d32 option will cause the VM to run in 32-bit mode. -d32选项将使VM以32位模式运行。 The -XstartOnFirstThread option is necessary for SWT applications. -XstartOnFirstThread选项对于SWT应用程序是必需的。

the swt jar executes native librairies which depend on your OS. swt jar执行依赖于你的操作系统的本机库。
You have one version of swt for: macosx 32 & 64 bits 你有一个版本的swt:macosx 32和64位
winsows 32 & 64 bits 胜利32和64位
linux 32 & 64 bits linux 32和64位

you can specify the -d32 jvm option. 您可以指定-d32 jvm选项。 It will force the JVM to run in the 32 bits mode. 它将强制JVM以32位模式运行。 However, you need to have a 32 bits implementation of the JVM available in your OS. 但是,您需要在操作系统中使用32位JVM实现。

You could also load dynamically at runtime the right SWT Jar: you include in your app both jar (swt-macosx-32bits & swt-macosx-64bits) and load programatically the required jar: 您还可以在运行时动态加载正确的SWT Jar:您在应用程序中包含jar(swt-macosx-32bits和swt-macosx-64bits)并以编程方式加载所需的jar:

String osArch = System.getProperty ("sun.arch.data.model");
if (osArch == null) {
    osArch = System.getProperty ("com.ibm.vm.bitmode");
}
URLClassLoader classLoader = (URLClassLoader) JarLoaderUtils.class.getClassLoader();
URL.setURLStreamHandlerFactory(new RsrcURLStreamHandlerFactory(classLoader));
Method addUrlMethod = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
addUrlMethod.setAccessible(true);
swtFileName = osArch.contains("64") ? SWT_MAC_x86_64 : SWT_MAC_x86;
URL swtFileUrl = new URL("rsrc:" + swtFileName);
addUrlMethod.invoke(classLoader, swtFileUrl);

Note that you seem to run your app in macosx, you should also add the -XstartOnFirstThread option. 请注意,您似乎在macosx中运行应用程序,还应添加-XstartOnFirstThread选项。

The problem is it's not Eclipse that is executing your built jar, it's Finder / the java launcher. 问题是Eclipse不是正在执行你构建的jar,它是Finder / java启动器。 What you need to do is goto /Applications/Utilities/Java Preferences.app and reorder (click drag) so that your 32 bit version is chosen before the 64 bit version. 您需要做的是转到/Applications/Utilities/Java Preferences.app并重新排序(单击拖动),以便在64位版本之前选择32位版本。 For reference mine looks like this: 为了参考我的看起来像这样:

我的Java首选项

then when you execute java -version (no -d parameter) you'll get the 32 bit JRE chosen. 然后当你执行java -version (no -d参数)时,你将选择32位JRE。 and when you double click the jar in finder or use open app.jar it should work. 当你在finder中双击jar或使用open app.jar它应该工作。

20/11/2011 12:10:36 AM  [0x0-0x3d89d86].com.apple.JarLauncher[73111]    java.lang.UnsatisfiedLinkError: Cannot load 32-bit SWT libraries on 64-bit JVM

Please focus on this error. 请关注此错误。 You need 64-bit dependency libraries for launching it. 您需要64位依赖库才能启动它。 It depends on default java loaded. 这取决于默认的java加载。 If its 32-bit package, you need your default jvm classpath to be 32-bit java. 如果是32位软件包,则需要将默认的jvm类路径设置为32位java。 You have option for changing it in eclipse. 您可以选择在eclipse中更改它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM