0

so I have this project in java that uses a jni .dll i wrote (which i've aptly named jniusb) that gives access to usb-hid devices. i've been using it for several months now without any notable issues until yesterday when i tried to launch my program from the jar instead of directly from netbeans. after a little debugging i characterized my issue as follows:

  1. clicking on the jar icon starts my program without loading the dll. displaying the error messages in my gui revealed "no jniusb in java.library.path" even though i copied my dll to the java binaries folder (which always seemed to work in the past).

  2. i tweaked the code to find the current directory and use "System.load" (with the dll copied to the same folder as my jar) instead of "System.loadLibrary". this approach threw the error "C:\Users\bpaik\Documents\NetBeansProjects\JniUsb\dist\jniusb.dll: Can't load AMD 64-bit .dll on a IA 32-bit platform". this made me scratch my head since i am most definitely working on an AMD 64-bit platform and running out of netbeans works with the same dll...

  3. running the jar from the command prompt (with either java.exe or javaw.exe) loads the dll just fine. i thought that maybe this meant that i was having admin privileges issues so i tested the theory by going to the java executable and granting admin privileges, but this did not change anything. (and i'm also pretty sure i've used load/loadlibrary before without admin...)

i am no java expert so i've pretty much exhausted my debugging abilities and am now hoping that someone with a little more experience than i will recognize the issue i've described, thanks.

UPDATE: so i've fixed the problem but I still have no idea what is going on. i did a x86 build of my .dll and threw it into the folder with my jar and everything loaded/worked just fine. so i guess somehow (even though i set the default program for my jar to be the x64 JVM) the jar i built with the x64 library in Netbeans is running in a 32-bit JVM. to sum things up:

  1. my jar is built with the x64 library in netbeans and runs fine with the x64 dll when launched from the command prompt.
  2. when launching the jar from the icon (with default program set to x64 java) my program and JVM somehow switch to a 32-bit JVM and work fine when i use the 32-bit version of my dll.
  3. trying to explicitly run my jar with the 32-bit java doesn't work at all. my gui doesn't display and the command prompt briefly opens/closes...
Ben
  • 1,132
  • 1
  • 15
  • 35
  • You have several JRE/JDK's installed. At least one 32-bit and one 64-bit? If so, check if one is used by default from commandline and another when you "double-click-on-a-jar" – esej May 25 '12 at 17:58
  • yeah i have one x86 and one x64. when i run from command i definitely use the x64 version. i right-clicked my jar icon and set it to open with the x64 java but i still have the dll issue. just for kicks i set it to open with the 32-bit and the cmd prompt briefly opens and closes - my gui never appears. – Ben May 25 '12 at 18:16
  • Well seems that part of the problem is figured out, what's left of it is windows-things. But it seems you want to target both 32-bit and 64-bit systems and your dll is 64-bit - is that plan ever gonna work? – esej May 25 '12 at 18:19
  • i compiled a x86 version of the .dll which works (i tested it with a 32-bit version of the jar) – Ben May 25 '12 at 18:22

2 Answers2

0

Do you have multiple versions of Java installed on your Machine? It appears that when you run the jar by itself, your machine is creating a 32-bit VM for it. Open an command prompt and type java -version to check which version of Java is being used.

Jeshurun
  • 22,940
  • 6
  • 79
  • 92
  • yes, i intend to release both 32 and 64 bit versions of the program (but the 32-bit should be x86 not IA 32). the java -version said "java version 1.7.0, Java(TM) SE Runtime Environment (build 1.7.0-b147), Java HotSpot(TM) 64-bit Server VM (build 21.0-b17, mixed mode)" - and i have no idea what that means... – Ben May 25 '12 at 18:02
0

Problem 2 is certainly caused by you running a 32-bit JVM. Netbeans presumably runs a 64-bit JVM. You'll need to use something like Launch4j to get the right JVM loaded (or include both a 32-bit and 64-bit version of your DLL).

From the command line, try -d32 and -d64 to test that theory out.

Keith Randall
  • 22,985
  • 2
  • 35
  • 54
  • -d64 ran the program and loaded my dll just fine but -d32 says "Error: This Java instance does not support a 32-bit JVM". also, i checked early on that i was using the right JVM by right clicking the jar and specifying which java i wanted to use. in netbeans i set the properties to do a 64-bit build so i'm confused how this would even open on a 32-bit JVM. – Ben May 25 '12 at 18:11
  • i did some more tests mentioned in the UPDATE section above. you seemed to be right but i'm still confused about what's going on... – Ben May 25 '12 at 19:02
  • There may be 3 totally separate java installations on your machine. One used by Netbeans, one used by the command line, and one used by the double-click machinery. Each can be 32, 64, or both, and with a different default if it has both. – Keith Randall May 25 '12 at 19:31
  • You should always be safe if you provide 32 & 64 bit versions of your DLL. – Keith Randall May 25 '12 at 19:32