Skip to main content
gamedev.net
🔒 Locked

Java to C++ - Automatic Migration

Started by ResearchMonkey 11 replies 2.4k views
Original Post
ResearchMonkey
ResearchMonkey
I'm about to begin a project porting a Java (actually J2ME) project to C++. Does anyone know anything about tools to automatically migrate the code between the two languages? Obviously no tool is going to be able to do all of the work but it would certainly help get things going. Also, the original project is still under development and so having a tool would be a good way of tracking a moving codebase. I'd always assumed that tools to do this existed in some form but a couple of hours spent searching the web hasn't produced anything useful. Is this a good tree to be barking up? Thanks for any advice, ResearchMonkey
MaulingMonkey
MaulingMonkey
Not aware of any such tools, C++ isn't GCed so there's going to be a fair bit of difference in the code - unless you're planning on using Managed C++. Similarly, Java dosn't have pointers, so there's going to be a fair bit of difference in the code that way too.

There may be tools, but I don't know of any. I'd get ready to make ample use of the boost smart pointers library for your RAII needs (http://boost.org/)
Ilankt
Ilankt
I think it's preety impossible.
lets say you have a function in java that gets an object, how the program should know if you are passing it by value, by refrence, or by pointer?
just a small example of the diffrences.
How appropriate, you fight like a cow!
SKATIN_HARD
SKATIN_HARD
There might be some tools out there but I am unaware of such. If you know c++ you could just go line by line and change what is needed but I agree using a tool could save you time. Also the tools would probably cost some money because I don't think they would be free/shareware.
ResearchMonkey
ResearchMonkey
Thanks everyone for your replies, it sounds like this is going to have to be done the hard way =( It seems surprising that noone's tried to do this yet. There are some issues but nothing that seems completely insurmountable. This probably means that I haven't thought about it enough.

AP> Thanks for the links, shame that the project is still under development. The reasons for porting are commercial and we also need to have readable C++ source code otherwise a Java->Native compiler would have been a great way to go. Oh well.

Thanks again,
ResearchMonkey
Andrew Russell
Andrew Russell
Given a clever program that can convert all your objects to smart pointers, it shouldn't be all that difficult. Asside from that, it's a simple matter of syntax conversion and probably manual conversion for uses of Java's library.

One would think that it may be faster, for most projects, to convert by hand. Although it may be benificial to make a program that converts the syntax and then the user could convert library use and object pointers by hand. Of course, you'd have to be proficient in both languages.

Interesting Question: Can the GNU Java compiler compile to object files or static libraries that can then be used by the GNU C++ compiler?
stubble
stubble
If there isn't a program which will convert it all for you (I don't think it's as straightforward as it initially seems) you could sniff the java code in using a modelling tool like Enterprise architect and then generate it as C++. Obviously this isn't going to do anything with the actual logic but it will give you some classes with member and method declarations.
snk_kid
snk_kid
Quote:
Original post by Andrew Russell
Interesting Question: Can the GNU Java compiler compile to object files or static libraries that can then be used by the GNU C++ compiler?


Sure CNI

Andrew Russell
Andrew Russell
Quote:
Original post by snk_kid
Quote:
Original post by Andrew Russell
Interesting Question: Can the GNU Java compiler compile to object files or static libraries that can then be used by the GNU C++ compiler?

Sure CNI


Awesome, I'll have to remember that.

Then of course, this is probably the simplest and most effective method for ResearchMonkey's problem.
snk_kid
snk_kid
Quote:
Original post by Andrew Russell
Then of course, this is probably the simplest and most effective method for ResearchMonkey's problem.


Its quite simple to do, you can compile java directly to native, automatically generate binding C++ headers from java code etc, however bare in mind that (last time i checked) GCJ's imp of the the java class libraries isn't 100% complete (most of it but not all). Also CNI is GCC only (it can do JNI but its not as nice as CNI) .
ResearchMonkey
ResearchMonkey
Having done a bit more research today in conjunction with some colleagues we've discovered java2cpp from Programics (http://www.programics.com/java2cpp.php). I've been playing around with it for a few hours and it looks like it should fit the bill very well. It translates Java to pretty readable C++ and, on the simple examples I've thrown at it, performs very well.

Still plenty of work to do on this project but at least I have a jumping off point and several thousand less lines of unfinished Java code to worry about.

Cheers,
RM

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.