JNI PROBELM :: Error in calling the native method

debapriya.patra's Avatar, Join Date: Mar 2009
Go4Expert Member
Hi All,

I am facing one problem on calling the native method from my java code.
Can anyone help me on this how to solve the problem.

My dll is created from C++ code and in my java class its loading properly.But when i am trying to call the native method its throwing exception::

Code:
java.lang.UnsatisfiedLinkError: display
    at com.delfigo.jni.JNIInvocation.display(Native Method)
    at com.delfigo.jni.JNIInvocation.main(JNIInvocation.java:26)

and my java class is given below where i am trying to call the native method::

Code:
public class JNIInvocation {
    // Native method declaration
    public native int display();

    // Load the DLL from java.path.entry
    static {
        System.load("D:\\delfigoSecurity\\DLL\\delfigo.dll");
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        // Call C++ method
        try {
            JNIInvocation lInvocation = new JNIInvocation();
            int lDisplay = lInvocation.display();
            System.out.println(lDisplay);
        } catch (Error e) {
            e.printStackTrace();
        }
    }
}
Its not able to find the native method declared in the dll .


Can anyone help me on this

Thanks in advance


Regards,
Debapriya

Last edited by shabbir; 31Mar2009 at 07:17.. Reason: Code blocks
0
vitallis's Avatar, Join Date: Apr 2009
Newbie Member
You have defined wrong PATH. To load your library write:

Code:
 
System.load("/D:\\delfigoSecurity\\DLL\\delfigo.dll");
0
debapriya.patra's Avatar, Join Date: Mar 2009
Go4Expert Member
No because i have tried with that option also its throwing same error.
0
debapriya.patra's Avatar, Join Date: Mar 2009
Go4Expert Member
from your point of view what is the right way ?? can u tell the solution i mean that option.
0
vitallis's Avatar, Join Date: Apr 2009
Newbie Member
In my code I use:
Code:
 
Runtime.getRuntime().load(...);
0
debapriya.patra's Avatar, Join Date: Mar 2009
Go4Expert Member
Hi,

I have changed the way you told to load the dll but its throwing me the same error.


Do you have any idea for this error.

Can you tell me the procedure whatever you did to call the C++ function inside java class.
So that i can follow the steps and do the same.


IS it a problem in my dll export which i declared for the native method ???


Anyway thanks for the reply once again

Regards
Debapriya
0
vitallis's Avatar, Join Date: Apr 2009
Newbie Member
Hi,
I wrote in Java code my own function to load a library module, the path should start with "/". In some class that is loaded the first I added items:

Code:
static Hashtable<String, String> charMaps = new Hashtable<String, String>();
Code:
 static{
  charMaps.put("%20", " ");
  charMaps.put("%21", "!");
  charMaps.put("%22", "\"");
  charMaps.put("%23", "#");
  charMaps.put("%24", "$");
  charMaps.put("%25", "%");
  charMaps.put("%26", "&");
  charMaps.put("%27", "'");
  charMaps.put("%28", "(");
  charMaps.put("%29", ")");
  charMaps.put("%2A", "*");
  charMaps.put("%2B", "+");
  charMaps.put("%2C", ",");
  charMaps.put("%2D", "-");
  charMaps.put("%2E", ".");
  charMaps.put("%3B", ";");
  charMaps.put("%3C", "<");
  charMaps.put("%3D", "=");
  charMaps.put("%3E", ">");
  charMaps.put("%40", "?");
  charMaps.put("%41", "@");
  charMaps.put("%5E", "^");
  charMaps.put("%60", "_");
  charMaps.put("%7B", "{");
  charMaps.put("%7C", "|");
  charMaps.put("%7D", "}");
  charMaps.put("%7E", "~");
 }
Code:
 public static void loadLibrary(String libName) {
  String separator = java.io.File.separator;
  String osName = System.getProperty("os.name");
  String location = null;
  String hardLib = System.getProperty("my.natives.libpath");
  libName = libName == null ? System
    .getProperty("com.compose.natives.lib") : libName;
  String libExt = osName.indexOf("Windows") == -1 ? "" : ".dll";
  if(hardLib != null && hardLib.length() > 0){
   location = hardLib;
   if(location.lastIndexOf(libName + libExt) == -1){
    if(location.lastIndexOf(separator) != location.length() - 1
      && location.lastIndexOf(separator) != location.length() - 1)
     location += separator;
    if(location.length() < libName.length() + libExt.length()
      || location.substring(
        location.length() - libName.length()
          - libExt.length()).compareTo(
        libName + libExt) != 0)
     location += libName + libExt;
   }
  } else{
   java.net.URL url = CNativeLibrary.class.getProtectionDomain()
     .getCodeSource().getLocation();
   location = new java.io.File(url.getFile()).getPath();
   Enumeration<String> enumer = charMaps.keys();
   while (enumer.hasMoreElements()){
    String s = enumer.nextElement();
    location = location.replaceAll(s, charMaps.get(s));
   }
   int extInd = location.lastIndexOf(".");
   if(extInd != -1
     && location.substring(extInd).equalsIgnoreCase(".jar")){
    int ind = location.lastIndexOf(separator);
    if(ind > 0 && ind + 1 < location.length())
     location = location.substring(0, ind + 1);
    else
     location = location.substring(0);
   }
   if(location.length() > 0
     && !location.substring(location.length() - 1).equals(
       separator))
    location += separator;
   libName = location + libName;
   location = libName + libExt;
  }
  if(location.charAt(0) != '/')
   location = '/' + location;
  if(location != null){
   try{
    Runtime.getRuntime().load(location);
   } catch(RuntimeException e){
    System.err.println("Cannot load \"" + libName + libExt
      + "\" from path " + location);
   }
  }
 }
This code tries to find the native library by
- path of the class where these module added;
- my property "my.natives.libpath" initialized on start.

If your problem persists then check the library module function signatures exprorted, in Windows they should start with underscore.
0
vitallis's Avatar, Join Date: Apr 2009
Newbie Member
BTW,
in this code I get the path of the class
Code:
CNativeLibrary.class
You should use your class.