Solved

JNI: Access a class declared in C++

Posted on 2002-06-17
6
238 Views
Last Modified: 2012-05-05
Dear all,

I have a class declared in C++ and I want to access it in Java code. The problem is I need several instances of the class, so I can't make a global reference to it.

Is there any method that I can pass a pointer of C++ to the Java code? And I need to do something with the class in Java code.

Thank you.

pigpig
0
Comment
Question by:pigpig
  • 3
  • 3
6 Comments
 
LVL 7

Expert Comment

by:Igor Bazarny
ID: 7086189
Hi,

You need to write JNI wrapper for your C++ class. Usually C++ pointer fits into java int, so your java class would look like:

class JavaWrapper{
    private int cppClass;
    public JavaWrapper(){
        init();
    }
    public native void doSomething();
    private native void init();
}

On JNI side, create instance of C++ class in init() implementation and store it in the cppClass field. In other methods you can get cppClass fiels and cast it back to C++ pointer type.

Possibly you will need to take care of memory issues--and it's non-trivial. In java, you know when you need new instance, but don't know when you don't need it anymore. You can use finalize() or weak references to clean up your C++ memory.

Regards,
Igor Bazarny,
Brainbench MVP for Java 1
 
0
 

Author Comment

by:pigpig
ID: 7097757
Hello bazarny,

Do you have some sample codes in doing the pointer conversion in C++? I have some trouble in it.

thx
pigpig
0
 
LVL 7

Expert Comment

by:Igor Bazarny
ID: 7098103

What kind of problem do you have? Is it access to java field or pure C++ problem? I think you just need to cast pointer to long and set value to java field. When you need it back, cast long to same pointer type (unless you are very strict about inheritance/virtual functions use, numeric values of different pointer types pointing to tha same instance may differ)

Regards,
Igor Bazarny


0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

Author Comment

by:pigpig
ID: 7098184
Can u see my problem?
I have a piece of code like that in C++:

#include <iostream.h>

class A
{
public:
     A(int n) : no(n) {}
     int no;
};

void main()
{
     A *a = new A(9);
     
     int apt = (int) &a;

        A *b = (A *)&apt;
        cout << b->no << endl;

     delete a;
}
0
 
LVL 7

Accepted Solution

by:
Igor Bazarny earned 100 total points
ID: 7098249

> int apt = (int) &a;
you take address of a itself. It should be apt = (int)a;
> A *b = (A *)&apt;
Here again, you take address of apt and convert it into pointer. Write: A* b = (A*)apt;
0
 

Author Comment

by:pigpig
ID: 7109905
done !!!!!!!!
thx very much~~~~~~
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
micro services spring boot application error 3 196
more than one jdk and one jre 1 41
Oracle SQL syntax check  without executing 6 55
How do I remove an object from a 3 26
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question