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

后端 未结 7 1977
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-28 10:40

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 sta

相关标签:
7条回答
  • 2020-12-28 11:13

    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.

    0 讨论(0)
  • 2020-12-28 11:14

    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.

    0 讨论(0)
  • 2020-12-28 11:16

    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

    0 讨论(0)
  • 2020-12-28 11:21

    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. :)

    0 讨论(0)
  • 2020-12-28 11:25

    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.

    0 讨论(0)
  • 2020-12-28 11:28

    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.

    0 讨论(0)
提交回复
热议问题