Rudiger
Rudiger

Reputation: 6834

Compiler to translate Java bytecode to platform-independent C code before runtime?

I'm looking for a compiler to translate Java bytecode to platform-independent C code before runtime (Ahead-of-Time compilation).

I should then be able to use a standard C compiler to compile the C code into an executable for the target platform. I understand this approach is suitable only for certain Java applications that are modified infrequently.

So what Java-to-C compilers are available?

Upvotes: 6

Views: 9997

Answers (7)

(I've been googling for this stuff, this is how I came to this question at SO.)

Upvotes: 2

Ivan Maidanski
Ivan Maidanski

Reputation: 276

I could suggest a tool called JCGO which is a Java source to C translator. If you need to convert bytecode then you can decompile the class files by some tool (e.g., JadRetro+Jad) and pass the source files to JCGO. The tool translates all the classes of your java program at once and produces C files (one .c and .h for each class), which could, further, be compiled (by third-party tools) into highly-optimized native code for the target platform. Java generics is not supported yet. AWT/Swing and SWT are supported.

Upvotes: 8

Rudiger
Rudiger

Reputation: 6834

Not really an answer to my own question, but how does Oracle do it?

http://download.oracle.com/docs/cd/B28359_01/java.111/b31225/chone.htm#BABCIHGA

Upvotes: 1

BobMcGee
BobMcGee

Reputation: 20110

GCJ has this capability, but it hasn't got great support for Java features past 1.4, and Swing support is likely to be troublesome. In practice though, the HotSpot JIT compiler beats all the ahead-of-time compilers for Java. See benchmarks from Excelsior JET. To clarify: GCJ converts java source/bytecode to natively compiled code

Toba will convert (old) Java bytecode to C source. However, it hasn't been updated since Java 1.1. It may be helpful to partially facilitate the porting, but it just can't handle all the complex libraries Java has.

Upvotes: 3

Dmitry Leskov
Dmitry Leskov

Reputation: 3155

There used to be a product called TowerJ, which was essentially a "via C" static compiler for Java, but it is long gone.

I was told that Sun Labs has created something like this as part of the Sun SPOT project, but I am not sure if it is public.

@BobMcGee: In the benchmarks you refer to, GCJ indeed loses, but Excelsior JET (which is a 32-bit AOT compiler) beats the 32-bit HotSpot on all three test systems, so I am not sure what was your point.

But, after all, there are lies, damn lies, and benchmarks. :)

Upvotes: 0

akuhn
akuhn

Reputation: 27793

Why do that? The Java virtual machine includes a runtime Java-to-assembly compiler.

Compilation at runtime can yield better performance, since all information about runtime values is available. While ahead-of-time compilation has to take assumptions about runtime values and thus may emits less fast code. Please refer to Java vs C performance by Cliff Click for more details.

Upvotes: 3

Aaron Digulla
Aaron Digulla

Reputation: 328536

AFAIK, there is no such product but you have two options:

  • Implement your own byte-code to C transpiler. Byte-code is pretty simple, this isn't too hard.

  • If you just want a native binary (i.e. when you don't need the C source code), then give GCJ a try.

Note: If you're doing this for performance reasons, then you're going to be disappointed. Java is generally as fast as C/C++. Moreover, improvements to the VM will make all Java code faster but not your native binary. Compiling the code will just give you a little better startup time.

Upvotes: 1

Related Questions