Sign in to follow this  
ResearchMonkey

Java to C++ - Automatic Migration

Recommended Posts

ResearchMonkey    122
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

Share this post


Link to post
Share on other sites
MaulingMonkey    1730
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/)

Share this post


Link to post
Share on other sites
Ilankt    229
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.

Share this post


Link to post
Share on other sites
SKATIN_HARD    175
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
What you actually need is a compiler. It's _entirely_ possible. However, the resulting code probably is not going to be very nice.. especially because of things like a lacking garbage collector :)

A preliminary search turned up this:
http://www.edg.com/jfe.html
and http://www.dinkumware.com/jproject.html

However, any tools you may encounter through either of these sites, are probably going to cost "big bucks."

You may also want to check out GNU's java compiler (which can compile to native code, yay!).. perhaps there's a backend for it to generate C/C++ code... shrug.

However, that brings up an interesting question. _Why_ are you porting the application in the first place? Is it purely for speed concerns? Memory footprint? Perhaps just using GNU's native code java compiler may provide the same end results, without the change of language. Just a thought.



Share this post


Link to post
Share on other sites
ResearchMonkey    122
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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
@Ilankt actually, java transforms the notion of object to suggest pointers. Every object in java is actually a pointer(or may be referred as a pointer) in C(C++ any?).Of course you will need J2me hierarchy implemented in C(let's call it a library) and an underlying framework to handle gc(to say at least, there may be other things as well, I am not fully aware)+ the tool to transform(actually it easy to transform(you can write a program in a few hours), just define a refactorizing technique.But beware the pointers!!

Share this post


Link to post
Share on other sites
Andrew Russell    1394
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?

Share this post


Link to post
Share on other sites
stubble    158
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.

Share this post


Link to post
Share on other sites
snk_kid    1312
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

Share this post


Link to post
Share on other sites
Andrew Russell    1394
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.

Share this post


Link to post
Share on other sites
snk_kid    1312
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) .

Share this post


Link to post
Share on other sites
ResearchMonkey    122
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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this