I am new to this forum, so please excuse me if i cannot put my question in a right way.
I want help in using a C++(.lib and.h)
files in java
.
I want to use methods of that .lib
file in my java code .
Prototype of function is like :
FunctionXYZ(BYTE *Data1, BYTE *Data2, BYTE *Data3, int Data4);
Environment i would be using is centOS.
Thank you in advance
P.S: I do not have source code for this .lib
Java native interface (JNI) is a framework provided by java that enables java programs to call native code and vice-versa. Using JNI a java program has the capability to call the native C code.
C is a compiled language that is it converts the code into machine language so that it could be understood by the machine or system. Java is an Interpreted language that is in Java, the code is first transformed into bytecode and that bytecode is then executed by the JVM (Java Virtual Machine).
C/C++ and Java are popular programming languages. The Java Native Interface (JNI) is a standard to integrate in a portable way C++ and Java code. JNI works both way i.e. C++ implementation can be called from JAVA and vice versa.
To use functions from a .lib
, you have to create JNI wrapper functions for these library functions, and then link them together with your library into a .dll
.
Example:
Assuming you have a function in your C++ library headers with this signature:
int example(int a, int b);
Create a function wrapper in C++:
JNIEXPORT jint JNICALL Java_MyClass_example (JNIEnv* env, jobject obj, jint a, jint b) {
return (jint) example(a, b);
}
Link the library and the wrapper into a DLL
Create a Java class with the native method:
public class MyClass {
public native int example(int a, int b);
}
Load the DLL using the System.loadLibrary
function (or similar)
example
method on an object of MyClass
you can use JNI technology which enables you to interoperate with native code
please refer to this http://www3.ntu.edu.sg/home/ehchua/programming/java/JavaNativeInterface.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With