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

北城以北 提交于 2019-12-18 11:13:52

问题


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?


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


  • https://github.com/badlogic/jack -- Java to C++ transpiler, ignores memory model and other stuff, uses Boehm GC for extra slowness and GC pauses

    The license is unclear to me.

  • http://ptolemy.eecs.berkeley.edu/publications/papers/03/java-2-C/ -- A Retargetable Optimizing Java-to-C Compiler for Embedded Systems

    A paper, not sure whether the program is available.

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




回答5:


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.




回答6:


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




回答7:


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



来源:https://stackoverflow.com/questions/1941244/compiler-to-translate-java-bytecode-to-platform-independent-c-code-before-runtim

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!