Solved

JNI: Access a class declared in C++

Posted on 2002-06-17
6
240 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Suggested Solutions

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

738 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