From C++ to Java
May 22, 2008 8:50 AM   Subscribe

In which I attempt to swap to a more fashionable programming language, just as it becomes unfashionable: are there any good tools for translating C++ to Java?

The objective: I have a large project in C++ that it's time to bring into the 21st century. Nothing clever about it, no tricky constructs, no pre-processor magic, just a big commandline app. I'm under no illusions that anything will be able to magically convert my code into a fully working program, but it would be good to have something to grind out boilerplate for the bulk of it, so that when I do the port I have at least something to use as a start and a structure to build around.

The background: So why do I want to do the port? Multiple reasons -

1) This is the first stage in converting it to an app with a fully blown GUI. Sure, I could use go for one of the GUI libraries for C++ but see the next point.

2) I've ended up supplying this application (an evolutionary analysis package) it on multiple platforms, which has turned into a bit of a nightmare. My users largely can't use make files, so it really has to be pre-compiled before it gets to them. This has turned into a lot of maintenance effort, especially in the case of Windows which I don't use often at all. Fix a bug, build app on the Mac. Copy code to Linux box and build app there. Copy app back to Mac. Copy code to Windows box and build app there. Copy app back to Mac. Upload all three files. Get a new bug report ... It'd be nice to do one build and distribute just that.

3) There are some particular Java libraries for visualisation and output that I'm eager to use.

4) Having projects scattered across several languages (C, C++, Python, RealBasic, Perl, Java, Obj-C and more), I've decided to let some of the languages go fallow and shift what I can to Java and Python, due to the mental space they occupy and the strain of staying competent and up-to-date with multiple languages. And having to solve the same problem in multiple languages is all too common an occurence ("Hey, I could do this with that library I wrote in Python. Pity this program's in Realbasic ...")

It could be argued that the effort in porting isn't worth the benefits. Indeed, that's what I have argued for the last few years but in my judgement that time has passed. Dragging the software across to Java will be a pain in the short term, but worth it in the long term.

Results so far:

* C2J: this gets mentioned a lot, but it's a bit flaky and works a file at a time.
* Mocha and Cappucino: two academic projects that seem to be no longer available.
* Jazillion: a commercial project with a good reputation and a "contact us for a quote!" price.

Any other ideas? I stress again that I don't expect any magic bullet, but any rough conversion would be a help.
posted by outlier to Computers & Internet (7 answers total) 2 users marked this as a favorite
 
I find it hard to believe a rewrite (in Java, sure, that's fine) wouldn't be desirable anyway. Couldn't you rewrite the thing into much, much better shape now that you're older and wiser?

I've been involved in porting projects. Rewrites are always so much better.
posted by rokusan at 8:58 AM on May 22, 2008


If you don't need to run on multiple platforms, you might look at the structure of your C++, start by porting the GUI to Java, and make native calls to the C++ "guts" (logic) as you port over more of the logic. This might make your migration plans more manageable.
posted by Blazecock Pileon at 9:12 AM on May 22, 2008


Rewrite. Cut and paste as needed.
posted by jeffamaphone at 10:06 AM on May 22, 2008


I've never used it and I'd never advocate using such a tool in the real world but have you looked at nestedvm? Ephedra might be worth taking a look at.
posted by rdr at 10:52 AM on May 22, 2008


Another possible technique, if you're committed to Python, would be to try to build a sensible, OO-based Python API on top of your core C++ application code (which would maybe be a layer of thin high-level Python service objects on top of a middleware of low-level Python code that calls your C++ directly using ctypes). Once you have a high-level interface like that in place, you can start rewriting the core bits in Java, and replacing your ctypes middleware with straight jython->java calls. I guess you'd still be stuck with rewriting the entire thing at once, though, since you can't call ctypes from jython.

You don't mention much about how your code is currently structured. If it's mostly procedural, C-style code with lots of function parameters and some C++ embellishments on top you'll probably have a more difficult time getting into idiomatic Java, in which case my own impulse would be just to rewrite it. If it's already very OO-oriented code you might have better luck with machine porting tools. I'm personally kind of skeptical about how useful they would be even if they worked extremely well, though.
posted by whir at 1:49 PM on May 22, 2008


If it turns out to be too much of a pain, you can look at using wxWidgets for the GUI and cross compiling your C++ with mingw (wxDev-C++ provides a decent gui for this, even).
posted by IronLizard at 8:14 PM on May 22, 2008


Code::Blocks also has multiple target support, IIRC.
posted by IronLizard at 8:23 PM on May 22, 2008


« Older ProgrammingFilter (ASP.net): Help me before I jump...   |   I'm looking for a hard-headed camera. Newer »
This thread is closed to new comments.