๐ŸŒ
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!')"); } } }
Java implementation of the Python interpreter
Jython Logo
Jython is an implementation of the Python programming language designed to run on the Java platform. It was known as JPython until 1999. Jython programs can import and use any Java class. โ€ฆ Wikipedia
Factsheet
Initial release January 17, 2001; 25 years ago (2001-01-17)
Stable release 2.7.4
/ 18 August 2024; 19 months ago (18 August 2024)
Written in Python and Java
Factsheet
Initial release January 17, 2001; 25 years ago (2001-01-17)
Stable release 2.7.4
/ 18 August 2024; 19 months ago (18 August 2024)
Written in Python and Java
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ integrating-java-with-python
Integrating Java with Python - GeeksforGeeks
July 23, 2025 - For this purpose, we need to create an instance of the class JavaGateway available in the module py4j.java_gateway. The first two lines of the Python script hello achieve this. When we invoke JavaGateway (line 2), Python tries to connect with the JVM of the Java program already running and returns an instance.
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
For example: ... GraalVM is a good choice. I've done Java+Javascript combination with GraalVM for microservice design (Java with Javascript reflection). They recently added support for python, I'd give it a try especially with how big its community has grown over the years. More on stackoverflow.com
๐ŸŒ stackoverflow.com
Calling Java from Python - Stack Overflow
It also supports Java calling back into Python so that Python clients can implement Java interfaces. It works by communicating over a socket. jpy is another in-process implementation. One of its key features is support for fast pass-by-value operations with arrays by use of pointer hand-off. jpype is another in-process implementation. Since it also uses internal C-based handoff it's highly performant. A simple example ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Using Python from within Java - Stack Overflow
I need to do some text and language processing, and I'd much rather use Python and a library like NLTK to do this. I'm aware of the Jython project, but it looks like this represents a way to use Java and its libraries from within Python, rather than the other way round - am I wrong about this? More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ how to call python from java
How to Call Python From Java | Baeldung
August 27, 2025 - In actual fact Python ships with a simple built-in HTTP server which we can use for sharing content or files over HTTP: ... If we now go to http://localhost:9000, weโ€™ll see the contents listed for the directory where we launched the previous command. Some other popular frameworks we could consider using for creating more robust Python-based web services or applications are Flask and Django. Once we have an endpoint we can access, we can use any one of several Java HTTP libraries to invoke our Python web service/application implementation.
๐ŸŒ
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.

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.

๐ŸŒ
Py4j
py4j.org
Welcome to Py4J โ€” Py4J
This is the Java program that was executing at the same time (no code was generated and no tool was required to run these programs). The AdditionApplication app instance is the gateway.entry_point in the previous code snippet. Note that the Java program must be started before executing the Python code above.
๐ŸŒ
pytz
pythonhosted.org โ€บ javabridge โ€บ java2python.html
Calling Python from Java โ€” python-javabridge 1.0.12 documentation
class MyClass { static final CPython cpython = CPython(); public List<String> whereIsWaldo(String root) { ArrayList<String> result = new ArrayList<String>(); Hashtable locals = new Hashtable(); locals.put("result", result); locals.put("root", root); StringBuilder script = new StringBuilder(); script.append("import os\n"); script.append("import javabridge\n"); script.append("root = javabridge.to_string(root)"); script.append("result = javabridge.JWrapper(result)"); script.append("for path, dirnames, filenames in os.walk(root):\n"); script.append(" if 'waldo' in filenames:"); script.append(" result.add(path)"); cpython.exec(script.toString(), locals, null); return result; } } ... execute is a synonym for exec which is a Python keyword.
Find elsewhere
๐ŸŒ
LinkedIn
linkedin.com โ€บ pulse โ€บ integrating-python-java-guide-developers-myexamcloud-7g5bc
Integrating Python and Java: A Guide for Developers
December 26, 2023 - To use Py4J, you need to download and add the JAR files to your Java project's classpath. Then, you can start a Python process from within your Java application and use its functions and objects. Here's an example of Java code using Py4J to call a Python function and print its return value:
๐ŸŒ
GitHub
github.com โ€บ ninia โ€บ jep
GitHub - ninia/jep: Embed Python in Java ยท GitHub
Jep embeds CPython in Java through JNI. ... Using the native Python interpreter may be much faster than alternatives.
Starred by 1.5K users
Forked by 163 users
Languages ย  C 52.1% | Java 30.2% | Python 17.7%
๐ŸŒ
DZone
dzone.com โ€บ coding โ€บ java โ€บ using python libraries in java
Using Python Libraries in Java
May 16, 2025 - In practice, more and more use cases are emerging where both languages are combined to produce better results. For example, in a microservices architecture: Java is used for core business logic and high-performance services (e.g., with Spring Boot). Python is used for data processing microservices or small utility tools.
๐ŸŒ
Medium
medium.com โ€บ @chamlinid โ€บ integrate-java-and-python-code-bases-1c4819fe19da
Integrate Java and Python code bases | by Chamlini Dayathilake | Medium
February 14, 2025 - Write a function inside a Python script and invoke the function inside the script itself. Use print(), to return values from the script. Create a ProcessBuilder instance in the Java code, with the path of the script file and required parameter ...
๐ŸŒ
AskPython
askpython.com โ€บ home โ€บ how to call java using python with jpype and pyjnius
Calling Java using Python | #1 guide to Jpype
November 8, 2023 - In the calci.java we are creating a java class that will be returning a multiplication of the randomly generated numbers a and b. C:\Users\tanma\Downloads\jpype-example\python\java\pkg\returnmath.java
๐ŸŒ
Real Python
realpython.com โ€บ java-vs-python
Java vs Python: Basic Python for Java Developers โ€“ Real Python
August 16, 2024 - The example above shows the functionality of the two preceding examples combined in one single loop. By default, the accompanying counter starts at zero, but with the optional keyword argument start, you can specify another value. Note: Check out Python enumerate(): Simplify Loops That Need Counters if you want to know more about Python loops and enumerate(). Python also understands the break and continue statements. Another control flow construct similar to Java is the if statement:
๐ŸŒ
CodeHS
codehs.com โ€บ tutorial โ€บ david โ€บ python-for-java-users
Tutorial: Python For Java Users | CodeHS
This tutorial is a very basic overview of Python for users that have Java experience.
๐ŸŒ
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 - The speed performance is a crucial metric, especially if your application deal with BigData. Tests in my research showed that using JEP is nearly pure python execution. Assuming you have Java (Maven) and Python in your system. In the Java project, in the pom.xml file, you set a dependency: The pom.xml dependency for running Python from Java ... You installed JEP in Java! ... This command installs the latest JEP version. You can specify a particular JEP version if you need it. In my example, I use JEP 4.0.3 version.
๐ŸŒ
Coursera
coursera.org โ€บ browse โ€บ computer science โ€บ software development
Introduction to Programming with Python and Java | Coursera
The courses were designed to be taken in sequence, starting with the first and ending with the last. That being said, learners can pick and choose the courses they are interested in based on their background. No, but the concepts move very quickly so it is helpful to have some introductory exposure (such as Pennโ€™s ... ). This Specialization is intended for anyone who has an interest in problem solving and wants to learn Python or Java.
Rating: 4.5 โ€‹ - โ€‹ 1.9K votes
๐ŸŒ
Processing
py.processing.org โ€บ tutorials โ€บ python-jython-java
Python, Jython and Java \ Tutorials
Python Mode for Processing extends the Processing Development Environment with the Python programming language.
๐ŸŒ
Devmio
devm.io โ€บ python โ€บ integrating-python-with-java-170663
Integrating Python with Java
April 21, 2020 - Python is an object-oriented scripting language, which automatically makes it a good pair for Java. But when combined with a Python interpreter written entirely in Java, like Jython, you could do things like write entire applets in Python.