5

I googled a bit but could not find any good Java to C source code converter.

My question is:

  1. Is this possible ?

  2. Are there any reliable Java to C src converter you can think of which I can have a look at?

Remi Guan
  • 21,506
  • 17
  • 64
  • 87
Rookie
  • 5,179
  • 13
  • 41
  • 65
  • 8
    Java and C are too different. Mainly because of their respective memory models. I don't think this is possible to do realiably – salezica Apr 09 '13 at 21:21
  • Why do you want to do this, anyway? – Antimony Apr 09 '13 at 21:23
  • 6
    Yes, there's a Java to C source converter: a human programmer. (Reliability may be an issue, though.) – Louis Wasserman Apr 09 '13 at 21:24
  • 4
    @Louis, that's not very reliable – Antimony Apr 09 '13 at 21:24
  • What is the purpose? We may be able to help you with another solution if we knew the problem. – Mikkel Apr 09 '13 at 21:28
  • Thanks all.. Appreciated – Rookie Apr 09 '13 at 21:29
  • 4
    If you *really* want to compile Java to C, you might try compiling Java to machine code with GCJ, then disassembling the machine code, then (somehow?) converting the assembly code to C. – Nick Apr 09 '13 at 21:29
  • I am trying to understand McEleise cryptosystem provided by Flexiprovider... The code is in Java... and I need a C module for my task... Since, I had to do this in just 5 days, I was wondering if there are tools out there which can help me speed up the process of Java to C conversion since the algorithm implementation is pretty complex and huge.. So, this thought came to me to ask experienced programmers at SO if they know of anything like that – Rookie Apr 09 '13 at 21:32
  • Thanks Nick! That's an interesting approach! – Rookie Apr 09 '13 at 21:33
  • It has been done, back in the Java Dark Ages. But (thankfully) none of the implementations saw the light of day. – Hot Licks Apr 09 '13 at 21:34
  • 2
    (A lot depends on the style of code. If the code is enough "C-like" then you may be able to automate a conversion with an ad-hoc filter and some hand "polishing". If the coders went all-out Java, OTOH, and created all sorts of classes and parameterized types then you're probably SOL.) – Hot Licks Apr 09 '13 at 21:37
  • Thanks Hot Licks... ya thts wht I figured out too abt wht cud be done... but thanks for the reply! I wud probably have to do it all the 'human programmer way'... only if I had some more time :( – Rookie Apr 09 '13 at 21:44
  • @WhozCraig you kidding? Translating code properly is a really hard job, I would *not* want to be responsible for maintaining what would be left after this. – djechlin Apr 09 '13 at 21:45
  • @djechlin I was *totally* kidding. I don't believe in porting language-to-langauge at all. Each language has intrinsic fortes' and you lose that with direct ports. Porting features and algorithms adapted to the language-specific has much better success and maintainability. I completely concur with you. And I've seen houses that port via intern/crash-course-trained lackey, and the results are exactly what you'd expect: a mess. – WhozCraig Apr 09 '13 at 23:59
  • See http://stackoverflow.com/a/3460977/120163 – Ira Baxter Apr 13 '13 at 10:27

3 Answers3

1

This is possible, but extremely difficult - for starters, you would need to integrate a garbage collector with your C source. There are a few projects that attempt this, e.g. Toba, but they're unreliable and no longer maintained. Usually you'll find somebody attempting something like this in their Master's thesis, after which it is quickly abandoned.

If you're doing this to try to speed up your program, then don't - Java is already pretty fast compared to natively compiled code (although it tends to use quite a bit more memory), and your translated C code is not going to be able to take full advantage of the C language.

Zim-Zam O'Pootertoot
  • 17,888
  • 4
  • 41
  • 69
  • Downvoted because I don't think this adds much information. It's tru the OP asked "is this possible" but I think an answer that says "uh, it'd be really difficult" doesn't add value. – djechlin Apr 09 '13 at 21:36
  • 2
    Almost ANYTHING is possible. If he's asking if it's practical, then no, it's not practical. The fact that there's no industrial tool for Java-C cross-compilation should be proof enough of this. – Zim-Zam O'Pootertoot Apr 09 '13 at 21:37
  • Thats what I thought of since JAva is object oriented while C is not... but was curious though.. thanks all for all your inputs .. really apprecitated – Rookie Apr 09 '13 at 21:42
  • 2
    That's not really the issue, because you can approximate Java objects with C structs. The problem is that Java is written to execute on a virtual machine, but C executes on the bare metal. – Zim-Zam O'Pootertoot Apr 09 '13 at 21:43
  • Thanks Zim Zam... really appreciated your input.. – Rookie Apr 09 '13 at 21:46
0

Use Java2C: A translator from Java to C language especially for embedded and fast realtime applications, including a javalike runtime System in C.

http://sourceforge.net/projects/java2c/

Amir Saniyan
  • 13,014
  • 20
  • 92
  • 137
0

You can also try XMLVM, which does not convert java source to C, but Java bytecode (which I believe it is far more useful).

You can have a look here

Panayotis
  • 1,792
  • 23
  • 32
  • Looks pretty interresting, but from what I see, it converts only to Javascript, Objective-C or java bytecode, not to something like C (my use case would be to create vanilla windows executables which don't need a JRE to run) – Balmipour Dec 27 '19 at 14:16
  • It also converts to C code too, and even embed the whole JDK – Panayotis Dec 30 '19 at 09:39