You can always create java code the same way webapps create HTML: with a template. Then you can have the java source code compiled into bytecode using a regular Java compiler ( see: Package javax.tools )

Not necessarily the best option, but definitely an option ( and quite simple btw )

Answer from OscarRyz on Stack Overflow
๐ŸŒ
Py4j
py4j.org
Welcome to Py4J โ€” Py4J
Here is a brief example of what you can do with Py4J. The following Python program creates a java.util.Random instance from a JVM and calls some of its methods. It also accesses a custom Java class, AdditionApplication to add the generated numbers.
Discussions

Best way to combine Python and Java?
I've used JPype for a while. It also starts a JVM from python. Once set up, interoperating with Java is transparent. You can start the JVM in such a way that it can be debugged directly using remote debugging tools. More on reddit.com
๐ŸŒ r/java
82
60
October 29, 2022
Calling Python in Java? - Stack Overflow
It depends on what do you mean by python functions? if they were written in cpython you can not directly call them you will have to use JNI, but if they were written in Jython you can easily call them from java, as jython ultimately generates java byte code. More on stackoverflow.com
๐ŸŒ stackoverflow.com
Calling Java from Python - Stack Overflow
What is the best way to call java from python? (jython and RPC are not an option for me). I've heard of JCC: http://pypi.python.org/pypi/JCC/1.9 a C++ code generator for calling Java from C++/Pytho... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Python Code Generation Tools
I'd probably use metaclasses if I had the kind of problem where code generation could actually help. The last time I saw a project using code generation where I thought that it ought have to used metaclasses was a tool to generate python code from swagger descriptions. Django's ORM is a good example where metaclasses were used to good effect (there are very few of those). In general I'm extremely suspicious of anybody who claims to have the kind of problem that requires either metaclasses or code generation would help though. It is suitable only for problems that are very, very, very abstract (e.g. like an ORM or REST API interaction framework). In 90% of cases I've seen the person attempting to use it vastly overgeneralized the problem that they actually had in an attempt to prepare for future problems never arose. In the process of doing that they then created a code clusterfuck that was hellishly hard to understand and debug (and in some cases I was left to clean up the mess). It wasn't simple and it can't, intrinsically, ever be simple (there is some language theory behind this). I could be convinced otherwise by describing your problem, but right now I'm 80% convinced that what you have described is an XY problem and that you're attempting to do this because you overgeneralized your problem. If you describe the actual problem you're solving it might help. More on reddit.com
๐ŸŒ r/Python
18
8
March 9, 2019
๐ŸŒ
Reddit
reddit.com โ€บ r/java โ€บ best way to combine python and java?
r/java on Reddit: Best way to combine Python and Java?
October 29, 2022 -

My project uses some packages that are available only in Python and heavily rely on C libraries. The project also greatly benefits from Java libraries and the JVM. What's the optimal way to call Python functions from Java?

I tried:

  1. Small web-services: overhead to serialize data, start and stop the services. Also debugging is harder and implementing each new function is now double the effort.

  2. Jpy: a library that runs an interpreter in the JVM. Spare the service start/stop, but: isn't really feasible for more than a single-liner, data translation between Java and Python is cumbersome, and I also encountered runtime segmentation fault errors.

Any other options?

The project is in the machine-learning domain, so involves exchanging large numeric arrays and text. In some cases the execution switches back and forth between the platforms.

You can always create java code the same way webapps create HTML: with a template. Then you can have the java source code compiled into bytecode using a regular Java compiler ( see: Package javax.tools )

Not necessarily the best option, but definitely an option ( and quite simple btw )

Answer from OscarRyz on Stack Overflow
๐ŸŒ
GitHub
github.com โ€บ kylecorry31 โ€บ VisionGen
GitHub - kylecorry31/VisionGen: A Python script to generate Java code for the Robot Vision API. ยท GitHub
June 2, 2021 - A Python script which generates Java code for the Robot Vision API to help teams get started with simple vision tracking. Future: Build a GUI for this. ... Then clone this repository. To use this script, run the following command from the source directory. Where ClassName is the name of the Java file/class.
Author ย  kylecorry31
๐ŸŒ
Jython
jython.org
Home | Jython
The seamless interaction between Python and Java allows developers to freely mix the two languages both during development and in shipping products. import org.python.util.PythonInterpreter; public class JythonHelloWorld { public static void main(String[] args) { try(PythonInterpreter pyInterp = new PythonInterpreter()) { pyInterp.exec("print('Hello Python World!')"); } } } from java.lang import System # Java import print('Running on Java version: ' + System.getProperty('java.version')) print('Unix time from Java: ' + str(System.currentTimeMillis()))
๐ŸŒ
GitHub
github.com โ€บ beeware โ€บ voc
GitHub - beeware/voc: A transpiler that converts Python code into Java bytecode
This is experimental code. If it breaks, you get to keep all the shiny pieces. ... Provides an API to let you programmatically create Java class files. Compiles Python 3.4 source files into Java class files, enabling you to run Python code on a JVM (including Android's VM).
Starred by 863 users
Forked by 510 users
Languages ย  Python 53.3% | Java 46.7% | Python 53.3% | Java 46.7%
Find elsewhere
๐ŸŒ
Jython
jython.org โ€บ jython-old-sites โ€บ archive โ€บ 21 โ€บ docs โ€บ jythonc.html
Using the Jython compiler - jythonc
The previous section describes how Python classes can be created that subclass from Java classes. This works very well when you want to pass a Python class into Java from Jython. This is not adequate for building real Java ".class" files that implement a Java class and can be passed directly ...
๐ŸŒ
Telosys
telosys.org
Telosys code generator for Java, JavaScript, Python, NodeJS, PHP, C#, etc
Telosys is a free code generator usable with different languages or frameworks : Java, JavaScript, Python, NodeJS, PHP, GoLang, C#, Angular, VueJS, etc
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ how to call python from java
How to Call Python From Java | Baeldung
August 27, 2025 - As before, the final step is to get the output from our script and check it matches what we were expecting. Continuing with Jython, we also have the possibility of embedding Python code directly into our Java code.
Top answer
1 of 12
116

Jython: Python for the Java Platform - http://www.jython.org/index.html

You can easily call python functions from Java code with Jython. That is as long as your python code itself runs under jython, i.e. doesn't use some c-extensions that aren't supported.

If that works for you, it's certainly the simplest solution you can get. Otherwise you can use org.python.util.PythonInterpreter from the new Java6 interpreter support.

A simple example from the top of my head - but should work I hope: (no error checking done for brevity)

PythonInterpreter interpreter = new PythonInterpreter();
interpreter.exec("import sys\nsys.path.append('pathToModules if they are not there by default')\nimport yourModule");
// execute a function that takes a string and returns a string
PyObject someFunc = interpreter.get("funcName");
PyObject result = someFunc.__call__(new PyString("Test!"));
String realResult = (String) result.__tojava__(String.class);

As of 2021, Jython does not support Python 3.x

2 of 12
77

I think there are some important things to consider first with how strong you wish to have the linking between Java and Python.

Firstly Do you only want to call functions or do you actually want Python code to change the data in your java objects? This is very important. If you only want to call some Python code with or without arguments, then that is not very difficult. If your arguments are primitives it makes it even more easy. However, if you want to have Java class implement member functions in Python, which change the data of the Java object, then this is not so easy or straight forward.

Secondly are we talking CPython, or will Jython do? I would say CPython is where its at! I would advocate this is why Python is so kool! Having such high abstractions however access to C or C++ when needed. Imagine if you could have that in Java. This question is not even worth asking if Jython is ok because then it is easy anyway.

So I have played with the following methods, and listed them from easy to difficult:

Java to Jython

Advantages: Trivially easy. Have actual references to Java objects

Disadvantages: No CPython, Extremely Slow!

Jython from Java is so easy, and if this is really enough then great. However it is very slow and no CPython! Is life worth living without CPython? I don't think so! You can easily have Python code implementing your member functions for you Java objects.

Java to Jython to CPython via Pyro

Pyro is the remote object module for Python. You have some object on a CPython interpreter, and you can send it objects which are transferred via serialization and it can also return objects via this method. Note that if you send a serialized Python object from Jython and then call some functions which change the data in its members, then you will not see those changes in Java. You just need to remember to send back the data which you want from Pyro. This, I believe, is the easiest way to get to CPython! You do not need any JNI or JNA or SWIG or .... You don't need to know any C, or C++. Kool huh?

Advantages:

  • Access to CPython
  • Not as difficult as following methods

Disadvantages:

  • Cannot change the member data of Java objects directly from Python
  • Is somewhat indirect (Jython is middle man)

Java to C/C++ via JNI/JNA/SWIG to Python via Embedded interpreter (maybe using BOOST Libraries?)

OMG this method is not for the faint of heart. And I can tell you it has taken me very long to achieve this in with a decent method. Main reason you would want to do this is so that you can run CPython code which as full rein over you java object. There are major things to consider before deciding to try and breed Java (which is like a chimp) with Python (which is like a horse). Firstly if you crash the interpreter, that's lights out for you program! And don't get me started on concurrency issues! In addition, there is a lot of boiler, I believe I have found the best configuration to minimize this boiler but it is still a lot! So how to go about this: Consider that C++ is your middle man, your objects are actually C++ objects! Good that you know that now. Just write your object as if your program is in C++ and not Java, with the data you want to access from both worlds. Then you can use the wrapper generator called SWIG to make this accessible to java and compile a dll which you call (System.load(dllNameHere)) in Java. Get this working first, then move on to the hard part! To get to Python you need to embed an interpreter. Firstly I suggest doing some hello interpreter programs or this tutorial Embedding Python in C/C. Once you have that working, its time to make the horse and the monkey dance! You can send you C++ object to Python via [boost][3] . I know I have not given you the fish, merely told you where to find the fish. Some pointers to note for this when compiling.

When you compile boost you will need to compile a shared library. And you need to include and link to the stuff you need from jdk, ie jawt.lib, jvm.lib, (you will also need the client jvm.dll in your path when launching the application) As well as the python27.lib or whatever and the boost_python-vc100-mt-1_55.lib. Then include Python/include, jdk/include, boost and only use shared libraries (dlls) otherwise boost has a teary. And yeah full on I know. There are so many ways in which this can go sour. So make sure you get each thing done block by block. Then put them together.

๐ŸŒ
Sails Software Inc
sailssoftware.com โ€บ how-to-call-python-code-from-java-and-vice-versa
How to Call Python Code from Java and Vice Versa - Sails Software Inc
July 18, 2023 - 1. Import the required Java classes and packages. 2. Initialize the Python interpreter in your Java code using the appropriate methods. 3. Call the Python code and retrieve the results in Java variables.
๐ŸŒ
CodeConvert AI
codeconvert.ai โ€บ python-to-java-converter
Free Python to Java Converter โ€” AI Code Translation | CodeConvert AI
The AI understands both Python and Java idioms and produces natural-looking code. ... Signing in unlocks CodeConvert AI's Pro tool, which includes more powerful AI models, an integrated chat assistant, code execution, personal notes, conversion history, and an enhanced interface. Every account gets 5 free credits per day to explore the full Pro experience. Code ConverterCode GeneratorCode ExplainerComment RemoverCode CheckerCode to PDF
๐ŸŒ
Medium
medium.com โ€บ geekculture โ€บ how-to-execute-python-modules-from-java-2384041a3d6d
How To Execute Python Modules From Java | by Galina Blokh | Geek Culture | Medium
July 14, 2022 - Four lines of code, and we have a python3.9 Interpreter in Java! Now you will see how to run the Python functions from the document youโ€™ve already seen.
๐ŸŒ
LinkedIn
linkedin.com โ€บ pulse โ€บ integrating-python-java-guide-developers-myexamcloud-7g5bc
Integrating Python and Java: A Guide for Developers
December 26, 2023 - It provides a bridge between the two languages by allowing developers to call Java classes and methods from Python scripts. To use Pyjnius, you first need to install it on your system, along with Kivy. Then, you can import Java classes and use them in your Python code.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ integrating-java-with-python
Integrating Java with Python - GeeksforGeeks
July 23, 2025 - Also, since Java provides a wide variety of collections, we can directly use them in a Python program by including their java packages in the program. We will make use of py4j for invoking Java functionality from Python. It can be installed by executing the following command from the command line: ... After this, we need to improve the IDE in which we will be writing our code.
๐ŸŒ
Quora
quora.com โ€บ Is-there-any-way-to-convert-the-java-code-to-python-code
Is there any way to convert the java code to python code? - Quora
Answer (1 of 6): Hi, It most definitely is! There is nothing in one of those languages that you can not do in one of the others. They might tackle some specific problems differently, but youโ€™ll be able to convert a program written in one language to another. That being said, each language has ...