Is there a tool out there that can automatically convert Python to Java? Can Jython do this?
-
3Victor, I thought you loved Python *way* too much to go converting it to Java..? ;-) – Galwegian Sep 30 '08 at 15:15
-
2:) I love Python, but I kinda have no choice there... – Victor Noagbodji Sep 30 '08 at 16:45
4 Answers
Actually, this may or may not be much help but you could write a script which created a Java class for each Python class, including method stubs, placing the Python implementation of the method inside the Javadoc
In fact, this is probably pretty easy to knock up in Python.
I worked for a company which undertook a port to Java of a huge Smalltalk (similar-ish to Python) system and this is exactly what they did. Filling in the methods was manual but invaluable, because it got you to really think about what was going on. I doubt that a brute-force method would result in nice code.
Here's another possibility: can you convert your Python to Jython more easily? Jython is just Python for the JVM. It may be possible to use a Java decompiler (e.g. JAD) to then convert the bytecode back into Java code (or you may just wish to run on a JVM). I'm not sure about this however, perhaps someone else would have a better idea.

- 133,303
- 56
- 317
- 449
-
1The JAD solution sounds like a winner. Perhaps you could edit your answer to just show this solution. – Peter Kelley Sep 30 '08 at 22:52
-
1I'm not clear that the JAD solution is actually viable as I don't know whether Jython classes are converted into .class files or whether they are interpreted by converting them to bytecode at runtime. It was a suggestion of something to look further into – oxbow_lakes Oct 01 '08 at 11:12
It may not be an easy problem. Determining how to map classes defined in Python into types in Java will be a big challange because of differences in each of type binding time. (duck typing vs. compile time binding).

- 104
- 2
-
And all those special method names. __call__ being something that will require real care to manage. – S.Lott Oct 01 '08 at 02:07
-
2There is a translator called [P2J](https://github.com/chrishumphreys/p2j) that can convert a subset of Python into Java. My [universal-transpiler](https://github.com/jarble/transpiler) project was written for the same purpose, but it also translates Python into C#, Java, JavaScript, and several other languages. – Anderson Green Sep 17 '16 at 22:20
-
@AndersonGreen I want to try your tool to convert python to java. I do not see an example in your readme. Do you mind throwing some light on that? – Karthik Jan 28 '19 at 10:02
-
1@AndersonGreen I tried your online convertor but looks like its failing when classes are declared in python. – Karthik Jan 28 '19 at 10:12
-
@Karthik, Yes, it's only compatible with a small 'purely-functional" subset of Python. – Anderson Green Jan 29 '19 at 02:18
Yes Jython does this, but it may or may not be what you want

- 1,274
- 1
- 16
- 32
-
7Indeed, but more precisely, `jython` creates JVM-bytecode from Python source code, _not_ Java source code – Tobias Kienzler Feb 17 '15 at 09:11
to clarify your question:
From Python Source code to Java source code? (I don't think so)
.. or from Python source code to Java Bytecode? (Jython does this under the hood)

- 59,062
- 28
- 129
- 143
-
4Yes it does convert to java source. from their page ( http://www.jython.org/archive/21/docs/jythonc.html ) : The jythonc tool generates actual Java source code, and then invokes a Java compiler over this source code to create the .class files – KIC Nov 26 '13 at 10:43