1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

JNI PROBELM :: Error in calling the native method

Discussion in 'Java' started by debapriya.patra, Mar 25, 2009.

  1. debapriya.patra

    debapriya.patra New Member

    Joined:
    Mar 25, 2009
    Messages:
    12
    Likes Received:
    0
    Trophy Points:
    0
    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 a moderator: Mar 31, 2009
  2. vitallis

    vitallis New Member

    Joined:
    Apr 7, 2009
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    You have defined wrong PATH. To load your library write:

    Code:
     
    System.load("/D:\\delfigoSecurity\\DLL\\delfigo.dll");
    
     
  3. debapriya.patra

    debapriya.patra New Member

    Joined:
    Mar 25, 2009
    Messages:
    12
    Likes Received:
    0
    Trophy Points:
    0
    No because i have tried with that option also its throwing same error.
     
  4. debapriya.patra

    debapriya.patra New Member

    Joined:
    Mar 25, 2009
    Messages:
    12
    Likes Received:
    0
    Trophy Points:
    0
    from your point of view what is the right way ?? can u tell the solution i mean that option.
     
  5. vitallis

    vitallis New Member

    Joined:
    Apr 7, 2009
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    In my code I use:
    Code:
     
    Runtime.getRuntime().load(...);
    
     
  6. debapriya.patra

    debapriya.patra New Member

    Joined:
    Mar 25, 2009
    Messages:
    12
    Likes Received:
    0
    Trophy Points:
    0
    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
     
  7. vitallis

    vitallis New Member

    Joined:
    Apr 7, 2009
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    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.
     
  8. vitallis

    vitallis New Member

    Joined:
    Apr 7, 2009
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    BTW,
    in this code I get the path of the class
    Code:
    CNativeLibrary.class
    You should use your class.
     

Share This Page