6 Replies Latest reply: Mar 26, 2008 10:33 AM by 807601 RSS

    Getting NoClassDefFoundError on using JNI

    807601
      Hi all,

      I'm trying to call a JNI function, to get data continuously. Since, I need this feature from an Eclipse plugin, I have placed the native code dll in Eclipse root folder

      From the native code I'm trying to invoke a class which in my Eclipse plugin, using FindClass method of JNIEnv interface pointer. At this point I'm getting NoClassDefFoundError.
      java.lang.NoClassDefFoundError: test/Grab
      **********************exception found****************
      Exception in thread "Thread-4" 
      JavaVM *jvm_p;
      static jclass cls;
      
      // It just trigger the CreateThread thread and give back the control to the calling block
      
      JNIEXPORT void JNICALL Java_test_Grab(JNIEnv *env)
      {
      if (env->GetJavaVM(&jvm_p) < 0) {
              printf("JNI ERROR: Failed getting reference to Java VM");       
          }
      
           ....
      
          ImaqBuffer = NULL;
      
           StopGrab = FALSE;
           DWORD               dwThreadId;
      
           HThreadGrab = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) ImaqThreadGrab, env, 0, &dwThreadId);               
           if (HThreadGrab == NULL)
                cout<<"HThreadGrab == NULL"<<endl;
      }
      
      // Need to send the data 'byte_array' data continuously back to a java class method
      DWORD ImaqThreadGrab(JNIEnv *env)
      {
           if (jvm_p->AttachCurrentThread((void**)&env, NULL) != JNI_OK) {
              printf("ERROR: Could not attach event thread to JVM");
              return 0;
          }
      
           byte     byte_array[] = {0,1,2,3,4,5,6,7,8,9};
      
      
           jthrowable e;
           cls = env->FindClass("test/Grab"); 
           e = env->ExceptionOccurred();
           if(e) {
                cout<<"\n**********************exception found****************\n";
                env->ExceptionDescribe();
           }
      
           static int error;
      
           while(StopGrab == FALSE)
           {
                jmethodID midByteArray = env->GetStaticMethodID(cls, "printByteArray", "([B)V");
                (jbyteArray)env->CallByteMethod(cls, midByteArray, byte_array);
      
           }
           jvm_p->DetachCurrentThread();
           return 0;
      }
      Java code:
      public class Grab {
      
           public static void printByteArray(byte[] a) {
                int length = a.length;
                int counter = 0;
                while (counter < 10) {
                     System.out.print(" " + a[counter]);
                     counter++;
                }
           }
      }
      I'm stuck up with this issue for more than 2 days, on googling it, i found a similar thread 'http://forum.java.sun.com/thread.jspa?forumID=52&threadID=5258507' and something I didn't able to figure out it.

      Could any one please point me out what is the problem here?

      Thanking in advance,
      Ramesh
        • 1. Re: Getting NoClassDefFoundError on using JNI
          791266
          Java code:
          public class Grab {
          
               public static void printByteArray(byte[] a) {
                    int length = a.length;
                    int counter = 0;
                    while (counter < 10) {
                         System.out.print(" " + a[counter]);
                         counter++;
                    }
               }
          }
          Is that class declared to be within the test package?
          • 2. Re: Getting NoClassDefFoundError on using JNI
            807601
            Hi kajbj,

            I'm sorry, i missed to include the package name in my initial post, here is the updated code with package name
            package test;
            
            public class Grab {
             
                 public static void printByteArray(byte[] a) {
                      int length = a.length;
                      int counter = 0;
                      while (counter < 10) {
                           System.out.print(" " + a[counter]);
                           counter++;
                      }
                 }
            }
            • 3. Re: Getting NoClassDefFoundError on using JNI
              791266
              What is the classpath set to?
              • 4. Re: Getting NoClassDefFoundError on using JNI
                807601
                Hi kajbj,

                One more information, for following code works fine if I didn't use the Win32 API thread, as follows
                JNIEXPORT void JNICALL Java_test_Grab(JNIEnv *env)
                {
                     StopGrab = FALSE;
                
                     if (jvm_p->AttachCurrentThread((void**)&env, NULL) != JNI_OK) {
                             printf("ERROR: Could not attach event thread to JVM");
                        }
                
                     jthrowable e;
                     cls = env->FindClass("test/Grab"); 
                     e = env->ExceptionOccurred();
                     if(e) {
                          cout<<"\n**********************exception found****************\n";
                          env->ExceptionDescribe();
                     }
                
                     byte     byte_array[] = {0,1,2,3,4,5,6,7,8,9};
                
                     // the thread stop when StopGrab goes to TRUE
                     while(StopGrab == FALSE)
                     {
                          jmethodID midByteArray = env->GetStaticMethodID(cls, "printByteArray", "([B)V");
                          (jbyteArray)env->CallByteMethod(cls, midByteArray, byte_array);
                     }
                     jvm_p->DetachCurrentThread();
                }
                But the issue with above code is, since it is running in a while a loop the caller does not have control over to stop the Grab thread at a particular point.

                So, anything I'm not handling the JNIEnv interface pointer?

                Thanks in advance,
                Ramesh
                • 5. Re: Getting NoClassDefFoundError on using JNI
                  807601
                  Hi kajbj,

                  I didn't set any classpath in dll side, Since the dll is placed in root of the Eclipse plugin folder, I assume, whatever the classpath is set with the Eclipse plugin, will be available to the dll functions. Please make me if I'm wrong.

                  Thanks in advance,
                  Ramesh
                  • 6. Re: Getting NoClassDefFoundError on using JNI
                    807601
                    Hi all,

                    Any Java/JNI/Eclipse Gurus can help to resolve this issue?

                    1. Either from a Win32 API thread or from the native method, I able to load the 'java/lang/System' class. BTW, i able to load my plugin class 'test/Grab' from the native method, but why not it is possible from the Win32 API thread.

                    2. I invoke the native function using this signature
                    JNIEXPORT void JNICALL Java_test_Grab (JNIEnv *);
                    In my native function, with this JNIEnv interface pointer I'm getting the JavaVM
                        if (env->GetJavaVM(&jvm_p) < 0) {
                            printf("JNI ERROR: Failed getting reference to Java VM");       
                        }
                    So, the classes available in my plugin class loader should be available to the Native JavaVM, please nudge me if i'm wrong.

                    I greatly welcome any points/solutions.
                    Thanking in advance,

                    Ramesh