views:

26

answers:

2

Hi,

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.

A: 

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

leoJava
Thanks for both answers :)
sparkFinder
A: 

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

leoJava
So - I don't need to include the header file for 'myPackageBoo', but just the jni.h? As long as I get the exact prototype specified by the machine generated header?
sparkFinder