Java to C/C++ exchange data objects #3634
-
Hello, I am trying to make it work to call C/C++ from java by passing data. I am using GraalVM 21.2.0 and using the docker image with llvm toolchain installed. Here is my simple C program
This file is compiled using Next, here is the Java code that uses it
I tried two approached to pass data from Java to C
Unfortunately both end up with the same exception
Any ideas how to resolve this? Or other hints how to pass data from Java to C (and back) that are of more complex types? Thanks |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
The problem is that foreign objects are always interpreted as pointers in C (that's the closest thing to a Java "reference" we have). You can't pass a Java object to a function that expects a by-value struct. The runtime would have to copy it to replicate by-value semantics. That is currently not implemented (and it probably also wouldn't be what you want, the resulting copy wouldn't be a Java object anymore). Modifying the |
Beta Was this translation helpful? Give feedback.
-
@rschatz one more question related to this - how should I pass string values from Java to C program? Extending the program in following way
and java part
fails with following exception
|
Beta Was this translation helpful? Give feedback.
The problem is that foreign objects are always interpreted as pointers in C (that's the closest thing to a Java "reference" we have). You can't pass a Java object to a function that expects a by-value struct. The runtime would have to copy it to replicate by-value semantics. That is currently not implemented (and it probably also wouldn't be what you want, the resulting copy wouldn't be a Java object anymore).
Modifying the
printHello
function to take a pointer as argument should fix it.