开发者

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:

  1. 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... */

  1. 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... */

  1. 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:

$(CC) $(CCOPTS) [$(CCOPTS64)] $(JAVAOPTS) LIB.c -o libPOW.so
Where:
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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜