views:

162

answers:

5

Are there any programs out there that will convert Java code to C++?

A: 

There could be potentially.

But the styles of the two languages are so different that the resulting C++ code would look very none C++ like and as such would be hard to maintain.

The real question is why yuo are trying to do this?

Martin York
+1  A: 

I've used this utility with basic projects:

http://www.euclideanspace.com/software/language/xes/userGuide/convert/javaToCpp/index.htm

Eton B.
eton - funnily enough, this was one of the 1st google results on my search. what issues did you encounter using this (if any) on your test projects. and were there any libraries that didn't play nicely??
jim
I didn't do a whole lot with it since this came up as mere curiosity. The basic projects I mentioned were area calculating applications for different figures, at least the Math library (or the portions of it I required) work well, simply a matter of proper renaming as explained. However, as recommended by other users I'd go for the native code solution anytime. I don't see a lot of potential on the application I posted but it was the best example I could provide for the original question.
Eton B.
yes, i'm always wary of 'conversion' utilities unless i really understand what they do. the decisions end up affecting your entire ability to comprehend what's going on
jim
+1  A: 

erotsppa - i wasn't aware of any until browsing this question. we had researched some java to c# tools a few years back with varied success.

anyway, a google search (which i'm sure you've done) turned up a few interesting results:

http://www.euclideanspace.com/software/language/xes/userGuide/convert/javaToCpp/index.htm http://www.scicontrols.com/R2J.htm

jim

jim
+4  A: 

Java is a completely different language to C++. The code will probably have to be completely rewritten from scratch. Even if there is a Java to C++ compiler:

  • It wouldn't work on all Java code.
  • It would not write code that looks like it is written by a C++ programmer.
  • It would probably not use the ordinary C++ or STL types so even if it is valid C++ it wouldn't integrate well with any other code.

You can compile some Java code to native code. Maybe that would be a better approach for you.

Mark Byers
Actually, you can do this. At the very least, you can write a JVM in C++ and compile the Java code to JVM that gets embedded in the C++ program. Alternately, you could take a Java compiler and have a C++ back end, as well as a C++-written runtime. This would be useful only if you have a C++ compiler and no Java compiler, of course, since there's no way to do this anywhere near idiomatically.
David Thornley
actually, the native code compilation sounds like a good option, especially if your expertise lies in java and not some 'tools'' interpretation of that in c++
jim
A: 

There's the JC Virtual Machine which translates Java bytecode to C, which is compiled and run: http://jcvm.sourceforge.net/

If you just want to use a Java library in a C++ application (or vice versa), then you should consider gcj from the GNU compiler collection instead. It's a java->native code compiler. The C++ compiler has specific extensions to interoperate with the code compiled with gcj, which means you can basically use a module written in Java as if it was written in C++.

Luther Blissett