JavacardOS will not accept order any more, please contact our partner Feitian online Store:
https://ftsafe.en.alibaba.com/index.html
https://ftsafe.en.alibaba.com/index.html
How does JAVA adjust C(JNA)?
How does JAVA adjust C(JNA)?
Java call c language library, often use two ways, JNI and JNA, using JNI way to achieve Java c, we need not only the library of c, but also need to write a c library in line with the JNI specification, this way is more trouble, need to c library Java to c data type conversion and other operations; JNA is a Java class library based on JNI technology that makes it easy to use Java to directly access functions in dynamically linked libraries without having to rewrite c libraries. JNA calls are relatively simple, requiring only that you write an interface class in Java that corresponds to c.
Re: How does JAVA adjust C(JNA)?
JNA views a DLL /.so file as a Java interface.
For example:
public interface TestDll1 extends Library {
TestDll1 INSTANCE = (TestDll1)Native.loadLibrary("TestDll1",TestDll1.class);
// There is a corresponding function in the library, and the prototype is void say(char *);
public void say(String str);
/*
Other functions in the interface, the most important thing is that the type and order of parameters in this interface should be strictly consistent with the parameters in the c interface
*/
}
A public static constant is required within the interface: instance.
//TestDll is the corresponding dynamic library
TestDll1 INSTANCE = (TestDll1)Native. LoadLibrary ("TestDll1", testdll1.class);
By INSTANCE, you can get an INSTANCE of this interface, thus using the methods of the interface, that is, functions that call external DLLS.
Note:
1. Native. LoadLibrary () function has two parameters:
The name of a DLL or.so file without a suffix. This is in line with the JNI specification because it is not possible to cross operating system platforms with a suffix.
The path to search DLL is:
1) the root path of the project
2) global path of the operating system,
3) path.
With this Class type, JNA dynamically creates an instance of the interface from the specified DLL /.so file.
2. In the interface, you only need to define the functions or public variables you need, and the ones you don't need can be undefined.
Public void say (String STR); // the type of the argument and the return value should be the same as that of the C function.
Char * is the argument to the C function. The corresponding Java type in JNA is Stirng.
Specific parameters can be found according to the corresponding demand.
For example:
public interface TestDll1 extends Library {
TestDll1 INSTANCE = (TestDll1)Native.loadLibrary("TestDll1",TestDll1.class);
// There is a corresponding function in the library, and the prototype is void say(char *);
public void say(String str);
/*
Other functions in the interface, the most important thing is that the type and order of parameters in this interface should be strictly consistent with the parameters in the c interface
*/
}
A public static constant is required within the interface: instance.
//TestDll is the corresponding dynamic library
TestDll1 INSTANCE = (TestDll1)Native. LoadLibrary ("TestDll1", testdll1.class);
By INSTANCE, you can get an INSTANCE of this interface, thus using the methods of the interface, that is, functions that call external DLLS.
Note:
1. Native. LoadLibrary () function has two parameters:
The name of a DLL or.so file without a suffix. This is in line with the JNI specification because it is not possible to cross operating system platforms with a suffix.
The path to search DLL is:
1) the root path of the project
2) global path of the operating system,
3) path.
With this Class type, JNA dynamically creates an instance of the interface from the specified DLL /.so file.
2. In the interface, you only need to define the functions or public variables you need, and the ones you don't need can be undefined.
Public void say (String STR); // the type of the argument and the return value should be the same as that of the C function.
Char * is the argument to the C function. The corresponding Java type in JNA is Stirng.
Specific parameters can be found according to the corresponding demand.
Re: How does JAVA adjust C(JNA)?
Experience summary:
In C, often use char * type for passing parameters, and often by the return value to obtain in Java, Java class only has spread the function of the parameters (note: String as the incoming parameters, only if you want to let it has function, the need to encapsulate a MyString class), as a result, when to and from parameters of the C (char *) match type, jna interface need to pass in a byte [] and the length of it.
Structure in JNA:
When a parameter in a C library is a struct type, the Java interface encapsulates a class that inherits from Structure
Such as:
Public static class Student extends Structure{
Public int the age;
Public int score;
// used when the C library parameter is a pointer to a structure
Public static class ByReference extends Student implements structure. ByReference {}
//C library parameters are used when struct
Public static class ByValue extends Student implements structure.byvalue {}
}
Note that the data types are still strictly corresponding to the data types in the C library.
For Pointers in the C library (int *... .. , as an outgoing parameter), there are already ready-made objects in the JNA that can be used directly
Usage:
Int length = 20;
IntByReference len = new IntByReference(length);
At the end of the call, get the outgoing value
Length = len. GetValue ();
Other xxByReference objects can be found on a case-by-case basis.
In C, often use char * type for passing parameters, and often by the return value to obtain in Java, Java class only has spread the function of the parameters (note: String as the incoming parameters, only if you want to let it has function, the need to encapsulate a MyString class), as a result, when to and from parameters of the C (char *) match type, jna interface need to pass in a byte [] and the length of it.
Structure in JNA:
When a parameter in a C library is a struct type, the Java interface encapsulates a class that inherits from Structure
Such as:
Public static class Student extends Structure{
Public int the age;
Public int score;
// used when the C library parameter is a pointer to a structure
Public static class ByReference extends Student implements structure. ByReference {}
//C library parameters are used when struct
Public static class ByValue extends Student implements structure.byvalue {}
}
Note that the data types are still strictly corresponding to the data types in the C library.
For Pointers in the C library (int *... .. , as an outgoing parameter), there are already ready-made objects in the JNA that can be used directly
Usage:
Int length = 20;
IntByReference len = new IntByReference(length);
At the end of the call, get the outgoing value
Length = len. GetValue ();
Other xxByReference objects can be found on a case-by-case basis.
Re: How does JAVA adjust C(JNA)?
This post should help JAVA development colleagues
Re: How does JAVA adjust C(JNA)?
Very nice, very good summary,That's great!!!
Re: How does JAVA adjust C(JNA)?
This approach to JAVA calling C is much simpler than JNI development. But there is a big performance cost, which is to say, jna is much slower than jni in terms of program performance. So while jni can be a pain in the neck to develop and process data type conversions, it can be used in situations where high performance is required.
Re: How does JAVA adjust C(JNA)?
If there are performance requirements, it appears that the choice is JNI.youyou wrote: ↑Mon Jan 14, 2019 3:13 amThis approach to JAVA calling C is much simpler than JNI development. But there is a big performance cost, which is to say, jna is much slower than jni in terms of program performance. So while jni can be a pain in the neck to develop and process data type conversions, it can be used in situations where high performance is required.
Re: How does JAVA adjust C(JNA)?
Thanksyouyou wrote: ↑Mon Jan 14, 2019 3:13 amThis approach to JAVA calling C is much simpler than JNI development. But there is a big performance cost, which is to say, jna is much slower than jni in terms of program performance. So while jni can be a pain in the neck to develop and process data type conversions, it can be used in situations where high performance is required.
Who is online
Users browsing this forum: No registered users and 5 guests