0

Situation: I have a library of JNI files, the library is comprised of several functions that are called by the main header file in that JNI library (i.e., code1.h). I have a Java file (i.e., code2.java) that I want to pass to and from JNI header file (code1.h). I created a source code for the (code1.h) called (code1.c).

My question is: Does (code1.h), (code1.c), and (code2.java) have to be the same name for the communication between the JNI and the java?

EDIT: So (code1.h), (code1.c), and (code1.java) all have to be the same name in order for the (code1.java) to pass strings to/from (code1.c)/(code1.h)? And it is not possible to have (code2.java) pass strings to/from (code1.c)/(code1.h) because they are not named the same, is this correct?

For instance,

public class code1 { /*this is code2.java, but should the name be changed to (code1.java) to match that of the JNI?*/
    static {
    System.loadLibrary("myjni"); 

}

to pass strings to code1.h/code1.c

This will be compiled for android using Linux Debian"Wheezy" and Eclipse with Android SDK and NDK

Aubrey Smith
  • 5
  • 1
  • 5
  • 2
    The name of the top-level public Java class (of which there can be only one per file) [must match the name of the file](http://stackoverflow.com/a/1841880/2864740) or it won't compile. This makes the presented scenario invalid to begin with. (Perhaps ask the question but change the names of the .c/.h files while leaving the .java file correctly named according to the public class it contains.) – user2864740 Dec 06 '13 at 01:36
  • If you question really is only about naming the class with respect to the file name, then as @user2864740 said, they must match (as the case with all Java classes). – under_the_sea_salad Dec 06 '13 at 02:24

2 Answers2

1

While Java requires a match between compilation unit name (SomeClass.java being the name and public class SomeClass{ being the declaration, C does not require this.

You may name the C source and header files as you see fit as long as the function names/exported symbol names match the name of the native method on the java side. For example:

//JavaClass.java
public class JavaClass{
    public native String getAString(String in);
}

And header would be:

// any name
JNIEXPORT jstring JNICALL 
Java_JavaClass_getAString(JNIEnv *, jobject, jstring);

with matching C files. You could name this header catsMakeTheWorldGoRound.h for all Java cares.

nanofarad
  • 40,330
  • 4
  • 86
  • 117
0

Here is an example of what your "JNI object" should look like.

//In my experience, it is better to put the JNI object into a separate package.
package org.example;
public class Code1
{
    static
    {
        // On a Linux system, the actual name of the library
        // is prefixed with "lib" and suffixed with ".so"
        // -- e.g. "myjni-java.so"
        // Windows looks for "myjni-java.dll"
        //
        // On a Windows system, we also need to load the prequisite
        // libraries first. (Linux loaders do this automatically).
        //
        String osname = System.getProperty("os.name");
        if (osname.indexOf("win") > -1 || osname.indexOf("Win") > -1)
        {
            System.loadLibrary("myjni");
        }
        System.loadLibrary("myjni-java");
    }

    // Now we declare the C functions which we will use in our Java code. 
    public static native void foo(int bar);

    public static native int bar(String foo);
    //...
}

Given that you have compiled your JNI library correctly, you can then call the C functions from other Java classes like this:

//Again, in my experience, it is better to explicitly give the package name here. 
org.example.Code1 Code1= new org.example.Code1();
Code1.foo(123);
int a= Code1.bar("Hello C function from Java function!");

Does this help you with your question? (I am not an expert in JNI, so I might not be able to help further.)

under_the_sea_salad
  • 1,754
  • 3
  • 22
  • 42
  • OP's question is a bit different--does the filename of the c header or source matter? – nanofarad Dec 06 '13 at 17:22
  • Since I am working on a Linux, should the load library look like this [System.loadLibrary("libmyjni-java")] instead of [System.loadLibrary("myjni-java")]? Since Linux is looking for the "lib" prefix. – Aubrey Smith Dec 06 '13 at 17:24