1
  void ProcedureParams_Initialize(ProcedureParams* pVal);
    flag ProcedureParams_IsConstraintValid(const ProcedureParams* val, int* pErrCode);
    flag ProcedureParams_XER_Encode(const ProcedureParams* val, ByteStream* pByteStrm, int* pErrCode, flag bCheckConstraints);
    flag ProcedureParams_XER_Decode(ProcedureParams* pVal, ByteStream* pByteStrm, int* pErrCode);
    flag ProcedureParams_BER_Encode(const ProcedureParams* val, ByteStream* pByteStrm, int* pErrCode, flag bCheckConstraints);
    flag ProcedureParams_BER_Decode(ProcedureParams* pVal, ByteStream* pByteStrm, int* pErrCode);

typedef struct {
    GeneralEvthParams g_params;
    DataParams d_params;
} EvtHandlerParams;

how i can add .h file directly into my .java file. i'm using NDk and .h file inside my jni folder.i want to use the functions of header file . how i can directly use functions in java activity? please help me

18446744073709551615
  • 16,368
  • 4
  • 94
  • 127
Deepak
  • 1,669
  • 1
  • 18
  • 28
  • how i can add .h file directly into my .java file. i'm using NDk and .h file inside my jni folder.i want to use the functions of header file . how i can directly use functions in java activity? please help me – Deepak Feb 26 '14 at 12:00

1 Answers1

0

You cannot invoke the C/C++ functions directly. The C implementations of Java native functions all have a very specific set of parameters:

  JNIEXPORT jint JNICALL Java_com_mycompany_MyClassName_myfunc
    (JNIEnv * env, jobject obj, jint code) {
     ...
  }

So you will have to create wrapper functions.

A reasonable approach is to define a class including the Java versions of all your native functions (as either static or instance methods), and invoke them via that class.

To generate C headers from the Java (well, not source but the .class files), you can do:

cd bin/classes
javah com.mycompany.MyClassName com.mycompany.AnotherClassName
18446744073709551615
  • 16,368
  • 4
  • 94
  • 127
  • i have already header file inside my jni folder, bt i have to only use the functions inside that header file? is it possible to call that methods directly? – Deepak Feb 26 '14 at 12:38
  • Yaa one more thing can is use structure pointers in .java class? if its possible what are the methods, i want to use because inside c source header file i have structure to pointers methods. – Deepak Feb 26 '14 at 12:39
  • Java cannot call C/C++ functions directly, the only way is to define native functions and call the C/C++ functions from them. Anyway, normal C/C++ functions cannot do anything useful with Java Objects or arrays. – 18446744073709551615 Feb 26 '14 at 12:41
  • then how we can access structure variables of .h file? – Deepak Feb 26 '14 at 12:44
  • You can keep pointers to native structures as Java integers, but freeing them will be a bit schizophrenic. Java VM has no direct access to the C structures of the underlying system. – 18446744073709551615 Feb 26 '14 at 12:44
  • in source .java file? – Deepak Feb 26 '14 at 12:44
  • access structures -- via C functions – 18446744073709551615 Feb 26 '14 at 12:45
  • the problem is that i have a thousand of methods , to handle them in java or to implement in .java file? its too difficult? any other possible way?? please suggest me more. i'm in trouble from last 5 days – Deepak Feb 26 '14 at 12:47
  • basically, you consider your C library as a Java object. Everything implemented in C is private. You define native getter/setter functions. – 18446744073709551615 Feb 26 '14 at 12:47
  • I would gradually add the methods that are needed. – 18446744073709551615 Feb 26 '14 at 12:48
  • Or, you can have one big function calling any library function by name_string or integer_id with an arbitrary list of parameters. Most likely, you have a few more or less similar groups of your functions. And not all functions need be exposed to Java. GTG now. – 18446744073709551615 Feb 26 '14 at 12:50
  • inside my .java file? have to any sample code to do this with structure pointers. – Deepak Feb 26 '14 at 12:51
  • That were 2 unrelated suggestions. 1) In theory, you can write one JNI function that can invoke any particular function from the library. You can even make a text interpreter that accepts the function name and the list of arguments (as one string or an array/list of strings) and returns the result converted to string. Although this is possible, I do not think it will be convenient because you will have to do a lot of text parsing in Java. – 18446744073709551615 Feb 27 '14 at 07:31
  • 2) Most likely, even if your task is to "port this library to Java", the library will be for some particular purpose. The project for which the library is needed will use only a limited number of the library functions. You need to create Java wrappers only for these functions. – 18446744073709551615 Feb 27 '14 at 07:36
  • So my suggestion is: write a Java app that demonstrates the most basic usage of the library; make the app work with the library; deliver the library as the 1st version. The guys who need the library will tell you that they need some more functions, they will give you a list (of missing functions). You will implement these functions, they will start using the library and give you a list of functions they need but didn't mention the 1st time. And so on. – 18446744073709551615 Feb 27 '14 at 07:44
  • Even if you manage to deliver these thousands of functions at once, who and how will test them? It would be naive to believe that this kind of code will need no testing. – 18446744073709551615 Feb 27 '14 at 07:50
  • 3rd suggestion (not sure if you are allowed to choose it): write the code that uses the library in C, do not expose any library functions to Java. You will have a few functions meaningful from the application viewpoint, end these are the functions that must be visible from Java. – 18446744073709551615 Feb 27 '14 at 07:59
  • yaa im using exactly your first method from morning. hope it will work. i make a new .c file which will work as interpreter between library and .java class. – Deepak Feb 27 '14 at 09:04
  • Take a look at languages like PostScript and Forth. They use an explicit stack and RPN (the Reversal Lukasiewicz Notation, it's a shame that they cannot spell his name and write "[some] Polish" instead). Anyway, it's easy to write an interpreter, but it's not so easy to actually _use_ that interpreter from the code (in particular, debug the thing that uses an interpreter). – 18446744073709551615 Feb 27 '14 at 09:33