29

Is there a way to run plain c code on top of the JVM?
Not connect via JNI, running, like you can run ruby code via JRuby, or javascript via Rhino.

If there is no current solution, what would you recommend I should do?
Obviously I want to use as many partials solutions as I can to make it happen.

ANTLR seems like a good place to start, having a full "ANSI C" grammar implementation...
should I build a "toy" VM over the JVM using ANTLR generated code?

Ferruccio
  • 98,941
  • 38
  • 226
  • 299
amitkaz
  • 2,732
  • 1
  • 20
  • 18
  • This is interesting.. trying to make some *portable* c code? – Carlos Rendon Jan 07 '09 at 20:41
  • 2
    Trying to interact different codes (from different languages) over a common ground (the JVM) – amitkaz Jan 07 '09 at 21:05
  • It's been a long time since I looked at JVM specs, but IIRC it tried to prevent getting too fast and loose with data type conversions. If this is still the case, there will be C programs you simply can't run on the JVM. – David Thornley Jan 07 '09 at 22:10
  • 1
    Not really. The compiler could insert invisible data conversions for you. – Zan Lynx Jan 07 '09 at 22:57

4 Answers4

14

Updated 2012-01-26: According to this page on the company's site the product has been bought out and is no longer available.

Yes.

Here's a commercial C compiler that produces JVM bytecode.

joel.neely
  • 30,725
  • 9
  • 56
  • 64
12

There are two other possibilities, both open-source:

JPC emulates an entire x86 pc within the JVM, and is capable of running both DOS and Linux.

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.

KIC
  • 5,887
  • 7
  • 58
  • 98
rasjidw
  • 121
  • 1
  • 3
  • 1
    As of 4/2013 JPC and NestedVM are valid soultions at the cost of speed (as they are emulated within the JavaVM). The commercial solution axiomsol.com has been sold off without further information available on its website. – michaelt Apr 06 '13 at 23:08
  • about JPC, it is neat but how could one interact with the x86 code? – KIC Jun 13 '16 at 12:45
6

It seems that LLJVM can also meet your requirement.

LLJVM: Source code is first compiled to LLVM intermediate representation (IR) by a frontend such as llvm-gcc or clang. LLVM IR is then translated to Jasmin assembly code, linked against other Java classes, and then assembled to JVM bytecode.

lllllllllllll
  • 8,519
  • 9
  • 45
  • 80
6

As of 2016 there is a young but promising option called gcc-bridge. Its intend is to leverage the JVM's implementation of R. The goal is to use R-libraries written in C or Fortran. But gcc-bridge can be used independently as a regular maven plugin. Also see the gcc-brigde-example.

KIC
  • 5,887
  • 7
  • 58
  • 98