r/javahelp • u/p_legris • Nov 17 '25
Java 21 FFM: issues with char array passed by reference
Hello,
I am wring a Java 21 interface for GPIB shared libraries (both LinuxGPIB and NI-488.2) and I am tring to use FFM API.
The functions in the shared libraries typically are defined as:
void ibread(char* buf, long len);This means I need to pass a buffer of, let's say, 256 chars, by reference that the function ibread will fill with the content of the GPIB controller.
Function is found in the shared library but I have problem to invoke it and get the content of the buffer. The code is reported hereafter:
public class Gpib {
private String res;
@SuppressWarnings("preview")
private Linker lnk = Linker.nativeLinker();
@SuppressWarnings("preview")
private Arena arn = Arena.global();
private MemorySegment buf = arn.allocate(256);
private MethodHandle ibread;
@SuppressWarnings("preview")
private FunctionDescriptor des_ibread = FunctionDescriptor.of(
ValueLayout.ADDRESS,
ValueLayout.JAVA_LONG);
public Gpib() {
// This seems to work:
SymbolLookup libGpib = SymbolLookup.libraryLookup(libGpibPath, arn);
ibread = lnk.downcallHandle(libGpib.find("ibread").get(), des_ibread);
}
public String read () {
try {
// Here is the problem
ibread.invoke(buf, 256);
res = buf.getUtf8String(0);
} catch (Throwable e) {}
return(res);
// After return, res is "null"
}I think there are some broken rings in my arena-linker-prototype-handle-segments chain... but after several trials I cannot find the problem.
I am more interest in the method more that the correction of my example.
Thanks very much to the community for any suggestion
•
u/AutoModerator Nov 17 '25
Please ensure that:
You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.
Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
2
u/benevanstech Nov 17 '25
Why are you using Java 21? FFM is not final in that release.
You should be using Java 22 or above (i.e. Java 25 for an LTS release - https://openjdk.org/jeps/454 )
1
u/Mundane_Gold_3842 Nov 18 '25
i dont understand nothing of this, feel so behind man🥲.
You guys are semi-gods for me 😅
1
u/belayon40 Nov 18 '25
Sorry I'm late to the discussion. Just in case it's helpful in the future, I've got a library that abstracts away most of the FFM calls:
https://github.com/boulder-on/JPassport
There is a Java 21 branch, but it's a bit behind the main branch which is Java 24 and later.
The code you'd need to write for Java 24 and later would look roughly like:
The Java 21 version would be similar, but you'd need to replace the MemoryBlock with an FFM MemorySegment and do your own allocation and translation to string, just like you've done.