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 - class llist
-
{
-
private:
-
record *start;
-
char filename[16];
-
int readfile();
-
int writefile();
-
record * reverse(record *);
-
void cleanup();
-
-
public:
-
llist();
-
llist(char[]);
-
~llist();
-
int addRecord(char[], char[], int, char[]);
-
int printRecord(char[]);
-
int modifyRecord(char[], char[], int, char[]);
-
void printAllRecords();
-
int deleteRecord(char[]);
-
void reverse();
-
};
then the C++ code: - int llist::readfile()
-
{
-
-
}
-
-
int llist::writefile()
-
{
-
-
}
-
-
llist::llist()
-
{
-
-
}
-
-
llist::llist(char name[])
-
{
-
-
}
-
-
llist::~llist()
-
{
-
-
}
-
-
int llist::addRecord(char name[], char address[], int yearofbirth, char telno[])
-
{
-
;
-
}
-
-
int llist::deleteRecord(char name[])
-
{
-
-
}
-
-
int llist::modifyRecord(char name[], char address[], char telno[])
-
{
-
-
}
-
-
void llist::printAll()
-
{
-
-
}
-
-
-
record::record()
-
{
-
}
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: - class UserInterface {
-
public native int addRecord(char [] name, char [] address, ....etc);
-
...
-
....
-
and so on
-
-
static {
-
System.loadLibrary("UserInterface");
-
}
-
-
code....
-
.................
-
}
6 11853
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
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... - class llist_H{
-
-
JNIEXPORT jint JNICALL Java_UserInterface_addRecord(..............)
-
(JNIEnv *, jobject, jint);
-
...
-
....
-
}
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
Thanks Jos! Okay....so something like the void initializer(); method in C++ would call the constructor and same thing with the destructor. - void initializer()
-
{
-
llist();
-
}
and - void initializer(char [] filename)
-
{
-
llist(filename);
-
}
Then call the function in java by public native void initializer(); Since the constructor is public, I can declare it anywhere.
Regards,
Jthep
Thanks Jos! Okay....so something like the void initializer(); method in C++ would call the constructor and same thing with the destructor. - void initializer()
-
{
-
llist();
-
}
and - void initializer(char [] filename)
-
{
-
llist(filename);
-
}
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 initializitation 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
Thanks. Btw, when compiling and running this code on UNIX, is there a way to run javax.swing.JOptionPane 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 UnsatisfiedLinkedError.
Regards,
Jthep
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
by: qazmlp |
last post by:
class base
{
// other members
public:
virtual ~base()
{
}
virtual void virtualMethod1()=0 ;
virtual void virtualMethod2()=0 ;
virtual void virtualMethod3()=0 ;
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
| |