Java native methods

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dawray
    New Member
    • Oct 2008
    • 2

    #1

    Java native methods

    Hi, i'm having problems running the following program can someone please tell me where i'm going wrong?

    This is the error i got when running the program
    Exception in thread "main" java.lang.Unsat isfiedLinkError : HelloWorld.prin t()V
    at HelloWorld.prin t(Native Method)
    at HelloWorld.main (HelloWorld.jav a:7)

    The java file

    Code:
    class HelloWorld
       {
           private static native void print();
       
           public static void main(String[] args)
          { 
             print();
          }
          
          static
          {
             System.loadLibrary("HelloWorld");
          }
       }
    The c file

    Code:
    #include <jni.h> 
    #include <stdio.h> 
    #include "HelloWorld.h" 
    
        JNIEXPORT void JNICALL Java_HelloWorld_print(JNIEnv *env, jobject obj) 
       { 
          printf("Hello World!\n");
          return;
       }
    The .h file

    Code:
    /* DO NOT EDIT THIS FILE - it is machine generated */
    #include <jni.h>
    
    /* Header for class HelloWorld */
    
    #ifndef _Included_HelloWorld
    #define _Included_HelloWorld
    
    #ifdef __cplusplus
    extern "C" {
    #endif
    /*
    
     * Class:     HelloWorld
    
     * Method:    print
    
     * Signature: ()V
    
     */
       JNIEXPORT void JNICALL Java_HelloWorld_print(JNIEnv *, jclass);
    
    #ifdef __cplusplus
    }
    #endif
    #endif
  • myusernotyours
    New Member
    • Nov 2007
    • 188

    #2
    Hi,

    To use the native function, it must be located in a library either a .dll or a .lib for windows and a .so for linux e.t.c

    The Jvm tries to load this file and fails. Thats why you are getting the error.
    So where is your native function located?

    Regards,

    Alex.

    Comment

    • dawray
      New Member
      • Oct 2008
      • 2

      #3
      Originally posted by myusernotyours
      Hi,

      To use the native function, it must be located in a library either a .dll or a .lib for windows and a .so for linux e.t.c

      The Jvm tries to load this file and fails. Thats why you are getting the error.
      So where is your native function located?

      Regards,

      Alex.
      my file is located in a .dll library ,but im still getting the same error

      Comment

      • myusernotyours
        New Member
        • Nov 2007
        • 188

        #4
        Originally posted by dawray
        my file is located in a .dll library ,but im still getting the same error

        Is your file in a directory that windows searches for dlls? These are directories usually listed in the PATH environment variable. You may choose to add the path to your dll there or just put it in the bin directory in your java install path. Another way is to put it in the system32 directory in your Windows directory.
        Windows will always search for any dlls in these directories since they are already in the PATH environment variable.

        Also be sure to compile the dll properly. There may be issues with the calling convention, your compiler documentation will tell you how to do this.

        Regards,

        Alex.

        Comment

        Working...