Basic class question

J

joe

I have the following header file generated by Java JNI's header file
generator (see bottom).

Obviously I will write a source file which will provide an implementation of
these methods.

I have a few questions regarding this:

1) Where is the class name defined? Is this a C++ class? It says "class
TestAPIMsg" in a generated comment but I can't see where this is set in the
header file.

2) How do I set global variables in this class (baring in mind it tells me
not to edit the header file)?

3) How do I add additional methods to this class i.e. ones not set in the
header file (again baring in mind I can't edit the header)?


/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class TestAPIMsg */

#ifndef _Included_TestAPIMsg
#define _Included_TestAPIMsg
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: TestAPIMsg
* Method: SendMessageTest
* Signature: (Ljava/lang/String;)Z
*/
JNIEXPORT jboolean JNICALL Java_TestAPIMsg_SendMessageTest
(JNIEnv *, jobject, jstring);

/*
* Class: TestAPIMsg
* Method: IncomingMessage
* Signature: (Ljava/lang/String;)V
*/
JNIEXPORT void JNICALL Java_TestAPIMsg_IncomingMessage
(JNIEnv *, jobject, jstring);

/*
* Class: TestAPIMsg
* Method: DoConnect
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_TestAPIMsg_DoConnect
(JNIEnv *, jobject);

/*
* Class: TestAPIMsg
* Method: DoDisconnect
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_TestAPIMsg_DoDisconnect
(JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif
 
T

TB

joe skrev:
I have the following header file generated by Java JNI's header file
generator (see bottom).

Obviously I will write a source file which will provide an implementation of
these methods.

I have a few questions regarding this:

1) Where is the class name defined? Is this a C++ class? It says "class
TestAPIMsg" in a generated comment but I can't see where this is set in the
header file.

The generated functions combined form a class interface even though
there isn't a formal class definition available. Most likely "jobject"
is a pointer to a structure of some sort that contains necessary
instance variables.
2) How do I set global variables in this class (baring in mind it tells me
not to edit the header file)?

What "global variables in this class"?
3) How do I add additional methods to this class i.e. ones not set in the
header file (again baring in mind I can't edit the header)?

Just add them.
 
B

Bob Hairgrove

I have the following header file generated by Java JNI's header file
generator (see bottom).

Obviously I will write a source file which will provide an implementation of
these methods.

I have a few questions regarding this:

1) Where is the class name defined? Is this a C++ class? It says "class
TestAPIMsg" in a generated comment but I can't see where this is set in the
header file.

It appears to be a Java class. The header is there so that a C or C++
program can call exported methods in the Java unit containing the
implementation of TestAPIMsg.
2) How do I set global variables in this class (baring in mind it tells me
not to edit the header file)?

If it is a Java class, you need to edit the Java program's source code
so that the methods to set them are exported, then regenerate the
header file.
3) How do I add additional methods to this class i.e. ones not set in the
header file (again baring in mind I can't edit the header)?

Again, in the Java program's source code, then regenerate the header.
 
A

Alf P. Steinbach

* joe:
I have the following header file generated by Java JNI's header file
generator (see bottom).

Obviously I will write a source file which will provide an implementation of
these methods.
Goodie.


I have a few questions regarding this:

I have moved to after the code.

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class TestAPIMsg */

#ifndef _Included_TestAPIMsg
#define _Included_TestAPIMsg
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: TestAPIMsg
* Method: SendMessageTest
* Signature: (Ljava/lang/String;)Z
*/
JNIEXPORT jboolean JNICALL Java_TestAPIMsg_SendMessageTest
(JNIEnv *, jobject, jstring);

/*
* Class: TestAPIMsg
* Method: IncomingMessage
* Signature: (Ljava/lang/String;)V
*/
JNIEXPORT void JNICALL Java_TestAPIMsg_IncomingMessage
(JNIEnv *, jobject, jstring);

/*
* Class: TestAPIMsg
* Method: DoConnect
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_TestAPIMsg_DoConnect
(JNIEnv *, jobject);

/*
* Class: TestAPIMsg
* Method: DoDisconnect
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_TestAPIMsg_DoDisconnect
(JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif

> 1) Where is the class name defined?

This header file does not define a class.

> Is this a C++ class?

Is what a C++ class? You have generated this header from Java source
code. Presumably you have /some/ understanding of that.

> It says "class TestAPIMsg" in a generated comment but I can't see
> where this is set in the header file.

What /what/ is "set"?


> 2) How do I set global variables in this class (baring in mind it
> tells me not to edit the header file)?

In which class?

> 3) How do I add additional methods to this class i.e. ones not set in the
> header file (again baring in mind I can't edit the header)?

Which class?
 
D

Dave Townsend

JNI doesn't generate a class or any C++ code, only the header file. The
header file
provides the declarations of extern "C" functions which you will have to
implement in a shared library or DLL. If you implement things correctly,
your java code can load the library
at run time and call the interface functions you have written. You must
understand that
the functions in the interface are "free" functions (ie, not members of a
class), but you can
implement their functionality using classes and member functions if you
wish. The "class"
name appearing in the generated file is the original Java class which you
used to generate
the JNI interface.



For instance, in your C/C++ you will have to implement this function:
JNIEXPORT jboolean JNICALL Java_TestAPIMsg_SendMessageTest
(JNIEnv *, jobject, jstring);

which might look something like:
JNIEXPORT jboolean JNICALL Java_TestAPIMsg_SendMessageTest
(JNIEnv *, jobject, jstring)
{
// convert jstring into string object.....

cout << "Received test message.....\n";
}

If you want to generate a class on the C/C++ side, you will need to add more
functions to
the Java JNI interface, such as createMyClass(), destroyMyClass(), etc.

You will not get very far unless with JNI you read the documentation for
JNI, I'd recommend getting one of the two main books on the subject, JNI is
a very complicated system, more complex than I could explain in an email.

dave
 

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,968
Messages
2,570,154
Members
46,702
Latest member
LukasConde

Latest Threads

Top