Introduction
In this post, we are going to take a look at how to call a C method in Java by using the foreign function API. This API lets you make down calls to a C library in a safe way. To use the code from this tutorial you need to use Java 22 or later.
Calling a C method from Java
To call a C method you first need to create an Arena, this lets you create a continuous region in memory. You will need this to pass the parameters to the C method.
Creating an Arena is done like this:
|
|
I like to use them in a try-with-resource statement, so they are closed when they are done. Otherwise, you would have to call the close method yourself. There are 4 types of Arenas:
- ofConfined: This arena is alive till it is closed and can only be accessed by the thread that created it.
- ofShared: This arena is alive till the close method is called, the arena can be accessed by multiple threads.
- ofAuto: This arena's lifetime is managed by the garbage collector and can be accessed by multiple threads.
- global: MemorySegments created in this arena are never deallocated and the arena has an unbounded lifetime.
The next step is to create a linker and to make a methodHandle. The Linker does the heavy lifting when creating down calls, it knows the calling conventions of the platform to call the C methods. To create a MethodHandle with the linker we need to know the name of the C method and the method signature. In this post, we will make a downcall to the strcmp method. It has the following signature:
|
|
The strcmp method takes two String values as parameters and returns an int value. The int value represents which string is different:
- 0 : both String are equal
- >0 : If the first not matching char is found in the first string
- <0: If the first not matching char is found in the second string
Creating a MethodHandle is done as follows.
|
|
The first parameter of downcallHandle() uses the Linker to find the method inside standard loaded C libraries with the name “strcmp”. The second parameter is the FunctionDescriptor, this is the signature of the method we are looking for. The first value of the FunctionDescriptor is the return value, and the following values are the parameters.
The next step is to create two strings that can be passed to the strcmp method. To create a string in native memory that is safe to pass to
the C method we need to use arena.allocateFrom()
. The allocateFrom method is a handy method to create a memorySegment for String values.
You can use it like this:
|
|
This creates two MemorySegments that each store a string. These Strings don't live on the Java heap but in native memory, which C can safely access. The next step is to invoke the strcmp method handle which you do like this:
|
|
This makes the call to the C method and returns an int value. In this case, it is safe to cast it to an int because the strcmp returns an int value.
Complete code
Below you will find the complete code example used in this tutorial.
|
|
Using your own C library
If you want to use your library there is the change ‘linker.defaultLookup()’ won't be able to find the C method. In those cases, you need to load it yourself. You can do that using the system::loadLibrary method or by creating a SymbolLookup. Creating a SymbolLookup has the benefit that the library will be loaded as long as the arena it's connected to is active. This means that when the arena gets closed the library will be unloaded and free up resources.
You create a symbolLookup like so:
|
|
This loads the library and binds the lifetime to that of the arena.
The next thing you want to do is use that symbolLookup when creating a MethodHandle. When using the downcallHandle pass the symbolLookup as a parameter. The linker will now look inside the C library to find the method you want to call.
|
|
This is all you need to load a library that is not loaded by default, or not used by the linker.defaultLookup().
Conclusion
In this post, we looked at how to make a call down to a C library. This is done by using the foreign function and memory API. This is an alternative way of calling C methods instead of having to use JNI.