5

Before someone shoots me down, I am aware of the Java to binary compilers and I'm not after one of those.

I also know there will be no perfect tool that converts everything without any problems. I am aware that missing Java libraries are a major problem; however my source doesn't make use of many Java libraries except for stuff like String and prints. I only want to the tool to create the classes that the Java source references. In the case of the String stuff I am happy to fill in the gaps or fix at a later stage. I just want the tool to do the boring bits so I don't have to do the translation manually.

In the case of required classes etc, I will manually fix those at a later stage but would appreciate a pointer to something that at the very least gets enough of the boring stuff done.

Once again I want the source translated and not a compiler to produce a binary. Basically I want to take some Java stuff and convert it to C++ for later use in other projects.

EDIT ADDITIONAL NOTES

Sorry if I was not clear in my previous parts of this question. I know that Java is very much different from C++. I have some Java code which is mostly filled with processing arrays and bits and has almost no object creation. In a sense it is very self-contained and has few calls to other classes. These classes seem to be prime candidates for conversion; the other stuff will have to be rewritten but at the least some parts are leveraged.

Anderson Green
  • 30,230
  • 67
  • 195
  • 328
mP.
  • 18,002
  • 10
  • 71
  • 105
  • If you really want to access your java utilities from C++ then you could use JNI instead of porting them. – Michael Anderson Apr 12 '12 at 02:20
  • Possible answer from the duplicate: http://tangiblesoftwaresolutions.com/Product_Details/Java_to_CPlusPlus_Converter_Details.html - but tools comes with a lot of caveats, and is not free. – Michael Anderson Apr 12 '12 at 02:27
  • @jesse if im asking for a java -> c++ how can that be a duplicate of yor suggestion which goes the other way ? – mP. Apr 14 '12 at 03:32
  • @michael not quite, i wish to convert some java classes to c++ so i can reuse the logic they contain. The important part is SOME, i know it is not possible to convert *Everything*. – mP. Apr 14 '12 at 03:33
  • https://www.tangiblesoftwaresolutions.com/product_details/java_to_cplusplus_converter_details.html – Bahramdun Adil Apr 02 '18 at 05:42

1 Answers1

13

j2c is one - it will convert Java source code into its rough C++ equivalent.

Obviously there are many caveats that others have mentioned (garbage collection, synchronization, etc) but it will provide a good starting point (and it's open source at that so you can easily improve that point =).

Depending on the size of your code base, it might also make sense to roll your own - writing one is not all that hard once you have a Java AST to work with given that the Java syntax is mostly a subset of C++, and there are many libraries which will parse Java code for you (j2c uses the one that comes with Eclipse for example). Doing a conversion by hand is a lot more error-prone than writing a tool for it.

Matthias Ronge
  • 9,403
  • 7
  • 47
  • 63
Jacek Sieka
  • 603
  • 6
  • 13