3

I'm trying to use my C++ in a Java application but fail at the simple JNA stuff and i dont know why. For testing purposes I use the DLL from the following tutorial: http://msdn.microsoft.com/en-us/library/vstudio/ms235636.aspx

Now i try to load the Add method with JNA

   import com.sun.jna.Native;
import com.sun.jna.Library;

public class Jnatest {

    public interface  CppLib  extends Library  {


        CppLib INSTANCE = (CppLib)Native.loadLibrary("MathFuncsDll", CppLib.class);


        double Add(double a, double b);

        double Subtract(double a ,double b);

        double Multiply(double a, double b);

        double Divide(double a, double b);
        } 

  public static void main(String[] args) {
      System.setProperty("jna.library.path", "C:\\JNA\\");
    CppLib lib = CppLib.INSTANCE;
    double res;
    res=lib.Add(1.0, 1.25);
    System.out.println("add:\t"+res); 
    }
}

When Compiling i get the following Loading Exception:

Exception in thread "main" java.lang.UnsatisfiedLinkError: %1 ist keine zulässige Win32-Anwendung.

    at com.sun.jna.Native.open(Native Method)
    at com.sun.jna.Native.open(Native.java:1759)
    at com.sun.jna.NativeLibrary.loadLibrary(NativeLibrary.java:260)
    at com.sun.jna.NativeLibrary.getInstance(NativeLibrary.java:398)
    at com.sun.jna.Library$Handler.<init>(Library.java:147)
    at com.sun.jna.Native.loadLibrary(Native.java:412)
    at com.sun.jna.Native.loadLibrary(Native.java:391)
    at Jnatest$CppLib.<clinit>(Jnatest.java:9)
    at Jnatest.main(Jnatest.java:23)

First i thougt there is something wrong with the Library Path where the Files are located. But System.setProperty("jna.library.path", "C:\JNA\"); should set the path imo. What else could be wrong?

Thank You

David

David_D
  • 173
  • 1
  • 2
  • 12
  • 2
    Sounds like you might be trying to load a x64 DLL on 32-bit or vice versa. – kichik Sep 30 '13 at 09:18
  • Checked it with PE Deconstructor and everything is 32Bit. Installed a never Eclipse version but nothing changed. I'm getting really desperate because i don't know what else could cause the problem. – David_D Sep 30 '13 at 09:41
  • 2
    Use `-Djna.library.path=xxx` on JVM launch. Depending on your VM's method of loading classes, `Native.loadLibrary()` may execute before the `System.setProperty()` call. Alternatively, skip the `INSTANCE` variable and call `Native.loadLibrary()` directly. – technomage Sep 30 '13 at 11:40
  • The most likely issue is that the DLL you are attempting to load is not correct for your system (e.g. 32-bit on 64-bit VM or vice versa). – technomage Sep 30 '13 at 11:43
  • would you kindly tell me more about -Djna.library.path=xxx on JVM launch. I tried to type java `-Djna.library.path="C://JNA//"` on cmd as administrator. C:/JNA is where i keep the DLL for this Project but i dont think its the right way. Can I set this Path somewhere in Eclipse? I really appreciate your time and patience. – David_D Sep 30 '13 at 12:38
  • See [setting command-line arguments in eclipse](http://stackoverflow.com/questions/862391/how-to-pass-the-d-sytem-properties-while-testing-on-eclipse) – technomage Sep 30 '13 at 19:50
  • BTW, windows accepts both forward and backslashes as filename path element separators, i.e. `C:/JNA/` and `C:\\JNA\\` are equivalent. Double forward slashes are *usually* ignored in file paths, but I wouldn't guarantee it in all situations. – technomage Sep 30 '13 at 19:52

0 Answers0