简体   繁体   中英

Could not find the class while using the java class inside the c++

I was following the https://www.ibm.com/developerworks/java/tutorials/j-jni/j-jni.html to use java class functions inside the c++ code.I am using the same code that they are using for Sample2.java. I did it in two different ways.

First way : I copied and pasted the same code in 'sublime text editor', and saved it under the name Sample2.java in Desktop . The code looks like :

 public class Sample2
     {
       public static int intMethod(int n) 
       {
           return n*n;
       }

     public static boolean booleanMethod(boolean bool) 
     {
          return !bool;
     }
    }

Then I compiled it using the following code : javac Sample2.java

My C++ code looks as follows :

#include <iostream>
#include "jni.h"
#include <string.h>
#include <typeinfo>
int main()
{
      JavaVMOption options[1];
      JNIEnv *env;
      JavaVM *jvm;
      JavaVMInitArgs vm_args;
      long status;
      jclass cls;
      jmethodID mid;
      jint square;
      jboolean answer;

 options[0].optionString = const_cast<char *>("-Djava.class.path=/home/aaa/Desktop/");

   memset(&vm_args, 0, sizeof(vm_args));
   vm_args.version = JNI_VERSION_1_8;
   vm_args.nOptions = 1;
   vm_args.options = options;
   status = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args); //If JVM creation is successful then status value is zero
   std::cout<<"Status of JNI creation is : "<<status<<std::endl;

   if (status != JNI_OK)
   {
     std::cout<<"JNI creation Failed : "<<std::endl;
     return 1;
   }
   std::cout<<"JNI creation Passed : "<<std::endl;
   cls = env->FindClass("Sample2");  // If the class cannot be found, cls will be zero.
   std::cout<<"The class of cls is : "<<typeid(cls).name()<<std::endl;
   // cls = env->FindClass("Sample2"); 
   std::cout<<"The value of cls is : "<<cls<<std::endl;
     if(cls !=0)
     {   mid = env->GetStaticMethodID(cls, "intMethod", "(I)I");
         if(mid !=0)
         {  square = env->CallStaticIntMethod(cls, mid, 5);
            printf("Result of intMethod: %d\n", square);
         }
     }
     jvm->DestroyJavaVM();
    return 0;
 }

Now when I use the following command in the 'Ubuntu' terminal, the C++ code successfully executes the java function.

Simple text editor and using an eclipse. The JNI works perfectly with the Sample2.java code, which is written using a simple text editor. But, when I use the same Sample2.java code inside package cversion.xeger then it somehow does not finds the class Sample2.java and the function intMethod of the Sample2.java class from the c++ code.

g++ -g -I/usr/lib/jvm/java-8-oracle/include/ -I/usr/lib/jvm/java-8-oracle/include/linux/ -L/usr/bin/java -L/usr/lib/jvm/java-8-oracle/jre/lib/amd64/server/ intMethod.cpp -ljvm

Problem : The problem occurs when I write the same Sample2.java code in ecllipse under the package cversion.xeger. The Sample2.java code in ecllipse is same as previous except that it has package name on the top. It looks like : package cversion.xeger;

public class Sample2
{
    public static void main(String args[]) {
        System.out.println("Helloworld");
    }
  public static int intMethod(int n) 
  {
      return n*n;
  }

    public static boolean booleanMethod(boolean bool) 
    {
          return !bool;
    }
}

I change the classpath in the Cpp file to :

 options[0].optionString = const_cast<char *>("-Djava.class.path=/home/aaa/eclipse-workspace/xeger/target/classes/");

and cls value to

cls=env->FindClass("cversion.xeger.Sample2");

but cls value is zero.This means it was not able to find the class.

I have provided the classpath upto classes inside the ecllipse workspace and I am calling the Sample2 class using the 'packagename.Sample2' inside FindClass() method.

Why is it not finding the class when I use the Sample2.java class in ecllipse? I am stuck on this one.

FindClass("cversion.xeger.Sample2");

This call is not correct. See the documentation. It should be

FindClass("cversion/xeger/Sample2");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM