7

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

Do they work well?

Anderson Green
  • 30,230
  • 67
  • 195
  • 328
Martin Damrau
  • 87
  • 1
  • 1
  • 2
  • 1
    unless you have the entire standard library implemented, this isn't much of a Return on Investment. a basic google shows lots of results. –  Feb 02 '10 at 16:18
  • 1
    Why exactly do you want to convert from Java to C? If speed is an issue, you can always try compiling Java to native machine code using GCJ for example. – Steve Feb 02 '10 at 16:26
  • While the question is not exactly the same, it is close enough for the answers to be valid: http://stackoverflow.com/questions/1096615/automatic-java-to-c-conversion/1096648#1096648 – David Rodríguez - dribeas Feb 02 '10 at 16:28
  • OP actually wants to go from Java to VHDL (see comment on other answers). He doesn't exactly show a lot of interest; he hasn't been back to SO since he asked the question. – Ira Baxter Apr 02 '16 at 20:33
  • Java to VHDL XD hahahahahahaha god, would you honestly want a machine to do that?? Also that's typical for FPGA engineers. They are a bunch of total cowboys in my experience. – Owl Jun 10 '19 at 16:42

2 Answers2

6

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.

Community
  • 1
  • 1
BobMcGee
  • 19,824
  • 10
  • 45
  • 57
  • 1
    5 years ago this was an iffy idea, now it is a flat out *terrible* one. Your C is likely to be slower than the original Java (JRE 8 is about as smart as most C compilers), and as a bonus gcj only supports up to Java 4, with some Java 5 support. We are now on Java 8, only the most trivial implementations would even compile by GCJ now. – BobMcGee Apr 03 '16 at 02:51
  • 2
    Just how bad an idea? There was one microbenchmark I converted over for a simple test (parsing text and counting words, then sorting). Results: Java 8, 8.492s realtime, 13.850s CPU time. GCJ compiled binary: 28.191s real time, 27.850s CPU time. – BobMcGee Apr 03 '16 at 03:19
5

Can you explain why you want to port your Java code to c?

If it's for performance you likely won't see much of an improvement. Java is a garbage collected language and currently there isn't an algorithm that can insert memory allocation and deallocation calls efficiently. There have been many researchers trying to solve this problem and they have some interesting solutions but I have not seen a good commercial product that can scale to large programs yet. You can look at the conference proceeding for previous ISMM conferences for more information.

If you want to speed your code up I suggest that you use a profiler and find the hot methods. Try and optimize the hot methods and if that is still not enough try and use JNI.

Stephen Curial
  • 1,686
  • 15
  • 14
  • 1st we like to go from Java to C, then from C to VHDL (for implementation into either FPGA or ASIC). – Martin Damrau Feb 02 '10 at 16:33
  • 1
    @Martin: So you want to go from Java to VHDL, and the best path looks like it uses C? You might want to edit your question, or attract a new one, to see if anybody has a better way to start with Java and get VHDL. I am reminded of the mid-90s, when I used f2c to change a Fortran linear programming code to C so I could interface it with Macintosh Common Lisp. – David Thornley Feb 02 '10 at 16:38