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.

Answer from oxbow_lakes on Stack Overflow
🌐
GitHub
github.com › chrishumphreys › p2j
GitHub - chrishumphreys/p2j: Python to Java translator · GitHub
Python to Java translator. Contribute to chrishumphreys/p2j development by creating an account on GitHub.
Starred by 120 users
Forked by 58 users
Languages   Python
🌐
GitHub
github.com › JanX2 › p2j
GitHub - JanX2/p2j: Python to Java translator
Python to Java translator. Contribute to JanX2/p2j development by creating an account on GitHub.
Author   JanX2
🌐
GitHub
github.com › sdziallas › softsys-p2j
GitHub - sdziallas/softsys-p2j: Python to Java Translator for Olin's Software Systems Course
Description: Python to Java Translator for Olin College's Software Systems course.
Author   sdziallas
🌐
GitHub
github.com › chrishumphreys › p2j › blob › master › README.md
p2j/README.md at master · chrishumphreys/p2j
Python to Java translator. Contribute to chrishumphreys/p2j development by creating an account on GitHub.
Author   chrishumphreys
🌐
Goucher
phoenix.goucher.edu › ~kelliher › f2019 › cs205 › lab0P2J.pdf pdf
Lab 0 P2J: From Python to Java
Hello! I'm an associate professor of Mathematics and Computer Science in the Center for Data, Mathematical, and Computational Sciences at Goucher College in the big city of Baltimore, MD. Before arriving here, I taught at Westminster College in the small town of New Wilmington, PA.
🌐
GitHub
github.com › chrishumphreys › p2j › commit › 9c1640925b0b7c0f4a50151f55b4c62d66b1fc07
Initial import · chrishumphreys/p2j@9c16409
April 7, 2011 - Python to Java translator. Contribute to chrishumphreys/p2j development by creating an account on GitHub.
Author   chrishumphreys
🌐
PyPI
pypi.org › project › p2j
p2j · PyPI
Here's another example of a Python source code and its Jupyter notebook after converting. The purpose of this package is to be able to run a code on Jupyter notebook without having to copy each paragraph of the code into every cell. It's also useful if we want to run our code in Google Colab. This parser isn't perfect, but you would be satisfactorily pleased with what you get. ... Specify the target filename with a -t. p2j <(curl https://raw.githubusercontent.com/keras-team/keras/master/examples/mnist_cnn.py) -t myfile.ipynb
      » pip install p2j
    
Published   Nov 17, 2019
Version   1.3.2
🌐
DaniWeb
daniweb.com › programming › software-development › threads › 507349 › p2j
p2j - python - DaniWeb
January 28, 2017 - I need to convert python code to java, can anyone help me - Topic in the Software Development forum contributed by Arpita_1
Find elsewhere
🌐
Python
mail.python.org › pipermail › pydotorg-www › 2016-April › 003528.html
[pydotorg-www] Converting Python code to java code/bytecode
April 30, 2016 - They have a new page on github: ... > 1) http://pybee.org/voc/ - Converts python to java byetcode > 2) https://www.py4j.org/index.html - Allows python code to access jvm > objects. > 3) https://github.com/chrishumphreys/p2j - An old python to java > translator....
🌐
Quora
quora.com › What-is-the-best-way-to-convert-Python-to-Java-assuming-that-performance-speed-and-code-readability-are-not-an-issue
What is the best way to convert Python to Java, assuming that performance, speed, and code readability are not an issue? - Quora
Answer (1 of 12): The fastest I have seen is by using a Tokenizer I will explain. #A Python Script… Delimiters in Python are how it is compiled to and for C to execute. We do this all the time with Java also. Computer languages are very similar and use Tokenisers just as you can see here below ...
🌐
YouTube
youtube.com › watch
From Python to Java: Converting a Simple Program - YouTube
Enjoy the videos and music you love, upload original content, and share it all with friends, family, and the world on YouTube.
Published   January 18, 2018
🌐
Javainuse
javainuse.com › py2java
Online Python to Java Converter Tool
Please add javainuse.com to your ad blocking whitelist or disable your adblocking software. × · Online tool to convert Python source code into Java.
🌐
GitHub
github.com › fmount › Py2J
GitHub - fmount/Py2J: A translator from Python to Java
A translator from Python to Java. Contribute to fmount/Py2J development by creating an account on GitHub.
Forked by 2 users
Languages   Java 98.2% | Java 98.2%
🌐
Reddit
reddit.com › r/jupyternotebooks › python script to jupyter notebook
r/JupyterNotebooks on Reddit: Python script to Jupyter notebook
July 3, 2019 - Just published a Python package on PyPI called p2j. Run pip install p2j and p2j script.py to get a Jupyter notebook script.ipynb. Example shown…
🌐
PyPI
pypi.org › project › p2j › 1.1.0
p2j
March 6, 2019 - JavaScript is disabled in your browser · Please enable JavaScript to proceed · A required part of this site couldn’t load. This may be due to a browser extension, network issues, or browser settings. Please check your connection, disable any ad blockers, or try using a different browser
Top answer
1 of 5
4

My solutions

java thread/process <-> Pipes <-> py subprocess

Use pipes by java's ProcessBuilder to call py with args "-u" to transfer data via pipes.

Here is a good practice.

https://github.com/JULIELab/java-stdio-ipc

Here is my stupid research result about "java <-> py"

  • [Jython] Java implement of python.

  • [Jpype] JPype is designed to allow the user to exercise Java as fluidly as possible from within Python. We can break this down into a few specific design goals. Unlike Jython, JPype does not achieve this by re-implementing Python, but instead by interfacing both virtual machines at the native level. This shared memory based approach achieves good computing performance while providing the access to the entirety of CPython and Java libraries.

  • [Runtime] The Runtime class in java (old method).

  • [Process] Java ProcessBuilder class gives more structure to the arguments.

  • [Pipes] Named pipes could be the answer for you. Use subprocess. Popen to start the Java process and establish pipes to communicate with it. Try mkfifo() implementation in python.
    https://jj09.net/interprocess-communication-python-java/

     -> java<-> Pipes <-> py https://github.com/JULIELab/java-stdio-ipc 
    
  • [Protobuf] This is the opensource solution Google uses to do IPC between Java and Python. For serializing and deserializing data efficiently in a language-neutral, platform-neutral, extensible way, take a look at Protocol Buffers.

  • [Socket] CS-arch throgh socket Server(Python) - Client(Java) communication using sockets https://jj09.net/interprocess-communication-python-java/ Send File From Python Server to Java Client

  • [procbridge] A super-lightweight IPC (Inter-Process Communication) protocol over TCP socket. https://github.com/gongzhang/procbridge https://github.com/gongzhang/procbridge-python https://github.com/gongzhang/procbridge-java

  • [hessian binary web service protocol] using python client and java server.

  • [Jython] Jython is a reimplementation of Python in Java. As a result it has much lower costs to share data structures between Java and Python and potentially much higher level of integration. Noted downsides of Jython are that it has lagged well behind the state of the art in Python; it has a limited selection of modules that can be used; and the Python object thrashing is not particularly well fit in Java virtual machine leading to some known performance issues.

  • [Py4J] Py4J uses a remote tunnel to operate the JVM. This has the advantage that the remote JVM does not share the same memory space and multiple JVMs can be controlled. It provides a fairly general API, but the overall integration to Python is as one would expect when operating a remote channel operating more like an RPC front-end. It seems well documented and capable. Although I haven’t done benchmarking, a remote access JVM will have a transfer penalty when moving data.

  • [Jep] Jep stands for Java embedded Python. It is a mirror image of JPype. Rather that focusing on accessing Java from within Python, this project is geared towards allowing Java to access Python as a sub-interpreter. The syntax for accessing Java resources from within the embedded Python is quite similar to support for imports. Notable downsides are that although Python supports multiple interpreters many Python modules do not, thus some of the advantages of the use of Python may be hard to realize. In addition, the documentation is a bit underwhelming thus it is difficult to see how capable it is from the limited examples.

  • [PyJnius] PyJnius is another Python to Java only bridge. Syntax is somewhat similar to JPype in that classes can be loaded in and then have mostly Java native syntax. Like JPype, it provides an ability to customize Java classes so that they appear more like native classes. PyJnius seems to be focused on Android. It is written using Cython .pxi files for speed. It does not include a method to represent primitive arrays, thus Python list must be converted whenever an array needs to be passed as an argument or a return. This seems pretty prohibitive for scientific code. PyJnius appears is still in active development.

  • [Javabridge] Javabridge is direct low level JNI control from Python. The integration level is quite low on this, but it does serve the purpose of providing the JNI API to Python rather than attempting to wrap Java in a Python skin. The downside being of course you would really have to know a lot of JNI to make effective use of it.

  • [jpy] This is the most similar package to JPype in terms of project goals. They have achieved more capabilities in terms of a Java from Python than JPype which does not support any reverse capabilities. It is currently unclear if this project is still active as the most recent release is dated 2014. The integration level with Python is fairly low currently though what they do provide is a similar API to JPype.

  • [JCC] JCC is a C++ code generator that produces a C++ object interface wrapping a Java library via Java’s Native Interface (JNI). JCC also generates C++ wrappers that conform to Python’s C type system making the instances of Java classes directly available to a Python interpreter. This may be handy if your goal is not to make use of all of Java but rather have a specific library exposed to Python.

  • [VOC] https://beeware.org/project/projects/bridges/voc/_ A transpiler that converts Python bytecode into Java bytecode part of the BeeWare project. This may be useful if getting a smallish piece of Python code hooked into Java. It currently list itself as early development. This is more in the reverse direction as its goals are making Python code available in Java rather providing interaction between the two.

  • [p2j] This lists itself as “A (restricted) python to java source translator”. Appears to try to convert Python code into Java. Has not been actively maintained since 2013. Like VOC this is primilarly for code translation rather that bridging.

  • [GraalVM] Source: https://github.com/oracle/graal

2 of 5
3

PySpark uses Py4J quite successfully. If all the heavylifting is done on Spark (or Mahout in your case) itself, and you just want to return result back to "driver"/Python code, then Py4J might work for you very well as well.

Py4j has slightly bigger overhead for huge results (that's not necessarily the case for Spark workloads, as you only return summaries /aggregates for the dataframes). There is an improvement discussion for py4j to switch to binary serialization to remove that overhead for higher badnwidth requirements too: https://github.com/bartdag/py4j/issues/159