简体   繁体   中英

Unable to call member function of C++ from java using JNI

I followed the instruction given in below said link Call c function from Java and after which I was able to make call to C++ function. But now I want to call member function of C++ class from Java. To give a clear picture of it I am citing the scenario below.

JAVA. There is a class called HelloWorld.java and it has native function called print() Now using Java's JNI I have create the equivalent header file for HelloWorld. After which i wrote the implemenation of this header file in the HelloWorld.cpp

Now from "HelloWorld.cpp" I want to call the member function of "rectangle.cpp" for which I have created object of "rectangle" and called its corresponding function. But upon compilation of code it give me an error called "unresolved external symbol". On the contrary when I write all the implemenation in corresponding header file "rectangle.cpp" that is in rectangle.h, the code compiles well and it gave me desired result. My Question, Isnt there a way, following which i can call the member function of .cpp class not its corresponding header file funcion.

Below is the code snippet::

HelloWorld.java

 class HelloWorld {

 private native void print();
 private native void multiply();


 public static void main(String[] args) {
     new HelloWorld().print();
     new HelloWorld().multiply();

 }

 static {
     System.loadLibrary("HelloWorld");
 }  }

HelloWorld.h

/* 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 *, jobject);

/*
 * Class:     HelloWorld
 * Method:    multiply
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_HelloWorld_multiply
  (JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif

HelloWorld.cpp

 #include <jni.h>
 #include <stdio.h>
 #include "rectangle.h"
 #include "HelloWorld.h"



JNIEXPORT void JNICALL   Java_HelloWorld_print(JNIEnv *env, jobject
obj)  
{
//     printf("Hello World!dfdfdfdf\n");
//     return;  

Rectangle rectangle;
rectangle.set_values(3,4);
printf("Area of Rectangle %d", rectangle.get_values());

} 
JNIEXPORT void JNICALL Java_HelloWorld_multiply
  (JNIEnv *, jobject)
{
//printf("dila ");
}

rectangle.h

#ifndef GUARD_SNAKE_H
#define GUARD_SNAKE_H
class Rectangle{
  public:
    Rectangle(void);
 //   ~Rectangle(); 


    void  set_values(int x,int y);
    int get_values();   
  private:
  int width;
  int height;


};

#endif

rectangle.cpp

// classes example
#include "rectangle.h"
Rectangle::Rectangle(void)
{
}
void  Rectangle::set_values(int x, int y) 
{
  width = x;
  height = y;
}
int Rectangle::get_values()
{
return width*height;    
}

Issue associated with aforewritten code: When I wrote all the implementation of "rectangle" in its header file "rectangle.h", it gave me the desired result. The problem is "rectangle" object created on "HelloWorld.cpp" file is not referring to "rectangle.cpp". That's why when i compiled and run it, it gave me the "unresolved external symbol" exception , which means, compiler or debugger cannot find implemenation for the function defined in "rectangel.h" file. Is there any way to solve this problem.. Please Help.

In case you want to use multiple sources, make sure to:

  • either put everything inside single library, or
  • make sure to create multiple libraries and make sure to properly link against them.

You can find very simple samples here:

https://github.com/mkowsiak/jnicookbook/tree/master/recipes/recipeNo021 https://github.com/mkowsiak/jnicookbook/tree/master/recipes/recipeNo023

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