1
  1. I am working on a C/C++ project.

  2. I found online some very valuable assets in form of Java libraries that I would like to integrate into my C/C++ project. These are big projects I wouldn't consider porting to C/C++.

  3. I read in a Stackoverflow post that Java code can be compiled to native code, thus one would be able to run a Java project without having a Java Virtual Machine in the middle. I found also that there is even a commercial tool for doing this called ExcelsiorJET thus making me believe even more that this could be a reliable thing.

Taking 1, 2 & 3 into consideration I wondered whether or not, one could take a Java library, compile it into a native library and then create a C or C++ wrapper on top of that library. This means that this way the Java Virtual Machine will not be needed anymore. This way I will get rid of the Java Virtual Machine layer. Could this be possible?

I searched online to see if someone had ever successfully created this kind of a pipeline, but couldn't get to a conclusion...

What I would like to ask is whether or not this pipeline could be possible, at least in theory if there are no online examples of people successfully creating it.

If this is possible could anyone point me in the right direction?

Jacob Krieg
  • 2,834
  • 15
  • 68
  • 140
  • If these libraries enforce in the API the usage of many classes then even with the repacking between C/C++ and Java is a burdon (even when automatically done). Ideally there should be separation between both languages, and probably some development in java is needed. It is development with one hand tied on the back. Feasible, though mixing things will be costly. – Joop Eggen Dec 17 '18 at 13:21
  • Have you tried JIT. It works by compiling bytecodes to native machine code at run time. Here is a Stack overflow answer that provides details on this tool. https://stackoverflow.com/a/95679/7548672 –  Dec 17 '18 at 14:14
  • There is a lot more material on the web for calling native code from Java (see JNA and JNI) than the other direction. You might be more successful structuring your application as a Java "driver" which coordinates the calls to your C code and the 3rd party Java you want to use. – dan.m was user2321368 Dec 17 '18 at 15:23
  • Here is an interesting description on calling Java from C++, via JNI (https://sweetcode.io/calling-java-code-from-cc-transparently/), which would eliminate the need for a Java "driver". (It still has a running JVM). – dan.m was user2321368 Dec 17 '18 at 15:42
  • @dan.mwasuser2321368 Thank you for your answer! Please correct me if I'm wrong but calling Java from C++ using JNI implies having the Java code run on a virtual machine. What I would like is to get rid of the virtual machine layer and have a C or C++ wrapper on top of a native library which was obtained from Java code. – Jacob Krieg Dec 18 '18 at 15:17
  • @JacobKrieg - Yes, that solution has the Java run on a virtual machine (JVM). I've never encountered Excelsior JET, but it isn't clear that the code it generates conforms to the method call convention that a C compiler expects (e.g. "extern 'C'") style, etc. – dan.m was user2321368 Dec 18 '18 at 15:50
  • @dan.mwasuser2321368 You're saying that in theory you could access the code Excelsior JET generates using the method call convention that a C compiler expects, is that right? – Jacob Krieg Dec 18 '18 at 16:02
  • @JacobKrieg - No. I'm saying that I have no idea if that is the case. – dan.m was user2321368 Dec 18 '18 at 18:54

1 Answers1

0

Have a look at JNI and the JVM startup sequence: it's all C/C++ coding at this level.

You can either compile java ahead of time (AOT) or you can call the JVM as if it was a library (this is how executable wrappers like Launch4j work).

One way to simplify Java/C/C++ interfacing is to use IDL. Once you have defined your IDL interface it can be used to generate Java/C/C++ code.

Perdi Estaquel
  • 819
  • 1
  • 6
  • 21