Does a Java to C++ converter/tool exist? [closed]
Asked Answered
U

10

60

I always asked myself if it would be possible to make a Java to C++ converter.

Maybe a tool that converts the Java syntax to the C++ syntax?

I am aware that the languages differ, but simple things like loops where the semantics match 1 to 1.

Is there such a tool? Or is it possible to make one?

Uncritical answered 17/2, 2009 at 13:36 Comment(11)
It's a good question. I was always wondering: Why is it that we can automatically translate complex natural languages (translate.google.com/translate_t), but fail at automatically translating between the much simpler, restricted programming languages?Fortuna
One reason, of course, is that no mistakes are tolerated when translating between programming languages, while humans are more flexible and understand the translation anyway, even if it contains errors.Fortuna
Some attempts - ONE and TWO to make a C to Java converter.Clubbable
j2c provides (some) syntactic conversion from Java to C++Mineralize
@jacekSieka cool project, any wikis about limitations, bugs would +1 :)Orbit
@Orbit the issue tracker is empty so it must be perfect =)Mineralize
I'm wondering the same thing. A fundamental law of computers, defined as "REGEX", means there is one simple and finite solution, I'm surprised no one has done this yet!Wurth
These distinctions between C#, Java and C++ are painful for developers, especially since, in hindsight, what is feasible by one of these languages is also done by another.Squirrel
I reposted the question on the softwarerecs SE since it got closed here: Java to C++ converterChincapin
j2c has moved to github.com/arnetheduck/j2c .Antonomasia
Found a decent converter online - javainuse.com/java2cppLapel
S
29

It's possible to do anything given enough time, money and resources. Is it practical? Beyond trivial examples not really. Or rather it depends on what constitutes an acceptable error rate.

The real problem is that the idioms are different in Java to C++. Java to C# for example would actually be far easier (because the idioms are much more similar). The biggest of course is that C++ has destructors and manually managed memory. Java uses finally blocks for this kind of behaviour and has garbage collection.

Also Java has a common Object supertype. C++ doesn't.

The generics to templates would be nigh on impossible I would imagine.

Stingy answered 17/2, 2009 at 13:44 Comment(10)
Common Object is not a problem. The converter just supplies one and then any class without a base class just add the inheritance. Generics in Java is just syntactic sugar and the C++ templates could handle that no problem. Going the other way would be impossible though.Anvil
Overall though I agree the idioms just don't match. Writting C++ is not done in the same way.Anvil
Common object is still a problem. Collections are built around that (particularly with generics). The idea being that you can insert anything. Equating that sort of thing to STL containers is not exactly 1:1.Stingy
regarding GC, the converter could only use heap allocated objects and only reference them using something like boost::smart_ptr's. This handles generics at least to a reasonable degree, you just emit something like: std::vector<boost::smart_pointer<Object> > for a vector of "things".Gawen
C++ supports GC so that wouldn't be problematic. But the real problem isn't this simple stuff (no common object super type? The solution to that should be obvious) but trying to emulate the Java memory model in C++.. ugh. Also there's the whole JNI/native code which may be C but expects some rather specific API that must be supported by the JVM. Edit: Should've read the date, why do all those ancient questions appear in the last time? -.-Libertine
I think such tool is reasonable if it won't try to implement all java features through translation. I would avoid GC and reflection at least. Also I would translate into C instead. I think such tool would be practical, because people don't want to rewrite code (like games) on multiple embedded platforms for example.Opposable
No, I really don't like such answers like "it is not practical", etc. I understand them always so: "really good idea, but we are too stupid to deserve it".Ratib
GC is no problem, one could either use a GC soluttion for the whole memory or write/integrate a GC library. Besides, on translation you know the layout of each class so you could restrict the pointer scan, just an idea. Generics is also no problem because [a] they are very very weak template like constructs, so you just instantiate a class with said types etc. || A problem is the reflection (if it is used) and of course the java standard library (because it is huge)Dispose
Because it is a java -> c++ compiler, it is insignificant, what c++ can, while java not (i.e. destructors). What java has, but c++ hasn't, should be emulated by a c++ way (gc).Ratib
static code analysis tools are able to find the memory freeing issues in C++, so translating the object allocation and freeing of memory can be automated, and I would like to import a set of C/C++ headers into the tool, and code them using java as if they were in classpath, which would translate them into linkable C/C++ code. but its just one of my wishes :)Legion
I
18

The Firefox HTML5 parser is written in Java and converted to C++. But I think the converter used there is quite specific for this project. Interestingly, it turned out the resulting C++ parser was faster than the old parser written in C++.

I'm also writing a converter as part of the H2 database, under src/tools/org/h2/java. The idea is to allow converting a subset of the H2 database to C++, so this is also not a general purpose translater.

And there is the open source project J2C.

So there are ways to convert Java to C++. But don't expect the translator support all features, and don't expect the resulting code to be any faster than a good Java JVM.

Improvisator answered 23/6, 2012 at 15:56 Comment(4)
Do you have any blog article about H2 to C++ conversion or sth like this (it's interesting how this idea came into your mind and what problems you met)?Casie
I don't write much blog articles... I wrote the converter to check whether the C++ version would be faster than a JVM on a Raspberry Pi (as the JVMs available for this platform, Cacao and Zero VM, are not that fast and need a lot of memory). I found it is a few times faster and needs less memory. The C++ version is a bit slower on a 'normal' computer (as I have expected). The current state is: very very simple demo apps can be converted. I'm not currently working on the converter, but maybe I will continue in the future.Improvisator
Hi, can you add some 2020's update here? Perhaps some news for "compile" (help and automatize something in the converting process) from Scala to C++.Templin
I'm afraid I don't have an update... The comment was written in 2012, but feel free to update the answer yourself! Or propose an update if you can't edit.Improvisator
G
8

Is is possible, no question, but it won't be so simple. It would be a Java compiler which generates C++.

If you want to do that from scratch, it will be very hard, you have to do all the work javac and the JVM do for you (e.g. garbage collection).

Btw. Google has a Java to JavaScript compiler (included in GWT)

Gnaw answered 17/2, 2009 at 13:40 Comment(2)
Fortunately, there are already various solutions for GC, generics, etc. in C++ which could be easily re-used.Ratib
unity game engine has an IL (c#) to cpp converter. It shouldnt be too hard to do java bytecode to cppHamal
M
7

There is one, bit I am not sure if it actually works. Java to C++ Converter-Tangible Software Soulutions.

It is weird how there are c++ to java converters, but only 1 java to c++ converter.

Monody answered 3/2, 2012 at 2:5 Comment(4)
Meanwhile there are also some alternatives available on GitHub, i.e. with source (unlike the suggested solution).Hopehopeful
@Hopehopeful It would help a lot if you could mention them by name :)Ethnogeny
@Hopehopeful you're welcome to mention them on Java to C++ converter.Chincapin
@FranckDernoncourt I don't have anything to do with Java anymore, but I found it through the GitHub search and Google with site:github.com, so I am sure someone who is still interested in it can find it in a similar fashion. It's been 2.5 years since my comment, I don't remember the exact projects from back then, sorry.Hopehopeful
P
3

As said it would be tough to convert Java to C++ but we can have an applicaiton or tool that generates code in Java and equivalnet C++ code.

I know one applicaiton which generates code in C++/Java/C# given a model which has its own way to deifine it.

That tool belongs to CA and name is CA Plex. Search on www.ca.com

Packton answered 17/2, 2009 at 17:7 Comment(0)
R
2

http://www.tangiblesoftwaresolutions.com/Order/Order_Upgrade_Instant_CPlus_Java_Edition.htm

Depends on the domain of where the code will be used, from a learning perspective perhaps it might be interesting.

i just found this via a google as I remembered seeing one in Univeristy that created code based on uml.

Raddy answered 17/2, 2009 at 13:40 Comment(0)
G
2

There are programs out there that claim they can do this, but none have gained enough popularity to be frequently mentioned, so we'll leave them at "attempts". Making a converter would require a lot of AI built into your program. The difficulty is increased tenfold when swing is involved because GTK/wxWidgets/Qt/win32 API all differ greatly from swing. But it is possible. Not that the code quality will be great, and no guarantees your program won't crash due to separate memory handling methods, but it's possible.

Gibeonite answered 17/2, 2009 at 13:41 Comment(0)
Z
2

Something neat would be a tool , that translate java to "C++ using Java API" (like GNU GCJ CNI), one problem remain is to manage array.length (array not vector) ...

Zellazelle answered 17/3, 2010 at 17:50 Comment(0)
D
2

The main issue is that java is a language that is written and designed to talk to a VM. I suppose it would be possible, but all you would be left is a very poorly optimized application with a self translating layer doing what the VM already does. I mean, sure, it is possible, it still wouldn't be a solution for anything i could think of. If your looking to make your sluggish java app native, maybe your thinking too hard, just use an application like JET, its actually quite good, and will give you the benefits a native app would bring. Of course if the VM is already doing what the app is asking it to do just as well as native code could(it happens.. sometimes :P) it might change nothing.

Java to c#, tho, sounds more reasonable, as both the languages are written in similar ways, talking to a framework as such, but this would still leave code very much unoptimized as code written from scratch for a particular framework can not be bested.

Delitescent answered 12/5, 2011 at 13:31 Comment(0)
T
2

Java to C would actually be the easiest. Remember you need to convert the language, If you do that, the required libraries can be converted by your new compiler. In other words Swing and AWT should not be a big problem...

I would start by taking a good look at the Java Native Interface (JNI). The JNI is a part of java which allows it to be used with C and C++. The reason I would start here is that it becomes fairly obvious how parts of Java may be implemented in C. Once I had a grasp on basic structures, like how Java Objects can be mapped onto C structures (struct) and how pretty much everything in Java is an Object including arrays, I might peek at the Open JDK source code.

The actual converter would have to convert all the imported Java libraries (and their imported libraries and so on...) which means you would need the source code for everything. This conversion no small task since the Java libraries are large.

The process would be time consuming, but no AI should be required. However, I see no reason to perform a conversion like this. It looses the portability of Java and would not gain the efficiency of C (except that it would be compiled to native code, but it would be better to compile the machine code directly from the Java).

Tasman answered 7/1, 2012 at 22:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.