473,795 Members | 3,100 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using JNI to access C++ class member functions

34 New Member
Hi I'm trying to write a JNI for a linkedlist class I wrote in C++. Basically I have a header file with the class definition and a C++ file with the definition of the class member functions.

I have a record file llist.h

Expand|Select|Wrap|Line Numbers
  1. class llist
  2. {
  3.     private:
  4.         record        *start;
  5.         char          filename[16];
  6.         int           readfile();
  7.         int           writefile();
  8.         record *      reverse(record *);
  9.         void          cleanup();
  10.  
  11.     public:
  12.         llist();
  13.         llist(char[]);
  14.         ~llist();
  15.         int addRecord(char[], char[], int, char[]);
  16.         int printRecord(char[]);
  17.         int modifyRecord(char[], char[], int, char[]);
  18.         void printAllRecords();
  19.         int deleteRecord(char[]);
  20.         void reverse();
  21. };
then the C++ code:

Expand|Select|Wrap|Line Numbers
  1. int llist::readfile()
  2. {
  3.  
  4. }
  5.  
  6. int llist::writefile()
  7. {
  8.  
  9. }
  10.  
  11. llist::llist()
  12. {
  13.  
  14. }
  15.  
  16. llist::llist(char name[])
  17. {
  18.  
  19. }
  20.  
  21. llist::~llist()
  22. {
  23.  
  24. }
  25.  
  26. int llist::addRecord(char name[], char address[], int yearofbirth, char telno[])
  27. {
  28. ;
  29. }
  30.  
  31. int llist::deleteRecord(char name[])
  32. {
  33.  
  34. }
  35.  
  36. int llist::modifyRecord(char name[], char address[], char telno[])
  37. {
  38.  
  39. }
  40.  
  41. void llist::printAll()
  42. {
  43.  
  44. }
  45.  
  46.  
  47. record::record()
  48. {
  49. }
Then I need a Java Code. For the JNI, I will create a method to display a menu that allows a user to add, delete, modify, and display records. The JNI will call the functions written in C++. But what is the code I put in the Java file to access the class functions? Normally, if it's just a regular function and not part of a class member, it would go something like:

Expand|Select|Wrap|Line Numbers
  1. class UserInterface {
  2.     public native int addRecord(char [] name, char [] address, ....etc);
  3.     ...
  4.     ....
  5.     and so on
  6.  
  7.    static {
  8.         System.loadLibrary("UserInterface");
  9.     }
  10.  
  11.     code....
  12.     .................
  13. }
Dec 4 '07 #1
6 11898
JosAH
11,448 Recognized Expert MVP
That's exactly how it's done. The javah tool generates the necessary header file
given your compiled .class file that contains the 'native' definitions. The generated
.h file contains the prototypes of the C/C++ functions you have to create yourself.

Those functions normally act as wrappers for your real functions. Compile and
link the stuff into a .dll (or .so file if you're on unix) and off you go.

kind regards,

Jos
Dec 4 '07 #2
jthep
34 New Member
So what if I wanted to call the constructor llist() to create a list and initialize all members of the class definition? public native Ilist(); gives me a syntax error since I have no return type. Since both are declared as private, I wrote the constructor and destructor so that it calls the readfile() and writefile() respectively. I'm not directly calling those two functions in the java file, do I still need a native line in there?

As for the header file would it resemble a class? From the steps I read on how to use the JNI was to create the java class first, then the header file with the function definition and it would be something like this for the addRecord...

Expand|Select|Wrap|Line Numbers
  1. class llist_H{
  2.  
  3. JNIEXPORT jint JNICALL Java_UserInterface_addRecord(..............)
  4.   (JNIEnv *, jobject, jint);
  5. ...
  6. ....
  7. }
Dec 4 '07 #3
JosAH
11,448 Recognized Expert MVP
So what if I wanted to call the constructor llist() to create a list and initialize all members of the class definition? public native Ilist(); gives me a syntax error since I have no return type. Since both are declared as private, I wrote the constructor and destructor so that it calls the readfile() and writefile() respectively. I'm not directly calling those two functions in the java file, do I still need a native line in there?
You cannot call a C++ ctor from Java directly; you have to craft another native
method (in C++) that creates your C++ object for you. Java and C++ are two
separate worlds; even a pointer to a C++ object doesn't mean anything to Java
(and vice versa). You need a native 'initialize()' method in Java to set up your
C++ list object.

kind regards,

Jos
Dec 4 '07 #4
jthep
34 New Member
Thanks Jos! Okay....so something like the void initializer(); method in C++ would call the constructor and same thing with the destructor.

Expand|Select|Wrap|Line Numbers
  1. void initializer()
  2. {
  3.     llist();
  4. }
and

Expand|Select|Wrap|Line Numbers
  1. void initializer(char [] filename)
  2. {
  3.     llist(filename);
  4. }
Then call the function in java by public native void initializer(); Since the constructor is public, I can declare it anywhere.

Regards,
Jthep
Dec 4 '07 #5
JosAH
11,448 Recognized Expert MVP
Thanks Jos! Okay....so something like the void initializer(); method in C++ would call the constructor and same thing with the destructor.

Expand|Select|Wrap|Line Numbers
  1. void initializer()
  2. {
  3.     llist();
  4. }
and

Expand|Select|Wrap|Line Numbers
  1. void initializer(char [] filename)
  2. {
  3.     llist(filename);
  4. }
Then call the function in java by public native void initializer(); Since the constructor is public, I can declare it anywhere.

Regards,
Jthep
Yep, true. I normally use the Java class as a real wrapper around a C++ class.
You need to convert a C++ pointer to a, say, Java long (or small byte[]) and
vice versa.

When your Java code creates the Java wrapper, it creates a C++ object (using
that initializitatio n native method) and stores the C++ pointer to the object as a
long (or byte[]). It's up to you to implement an explicit 'delete()' method in Java
or let it go with the flow and get rid of the wrapped C++ object when/if the Java
object is finalized and its finalize() method is called (that's where you destroy
the wrapped C++ object then).

That way the rest of your Java code doesn't even 'know' that its talking to a C++
object. Internally your Java class just passes that secret pointer back and forth.

kind regards,

Jos
Dec 4 '07 #6
jthep
34 New Member
Thanks. Btw, when compiling and running this code on UNIX, is there a way to run javax.swing.JOp tionPane on UNIX? I had some old java projects that I did on eclipse using JOptionPane, I could compile and run in eclipse but I can only compile in UNIX. I couldn't run it without it giving me the UnsatisfiedLink edError.

Regards,
Jthep
Dec 5 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

2
10476
by: Kevin Saff | last post by:
Apparently I'm missing something. Stroustrup (15.3) says of protected access: If is protected, its name can be used only by member functions and friends of the class in which it is declared and by member functions and friends of classes derived from this class. Since private access cares about the calling class rather than the calling object, I assumed the same was true for protected access, but the following code fails in MSVC6:
7
9764
by: Wolfgang Jeltsch | last post by:
Hello, I want to write a list class with an iterator class as an inner class. The iterator class must have access to certain private members of the list class in order to do its job. Here is a reduced code example: class List { private: void *rootNode; class Iterator {
3
2326
by: quo | last post by:
two questions: 1) Does this program demonstrate the basic difference between public and private access? It appears correct to say that instances of a class cannot directly call a private method, but a public method can be called by the instance to invoke the private method. 2) So is it true that only public methods of a class can invoke a private method of that same class?
11
4618
by: Roger Leigh | last post by:
The C++ book I have to hand (Liberty and Horvath, Teach yourself C++ for Linux in 21 Days--I know there are better) states that "static member functions cannot access any non-static member variables". However, this doesn't seem entirely correct. It also doesn't mention whether static member functions can access protected and private member data and methods (and I couldn't spot this in the FAQ). I have a class row<Row> which derives from...
19
2351
by: qazmlp | last post by:
class base { // other members public: virtual ~base() { } virtual void virtualMethod1()=0 ; virtual void virtualMethod2()=0 ; virtual void virtualMethod3()=0 ;
34
3695
by: Dennis | last post by:
I would like to dynamically allocate in a sub a 2 dimensional Array float *myarray = new float ; of course I get an error. How do you allocate a 2D array using the New operator? I currently use static float gxy;
1
3983
by: Bryan Parkoff | last post by:
I know how to write "Pointer to Function" inside struct or class without using static, but I have decided to add static to all functions inside struct or class because I want member functions to be bound inside struct or class to become global functions. It makes easier for me to use "struct.memberfunction()" instead of "globalfunction()" when I have to use dot between struct and member function rather than global function. I do not have...
3
2070
by: Richard Webb | last post by:
Hi all, I guess this is more of a design problem than a language problem, but I'm confused either way! I have a class and it has a private data member which is a struct. The size of the struct is what I would call relatively large (about 1Mb). I have written methods for this class so that the struct can be correctly filled with the corerct data and certain parts of the struct can be extracted. But the problem I face is that I now have...
11
2554
by: Brent Ritchie | last post by:
Hello all, I have been using C# in my programming class and I have grown quite fond of C# properties. Having a method act like a variable that I can control access to is really something. As well as learning C#, I think that it's way overdue for me to start learning C++ Templates (I've been learning it for about 5 years now). I think that adding this type of functionality would be a good exercise to help learn template programming....
2
1473
by: Niklas Norrthon | last post by:
I want to share a technique I recently have found to be useful to get around some obstacles that data protection can raise. Consider the following class: // foo.h #ifndef H_FOO #define H_FOO class Foo
1
10163
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10000
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9037
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7538
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6779
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5436
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.