views:

5230

answers:

7

Hi
I always asked myself if it would be possible to make a Java2C++ 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 semantic matches 1 on 1 ..
is there such a tool . or is it possible to make one ?

A: 

http://tangiblesoftwaresolutions.com/Product_Details/Instant_CPlusPlus_Java_Edition_Details.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 htat created code based on uml.

Paul Whelan
+1  A: 

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)

Johannes Weiß
A: 

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.

John T
+5  A: 

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.

cletus
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.
Martin York
Overall though I agree the idioms just don't match. Writting C++ is not done in the same way.
Martin York
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.
cletus
+2  A: 

I'm guessing the tool would be you.. :) (Not implying you're a tool in the derogatory sense)

Ryan Emerle
A: 

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

A: 

Something neat would be a tool , that translate java to "C++ using Java API" (like GNU GCJ CNI), on problem remain it to manage array.lenght

Regards

http://rzr.online.fr/q/gcj

rzr