So I was looking at various packages for for Java that allow you to run python code via Java. Jython does not handle the equivalent of python 3.6 code. So basically, I have code written in python 3.6 and requires so. I need to be able to utilize that python code via Java without having a python interpreter. Is there such a package? (Note I will not be changing the python code because that is a framework and that traditionally be used in python. I have a python framework that would be traditionally utilized via python code of course, for python 3.6. Instead, I would like to be utilize it via Java 8 code. Now I looked into Jython but it does not handle 3.6 or 3.x for that matter. I will not be changing the python code from the framework. Additionally, the Java Package should be able to run the python code without an interpreter. Is there such a thing? Py4j requires a python interpreter.
Asked
Active
Viewed 259 times
-2
-
3"run python without an interpreter".. huh? That's like asking to run Java without a JVM. – xtratic Apr 16 '19 at 15:53
-
2For Java to be able to run python without an interpreter would sort of require it to implement an interpreter. – juanpa.arrivillaga Apr 16 '19 at 15:56
-
1@JustAFellowCoder: You will probably need to have two operating system processes, one for your Python code and one for your Java code, which can communicate in some way (e.g. TCP sockets on the loopback interface). This is a general solution that works for any software written in any language. Of course, in this scenario, the Python code would need a Python interpreter (or equivalent) to execute, and the Java code would need a JVM to execute; those environments would simply exist in separate (parallel) operating system processes. – Daniel Pryden Apr 16 '19 at 16:00
-
@juanpa.arrivillaga Is there such an implementation of an interpreter, in the form of a jar file. – JustAFellowCoder Apr 16 '19 at 16:01
-
1@JustAFellowCoder: Yes, it's called Jython. – Daniel Pryden Apr 16 '19 at 16:01
-
@DanielPryden as I have specifically stated in the question, Jython is not an option for it does not handle 3.x python code. – JustAFellowCoder Apr 16 '19 at 16:02
-
3@JustAFellowCoder: To the best of my knowledge, there does not exist a non-Jython implementation of a Python interpreter written in pure Java. If one existed, it would be a competitor to Jython; if it were open-source, it would probably be absorbed by the Jython project (or vice versa). You are welcome to write such an interpreter yourself if that's what you need! – Daniel Pryden Apr 16 '19 at 16:03
-
@JustAFellowCoder: Also, at this point, it looks like your question is no longer asking a *question* about *programming*: instead, you're asking for a *recommendation* of a *programming tool*, which is explicitly off-topic for Stack Overflow. – Daniel Pryden Apr 16 '19 at 16:04
-
possible duplicate of https://stackoverflow.com/questions/2351008/when-will-jython-support-python-3 – Daniel Pryden Apr 16 '19 at 16:10
-
@danielpryden One would argue the "duplicate emphasizes jython. – JustAFellowCoder Apr 16 '19 at 16:14
3 Answers
3
GraalVM compiles Python code to Java bytecode and runs it on the JVM using graalpython, with this caveat:
This Python implementation currently aims to be compatible with Python 3.7, but it is a long way from there, and it is very likely that any Python program that requires any imports at all will hit something unsupported. At this point, the Python implementation is made available for experimentation and curious end-users.

David Conrad
- 15,432
- 2
- 42
- 54
1
No, running Python code requires some Python interpreter or other.

brunns
- 2,689
- 1
- 13
- 24
-
1Or a Python compiler/transpiler that targets another language (e.g. Cython), although that has its own limitations. – Daniel Pryden Apr 16 '19 at 15:58
0
Since you won't change it, you could convert the Python to an executable and use Java to spawn it as a new process.
ProcessBuilder pb = new ProcessBuilder("C:\\...\\file.exe", command arguments, ..., ...);
pb.start();
You could use File I/O to communicate between Python and Java as the easiest solution.

Chris P
- 11
- 1
-
That doesn't run "without an interpreter", it simply embeds the interpreter into an executable file along with the compiled Python bytecode. You still need an out-of-process Python interpreter. – Daniel Pryden Apr 16 '19 at 16:07
-
If Python scripts interact with other Python scripts in that framework, will this method mess with it. – JustAFellowCoder Apr 16 '19 at 16:12
-
-
@JustAFellowCoder If the actual python interpreter is installed on the machines you want to run from, then you don't need to convert to an exe. You can just run the python process from java as you would on a command line – Chris P Apr 16 '19 at 16:30