Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 530
Rudiger Avatar asked Dec 21 '09 16:12

Rudiger


People also ask

Which compiler converts the Java bytecode into machine language code which then get executed by the JVM?

The JDK includes a tool, javac, that compiles from Java source code to a target of Java bytecodes. The bytecodes are packaged in class files (also defined by the JVM specification). To run an application, the JVM loads class files and executes bytecodes.

Does Java compiler convert source code to bytecode?

The Java compiler (javac) converts the source code into bytecode. Bytecode is a kind of average machine language. This bytecode file (. class file) can be run on any operating system by using the Java interpreter (java) for that platform.

What is JIT compiler in Java?

The Just-In-Time (JIT) compiler is a component of the Java™ Runtime Environment that improves the performance of Java applications at run time. Java programs consists of classes, which contain platform-neutral bytecodes that can be interpreted by a JVM on many different computer architectures.

Is JVM is a compiler or interpreter?

The JVM converts that code into machine code using the Java interpreter. The JVM uses the interpreter at runtime, after that it execute the code on the host machine. As the Java compiler compiles the source code into the Java bytecode.


3 Answers

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.

like image 143
Ivan Maidanski Avatar answered Sep 29 '22 07:09

Ivan Maidanski


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.

like image 25
akuhn Avatar answered Sep 29 '22 08:09

akuhn


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.

like image 32
BobMcGee Avatar answered Sep 29 '22 07:09

BobMcGee