Our Online Store have the new products: RFID antenna board. Currently it can work with JC10M24R and JCOP4 card chips.
Compared with normal cards, the antenna board module has a smaller size and fixed holes, which is easy to integrate in the IOT(Internet Of Things) project.

How does JAVA adjust C(JNA)?

Other Tools
youyou
Posts: 55
Joined: Fri Jul 06, 2018 3:42 am
Points :386
Contact:

How does JAVA adjust C(JNA)?

Post by youyou » Sun Jan 13, 2019 11:54 pm

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.

youyou
Posts: 55
Joined: Fri Jul 06, 2018 3:42 am
Points :386
Contact:

Re: How does JAVA adjust C(JNA)?

Post by youyou » Sun Jan 13, 2019 11:57 pm

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.

youyou
Posts: 55
Joined: Fri Jul 06, 2018 3:42 am
Points :386
Contact:

Re: How does JAVA adjust C(JNA)?

Post by youyou » Sun Jan 13, 2019 11:58 pm

Experience summary: :D

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.

youyou
Posts: 55
Joined: Fri Jul 06, 2018 3:42 am
Points :386
Contact:

Re: How does JAVA adjust C(JNA)?

Post by youyou » Mon Jan 14, 2019 1:31 am

This post should help JAVA development colleagues :geek:

lanzi
Posts: 39
Joined: Fri Sep 28, 2018 3:41 am
Points :200
Contact:

Re: How does JAVA adjust C(JNA)?

Post by lanzi » Mon Jan 14, 2019 2:26 am

Very nice, very good summary,That's great!!!

youyou
Posts: 55
Joined: Fri Jul 06, 2018 3:42 am
Points :386
Contact:

Re: How does JAVA adjust C(JNA)?

Post by youyou » Mon Jan 14, 2019 3:13 am

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. :P

lanzi
Posts: 39
Joined: Fri Sep 28, 2018 3:41 am
Points :200
Contact:

Re: How does JAVA adjust C(JNA)?

Post by lanzi » Thu Jan 17, 2019 5:14 am

youyou wrote:
Mon Jan 14, 2019 3:13 am
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. :P
If there are performance requirements, it appears that the choice is JNI.

lanzi
Posts: 39
Joined: Fri Sep 28, 2018 3:41 am
Points :200
Contact:

Re: How does JAVA adjust C(JNA)?

Post by lanzi » Thu Jan 17, 2019 5:14 am

youyou wrote:
Mon Jan 14, 2019 3:13 am
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. :P
Thanks :)

youyou
Posts: 55
Joined: Fri Jul 06, 2018 3:42 am
Points :386
Contact:

Re: How does JAVA adjust C(JNA)?

Post by youyou » Thu Mar 21, 2019 5:28 am

Nice :)

Post Reply Previous topicNext topic

Who is online

Users browsing this forum: No registered users and 6 guests

JavaCard OS : Disclaimer