JNI Header file generating class
I'm currently using the JNI to generate C headers for native methods being used in a Java class ABC. However, I'd like to use these methods elsewhere, in another class XYZ, so hence I made a class called cLib which basically just had the prototypes of the native methods, and which when generated gave me the header file for the methods I needed.
The 开发者_运维百科problem is, JNI attaches the name of the Java class the prototype was declared in to the name of the function in the header file, so would I need to just separately generate two header files for each of the Java classes ABC, XYZ?
Best.
Three options:
- Call same library methods from Java.
public class Boo {
public V doSomething(...) {
return (Common.doSomething(...));
}
}
public class Wow {
public V doSomething(...) {
return (Common.doSomething(...));
}
}
public class Common {
public static native V doSomething(...);
}
/** Trivial JNI Implementation omitted... */
- Call same library methods from C/Assembly.
public class Boo {
public V native doSomething(...);
}
public class Wow {
public V native doSomething(...);
}
/** Both JNI methods call same C/Assembly native function, similarly... */
- Duplicate code automatically. ;)
see java.lang.Compiler
Cheers, leoJava
Looking at the question from another point of view... there is not a problem including native code for several classes in a single LIB.c file used to construct a "libPOW.so".
Consider the following content of a file "LIB.c":
/* Common Header Files... including jni.h /
/
* Class: your.pkg.Boo
* Method: doSomething
* Signature: (I)I
*/
JNIEXPORT jint JNICALL Java_your_pkg_Boo_doSomething(
JNIEnv env, jobject jobj, jint job)
{
...
}
/
* Class: your.pkg.Wow
* Method: doSomething
* Signature: (I)I
*/
JNIEXPORT jint JNICALL Java_your_pkg_Wow_doSomething(
JNIEnv *env, jobject jobj, jint job)
{
...
}
Then compile via:
Where:
$(CC) $(CCOPTS) [$(CCOPTS64)] $(JAVAOPTS) LIB.c -o libPOW.so
CCOPTS == "-G -mt" (solaris) OR "-Wall -Werror -shared -shared-libgcc -fPIC" (Linux)
CCOPTS64 == "-xcode=pic32 -m64" (SparcV9) OR "-m64" (AMD64)
JAVAOPTS == "-I$(JAVA_HOME)/include -I$(JAVA_HOME)/include/$(OSNAME) -I."
Cheers, leoJava
精彩评论