When compiling C++ you of course use a compiler for the target platform you want to compile for. Is there a C++ compiler that targets the JVM (so instead of using the Java "native" interface C++ code is compiled to Java byte code)?
Asked
Active
Viewed 2.7k times
48
-
1http://stackoverflow.com/questions/421951/running-interpreting-c-on-top-of-the-jvm – chrisaycock Nov 19 '10 at 02:38
-
http://en.wikipedia.org/wiki/Java_Virtual_Machine#C_to_bytecode_compilers – chrisaycock Nov 19 '10 at 02:39
-
Are you asking this question because you have some C++ library you want to use from Java and you want to avoid using JNI? If so, keep in mind that if your C++ code uses platform specific libraries, you aren't going to be able to call those without using JNI... – Laurence Gonsalves Nov 19 '10 at 02:42
-
8@bwawok: Which JVM JIT compiler outputs C++? – Fred Nurk Nov 19 '10 at 03:03
-
@bwawok What do you mean JIT compiler compiles byte code to C++? – Koray Tugay Jan 21 '15 at 12:55
-
1@KorayTugay I have no idea I wrote the comment 5 years ago, might want to look at a more recent post – bwawok Jan 22 '15 at 13:57
2 Answers
36
NestedVM provides binary translation for Java Bytecode. This is done by having GCC compile to a MIPS binary which is then translated to a Java class file. Hence any application written in C, C++, Fortran, or any other language supported by GCC can be run in 100% pure Java with no source changes.

ephemient
- 198,619
- 38
- 280
- 391
-
1
-
4
-
I actually found this independently (I remembered something about GCC MIPS, and so I Googled "GCC MIPS JVM"). But in retrospect it's not surprising that it's already been in an answer on SO. – ephemient Nov 19 '10 at 04:14
-
3Really, the reason I was interested is that Mathematica 8 can now generate C from Mathematica code. I was interested in running code made in Mathematica inside of a Java program without using MathLink or some other technology that would require the end user to install extra software. Really, this whole idea is only conceptual for me. Source: http://awurl.com/FTlvm62ns – knpwrs Nov 19 '10 at 04:15
-
2@TheLQ: `Why on earth would you want to run a C or C++ program in the Java VM?`. Well, for C++, having fully featured RAII on a JVM would be a good thing. And operator overloading. And, well, lambdas... :-D – paercebal Dec 16 '12 at 22:36
-
And simply because Standard C++ code is portable on a host of platforms, including the CLR. (I'm not talking about libraries) The JVM seems to be Terra Incognita on that matter. @chrisaycock: I'm not the OP but yes, I'm envious of C++/CLI :) – Laurent LA RIZZA Jul 03 '13 at 11:26
-
@ TheLQ the simple answer is that any working C or C++ code can be used on the JVM. Porting, rewrites, etc take time and money. A compiler with some tests is a better option in many cases. I could see it being useful with some legacy code issues too. – Nick P Jul 31 '13 at 20:28
-
@TheLQ: "Why on earth would you want to run a C or C++ program in the Java VM?": For you I recommend think about "if all you have is a hammer, everything looks like a nail"; The answer is same reason why C/C++ is implemented for CLI,,,,, – Abdurrahim Dec 25 '16 at 14:21
13
As I have answerd on this similar SO question there is a new kid in town called gcc-bridge. It's a maven plugin and supports all the gnu laguages like c, c++, fortran.