header file to include java classes?

S

Swapnil Kale

Hi,
I'm working on a Migration project (Forte to JAVA).

The forte client had a C++ dll which used to call one more FORTE dll
for a complex database calculations.

Now all the forte code has been migrated to JAVA except this piece of
code where C++ dll calls Forte DLL.


I'm using JNI to call the Java classes from C++. Some of the methods
in C++ has the header files where the signature has reference to the
Forte Classes (Which are now Java Classes).

How do I change the header files to include Java classes in the
signature?

(I couldnt find a strong reference for this, but i assume that for all
the classes in Java I'll need to create the .h file using javah
utility.


Here is the Sample in one of the C++ header files:

qqhConnectionHandleClass * m_ForteConnHandle; //
qqhConnectionHandleClass was Forte class which is now a
ConnectionHandleClass in Java.


HWND m_hwndParent;
int m_autocommit;
StatementHandle * m_StmtHandles;
.....

Almost all the signatures in the header files of C++, have references
to Forte / Java classes.

Do I need to use javah and create a header file for the classes and
then include in the C++ header file?


In the implementation class (C++) just including jni.h solves my
problem as it is enough to create the jvm and loading classes/
executing methods..


Any help is appreicated.

Newbie in C++ :)

Thanks,
Swapnil.
 
I

Ioannis Gyftos

I'm using JNI to call the Java classes from C++. Some of the methods
in C++ has the header files where the signature has reference to the
Forte Classes (Which are now Java Classes).
[...]

This is offtopic here.

I do have a bit of experience in this myself, but I am not sure what
your problem is. You mention in the last paragraph of yours that you
made the link between java and c++. Do you have any particular problem
or are you just unsure?

I do recognize, though, that there is not too much documentation on
the net, and a large part of it is very outdated.

Well, in short, you design your Java classes and use javah to create
the headers. In C++, you can #include this header alongside jni.h and
make a library. In it, you can communicate with Java through
GetObjectClass, GetFieldID etc. When you are done, in Java, you
declare those functions (take note of the native keyword) preferably
in some wrapper class, which also has something like:

public class BlahBlah{
static {
System.load("/home/trail/libSomething.so");
}

to load the library.

Note that there is a better way to load it that does not need an
absolute path, but I don't remember the methods atm.

The C++ function would like like this:

JNIEXPORT void JNICALL Java_ClassName_MethodName(
JNIEnv * JAVA, jclass Ccls,
/*in*/ jstring j_something, jint j_somethingelse,
/*out*/ jobject ret_obj)
{
/* get info from Java */
int shalala = j_somethingelse;
const char* c_something = JAVA->GetStringUTFChars(j_something,
NULL);
...
JAVA->ReleaseStringUTFChars(j_something, c_something);
...
/* change Java class members' data */
jclass cls = JAVA->GetObjectClass(ret_obj);
jfieldID fid_res = JAVA->GetFieldID(cls, "resultCode", "I");
JAVA->SetIntField(ret_obj, fid_res, RES_SUCCESS_OR_WHATEVER);
}
How do I change the header files to include Java classes in the
signature?

I am unsure of what you mean by this.
 
S

Swapnil Kale

Hi,
Thanks for the update.

Let me be more specific on this.

I've a C++ dll which is calling FORTE (the language developed by SUN
10/15 yrs back. Now it is outdated). Now after I migrate the project
to JAVA (Forte was pretty similar to JAVA), My task would be to call
the Java programs into the existing C++ dlls.

I could use the JVM and the getobject methods etc...using JVM
creattion in C++.

My problem: The existing header files of the dlls / C++ contains the
type of the classes .

/*
// EnvironmentHandle.h: interface for the EnvironmentHandle class.
*/
e.g. qqhEnvironmentHandleClass * GetForteHandle(void) { return
m_ForteEnvHandle;}

qqhEnvironmentHandleClass was a Forte class which is a
EnvironmentHandleClass in Java now. (not reflected in the code),

The new code could be EnvironmentHandleClass * GetForteHandle(void)
{ return m_ForteEnvHandle;}

//just an assumption, didnt find the right way yet.

Now How do I refer this converted Java Class in the C++ header.

How do I tell the C++ header where to look for the Java classs?

Regards,
Swapnil
--------------------------------------------------------------------



in C++ has the header files where the signature has reference to the
Forte Classes (Which are now Java Classes).

[...]

This is offtopic here.

I do have a bit of experience in this myself, but I am not sure what
your problem is. You mention in the last paragraph of yours that you
made the link between java and c++. Do you have any particular problem
or are you just unsure?

I do recognize, though, that there is not too much documentation on
the net, and a large part of it is very outdated.

Well, in short, you design your Java classes and use javah to create
the headers. In C++, you can #include this header alongside jni.h and
make a library. In it, you can communicate with Java through
GetObjectClass, GetFieldID etc. When you are done, in Java, you
declare those functions (take note of the native keyword) preferably
in some wrapper class, which also has something like:

public class BlahBlah{
static {
System.load("/home/trail/libSomething.so");
}

to load the library.

Note that there is a better way to load it that does not need an
absolute path, but I don't remember the methods atm.

The C++ function would like like this:

JNIEXPORT void JNICALL Java_ClassName_MethodName(
JNIEnv * JAVA, jclass Ccls,
/*in*/ jstring j_something, jint j_somethingelse,
/*out*/ jobject ret_obj)
{
/* get info from Java */
int shalala = j_somethingelse;
const char* c_something = JAVA->GetStringUTFChars(j_something,
NULL);
...
JAVA->ReleaseStringUTFChars(j_something, c_something);
...
/* change Java class members' data */
jclass cls = JAVA->GetObjectClass(ret_obj);
jfieldID fid_res = JAVA->GetFieldID(cls, "resultCode", "I");
JAVA->SetIntField(ret_obj, fid_res, RES_SUCCESS_OR_WHATEVER);

}
How do I change the header files to include Java classes in the
signature?

I am unsure of what you mean by this.
 
I

Ioannis Gyftos

Now How do I refer this converted Java Class in the C++ header.

How do I tell the C++ header where to look for the Java classs?

I believe you can't use the whole Java class in C++ through JNI. JNI
is used to access and call member functions. The 'native' Java keyword
refers to functions that run outside the JVM, but not to classes

If you use javah on a Java member function (well, on a .java file but
you get what i mean), you get a C++ header with also a parameter that
is a handle to that Java object. You can use that handle to get access
to other variables of that class and stuff like that through the JNI
API. You also have similar access to objects that are parameters to
the function call. But you don't get a full class in the C++ header.

Why do you need the full Java Class in C++? If, for example, you are
trying to create an object through its Java class in C++, i doubt you
would be able to do it directly. JNI might provide functionality for
something like that, though, but i didn't need it so I didn't
research :) However I don't recall seeing anything like that, either.
However, you could create that Java object through a Java factory,
that returns an instance of that Java object, that C++ can have access
to.

Are you trying to have a C++ executable that creates the JVM? In that
case, from what I understand, it might be better to have a Java jar
that loads a C++ library that only deals with the dll.

Think of JNI as a bridge between functions, not classes. Besides,
classes are a compile-time construct and objects are a run-time one.
This communication is done dynamically, so you have to deal with
objects.

Forgive my horrible terminology, I was in a hurry :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top