Discussions

EJB programming & troubleshooting: Calling a native function from java

  1. Calling a native function from java (2 messages)

    Hi

    I am trying to call a c++ function from java. I used the javah -jni option to generate the header file. Then I defined the function in a c++ file as follows.

    JNIEXPORT jstring JNICALL Java_TestNative_GetMilliSecTime
      (JNIEnv *env, jobject jobj){
    struct _timeb timebuffer;
        char *timeline;
    //CString tempTime;
    char OutputTime[200];

        _ftime( &timebuffer );
        timeline = ctime( & ( timebuffer.time ) );


        sprintf( OutputTime, "%.19s.%hu %s", timeline, timebuffer.millitm, &timeline[20] );

    return (newJavaString(env, OutputTime));
    }

    The java file is given below:
    public class TestNative
    {
        public native String GetMilliSecTime();
        static {
            System.loadLibrary("GetTime");
        }
        public static void main(String args[]){
           
            TestNative nt = new TestNative();
            System.out.println(nt.GetMilliSecTime());
        }
    }

    The code compiles, but when I run it I get the following error:

    Exception in thread "main" java.lang.UnsatisfiedLinkError: GetMilliSecTime
            at TestNative.main(TestNative.java:10)

    Any suggestions???
    Thanks in advance
    Shiva

  2. Hi,

    I have the same problem and would be interested to know if you found a solution to this.

    Thanks
    Ian
  3. This error might come because your dll file formed from the CPP file might not be in the same directory as the final JAVA file. just check that again. if not you neet to copy that file ...and also include the same exception error in your code. Hope that works out.