Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java to C cross compilation [closed]

Does anyone know of a good Java to C cross compiler?

Do they work well?

like image 421
Martin Damrau Avatar asked Feb 02 '10 16:02

Martin Damrau


People also ask

Does Java compile down to C?

very first Java compiler developed by Sun Microsystems was written in C, but now the class libraries are always written in Java (since they are intended to be run using the Java VM itself).

Is Java cross compiler?

Yes, it is possible for target to be greater than source. That is, we can write code using strictly java 8 syntax, and (using a java 11 or later compiler) compile it to java 11 bytecode.

What is cross compilation in Java?

A cross compiler is a compiler capable of creating executable code for a platform other than the one on which the compiler is running. For example, a cross compiler executes on machine X and produces machine code for machine Y.


1 Answers

This is very similar to this question, and answers may be helpful to you: Compiler to translate Java to C.

Summary: There are tools for this (Toba, GCJ, etc), but you may run into problems with not all of the Java libraries being ported. In the end, tools will probably only do PART of the work, and you'll have to hand-code some of the rest.

A good first step is to convert your Java code to only use standard libraries available in Java 1.4. In fact, you'll probably want to wean as much as possible off of anything not in java.lang.* or java.util.* packages in order to simplify the porting procedure.

Depending on the size of your codebase, it may actually be easier to rewrite the bulk directly rather than relying on tools. Java and C have a lot of syntax similarity, but the mismatch between C's straight procedural code, and Java's object oriented features could cause problems. Automated tools may generate virtually unmaintainable C code when trying to work around this, and there's always the possibility for subtle bugs.

2016 update: Don't do this, not now, not ever. The options that used to provide this have not been maintained (GCJ, for example), and it's arguably easier to find a developer fluent in java than C. Also, Java performance has continued to improve, and baseline implementations tend to have similar performance. Optimized C is still faster, but the edge gets smaller and smaller with every JRE version.

like image 168
BobMcGee Avatar answered Sep 28 '22 04:09

BobMcGee