Calling a native function from java


EJB programming & troubleshooting: Calling a native function from java

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


    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 {
        public static void main(String args[]){
            TestNative nt = new TestNative();

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

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

    Any suggestions???
    Thanks in advance

  2. Hi,

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

  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.