GraalVM, Anyone managed to run Java and C/C++?

I want to use C/C++ functions from Java without JNI,
Graal seems to say that they can do it, but I don’t know how.

It can run C code fine, and I am sure it can run Java code.
I have seen countless examples of R and JS using Java and Java using JS.

If anyone could give a short example on Java calling C or the reveres that would be great.
Thanks!

First, I want to thank you for making this project visible for me. While keeping nose in own projects, one can easily miss some interesting things happening.

From what I gather (kind of studding Graal for 1h):

Problems the GraalVM tries to solve

  • JVM had some externally introduced support for other languages than Java for a long time, E.g. Scala, Groovy, Kotlin. But support for those languages was so far always as a secondary citizens. Behind it it was always Java object model and Java specific ByteCode. This Object model is not compatible with C++, it uses similar concepts like methods, classes, e.t.c. But there are huge differences in details how the memory works, how class is represented in memory regardless of memory management (that is separate subject on its own). Scala class compiled into ByteCode in reality is an very “ugly” Java class with many generated methods that are needed to implement all the sugar feature of the language.
  • The more alien to Java Object model the language was the more strange code is needed to be introduced to close the gap.
  • Common Language Runtime (CLR) that is behind Microsoft’s .NET and C#) does quite similar thing as Java VM. Different but same principles. Since Java was first, and MS was already supporting C, C++, VisualBasic, MS went for more language agnostic virtual execution runtime. So .Net platform supports many languages as first class citizens.

How they try to solve it:

  • GraalVM builds on Java VM.
  • It tries to go further than JVM/CLR and other language initiatives for JVM. Basically aggregating and improving on what was (e.g. LLVM).
  • I do not know how far JVM is already modified, but they mention that some things are already present in Java 10 as experimental feature.
  • They are building language agnostic runtime environment like JVM/CLR with support for many languages.
  • And compiler frameworks
  • ByteCode (java specific) -> BitCode (language agnostic)
  • They are making it open so you can always introduce your own language
  • And so far I see that it is open source.
  • There is still clear boundary of “Virtual Execution Runtime”. So whenever you see “cross language interoperability” it is highly focused within the same VM/runtime. As it is still managed runtime as it is for JVM and CLR.
  • So I expect that JNI (or equivalent) is still a way to go outside.

Here is some mention of something might to interest you:

1 Like

Thomas Wuerthinger answered on Twitter.
He is basically saying that Java and C in the same jitted application is a no go at the moment.

But, you can use SubstrateVM (SVM) to precompile your Java code together with C code, and then it will work.
It looks like a pain to use so I am leaning towards just letting my application stay on JNI.
JNI is still a hassle, but at least it isn’t alien to others.