0

I made an application with SWT and export its jar and it I could Run it. my machine is (win xp).

and my Java Version : java version "1.6.0_39" Java(TM) SE Runtime Environment (build 1.6.0_39-b04) Java HotSpot(TM) Client VM (build 20.14-b01, mixed mode, sharing)

then I try to Run my jar on a machine windows 7 : java version "1.7.0_17" Java(TM) SE Runtime Environment (build 1.7.0_17-b02) Java HotSpot(TM) 64-Bit Server VM (build 23.7-b01, mixed mode)

but This machine couldn't launch the application. then I tried to Run Jar on CMD by using: java -jar myapp.jar then I got the exception:`

Exception in thread "main" java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)Caused by: java.lang.UnsatisfiedLinkError: Cannot load 32-bit SWT libraries on 64-bit JVM
    at org.eclipse.swt.internal.Library.loadLibrary(Unknown Source)
    at org.eclipse.swt.internal.Library.loadLibrary(Unknown Source)
    at org.eclipse.swt.internal.C.<clinit>(Unknown Source)
    at org.eclipse.swt.widgets.Display.<clinit>(Unknown Source)
    at projectPackage.G.init(G.java:258)
    at projectPackage.G.main(G.java:225)
    ... 5 more`

and My Eclipse is using jre6

So How Could I generate Jar to work in both machines ?

Thanks in advance for your time

Heba Ahmed
  • 45
  • 4

2 Answers2

1

Well, the exception is quite self explanatory:

UnsatisfiedLinkError: Cannot load 32-bit SWT libraries on 64-bit JVM

To get your jar working on both 32bit and 64bit you should have a look at this answer:

Create cross platform Java SWT Application

You basically have to include the swt.jar for all the platforms you plan to deploy your app to and the given code will determine during runtime which version of the .jar to use.


A quick and dirty fix is to open your jar file and exchange the contained swt.jar (32bit) with the other version (64bit). This however, would restrict your app to 64bit.


Conclusion:

  • Either create separate jars for the different OSs and bit versions (each containing only one swt.jar)
  • Or create one jar to rule them all (containing all the swt.jars)
Community
  • 1
  • 1
Baz
  • 36,440
  • 11
  • 68
  • 94
  • Oh, yes I got it. My Question now is how to generate version to Run on 64-bit JVM only Am I will replace 32-bit JVM in my machine with 64-bit OR I have to fix Eclipse config to generate the appropriate jar ?? Sorry, but I'm totally misunderstand this issue – Heba Ahmed Sep 04 '13 at 11:49
  • 1
    @HebaAhmed You should be ok if you add the 64bit version to your build path and remove the 32bit one. – Baz Sep 04 '13 at 11:55
  • 1
    @HebaAhmed No need to download 64bit eclipse, you just need the 64bit version of SWT: [SWT Binary and Source](http://download.eclipse.org/eclipse/downloads/drops4/R-4.3-201306052000/#SWT) – Baz Sep 04 '13 at 12:10
1

The problem comes from your swt dll : they are for a 32 bit java,and you have a Win7 64 bit OS. Go here and dowload the proper jar http://download.eclipse.org/eclipse/downloads/drops4/R-4.3-201306052000/#SWT Now, to make a available on every arch, you need to download the library according the OS, and then load it at runtime like this :

URLClassLoader child = new URLClassLoader (swtJar.toURL(), this.getClass().getClassLoader());
Class classToLoad = Class.forName ("org.eclipse.swt.widgets.Display", true, child);
Method method = classToLoad.getDeclaredMethod ("getDefault");
Object instance = classToLoad.newInstance ();
Object result = method.invoke (instance);

Or you can use my jnlp file :

<resources os="Windows" arch="x86">
    <nativelib href="swt-native-win32-windows-x86.jar" />
</resources>

<resources os="Windows" arch="x86_64">
    <nativelib href="swt-native-win32-windows-x86_64.jar" />
</resources>
Vinz243
  • 9,654
  • 10
  • 42
  • 86