Sorry to ressurect the thread, but there was no accepted answer...

You could also use Py4J. There is an example on the frontpage and lots of documentation, but essentially, you just call Java methods from your python code as if they were python methods:

>>> from py4j.java_gateway import JavaGateway
>>> gateway = JavaGateway()                        # connect to the JVM
>>> java_object = gateway.jvm.mypackage.MyClass()  # invoke constructor
>>> other_object = java_object.doThat()
>>> other_object.doThis(1,'abc')
>>> gateway.jvm.java.lang.System.out.println('Hello World!') # call a static method

As opposed to Jython, Py4J runs in the Python VM so it is always "up to date" with the latest version of Python and you can use libraries that do not run well on Jython (e.g., lxml). The communication is done through sockets instead of JNI.

Disclaimer: I am the author of Py4J

Answer from Barthelemy on Stack Overflow
๐ŸŒ
DZone
dzone.com โ€บ coding โ€บ java โ€บ using python libraries in java
Using Python Libraries in Java
May 16, 2025 - With Jython, you can easily execute external Python scripts from Java. The integration works well for simple scripts, especially when passing parameters from Java to Python. However, for more complex Python 3 libraries, you should consider ...
๐ŸŒ
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.

๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ how to call python from java
How to Call Python From Java | Baeldung
August 27, 2025 - As PythonIntepreter implements AutoCloseable, itโ€™s good practice to use try-with-resources when working with this class ยท The PythonInterpreter class name does not imply that our Python code is interpreted. Python programs in Jython are run by the JVM and therefore compiled to Java bytecode before execution ยท Although Jython is the Python implementation for Java, it may not contain all the same sub-packages as native Python ยท Another third-party library that we could consider using is Apache Common Exec which attempts to overcome some of the shortcomings of the Java Process API.
๐ŸŒ
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.
๐ŸŒ
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 - Now you can execute Python directly from Java. First, we should define the JEP library path (specifically where Java will search for Python to execute) and initialize the MainInterpreter object:
Find elsewhere
๐ŸŒ
Py4j
py4j.org
Welcome to Py4J โ€” Py4J
Py4J enables Python programs running in a Python interpreter to dynamically access Java objects in a Java Virtual Machine. Methods are called as if the Java objects resided in the Python interpreter and Java collections can be accessed through standard Python collection methods.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ jython โ€บ jython_importing_java_libraries.htm
Jython - Importing Java Libraries
One of the most important features of Jython is its ability to import Java classes in a Python program. We can import any java package or class in Jython, just as we do in a Java program. The following example shows how the java.util packages are imported in Python (Jython) script to declare an obje
๐ŸŒ
LinkedIn
linkedin.com โ€บ pulse โ€บ integrating-python-java-guide-developers-myexamcloud-7g5bc
Integrating Python and Java: A Guide for Developers
December 26, 2023 - It allows developers to seamlessly use Python code within a Java application and enables interoperability between the two languages. Jython code can call Java libraries, and Java code can also use Python libraries.
๐ŸŒ
Medium
medium.com โ€บ graalvm โ€บ supercharge-your-java-apps-with-python-ec5d30634d18
Supercharge Your Java Apps with Python | by Tim Felgentreff | graalvm
May 5, 2023 - The GraalVM ecosystem consists of a very interesting collection of languages: JavaScript, Ruby, Python, WebAssembly, Java, LLVM bitcode, and more. All of them bring unique advantages. Python on GraalVM opens up the rich ecosystem of Python data science libraries to Java developers. While the Python support on GraalVM is still experimental, you can use it today to extend your Java applications with Python code and libraries.
๐ŸŒ
Readthedocs
jpy.readthedocs.io โ€บ en โ€บ latest โ€บ intro.html
Introduction โ€” jpy 0.9.0 documentation
The same resulting shared library is used as a Python jpy module and also as native library for the Java library (jpy.jar). Python programs that import the jpy module can load Java classes, access Java class fields, and call class constructors and methods. Java programs with jpy.jar on the ...
๐ŸŒ
The SW Developer
theswdeveloper.com โ€บ post โ€บ java-python-jep
How to execute Python code in Java - The SW Developer
September 10, 2021 - Jep is an open source library which gives you the ability to execute Python code in Java and vice versa. Let's say you have an existing Java application and you need to use some 3rd party library.
๐ŸŒ
Readthedocs
jython.readthedocs.io โ€บ en โ€บ latest โ€บ JythonAndJavaIntegration
Chapter 10: Jython and Java Integration โ€” Definitive Guide to Jython latest documentation
Most Jython developers are either Python developers that are looking to make use of the vast library that the JVM has to offer, or Java developers that would like to utilize the Python language semantics without migrating to a completely different platform. The fact is that most Jython developers are using it so that they can take advantage of the vast libraries available to the Java world, and in order to do so there needs to be a certain amount of Java integration in the application.
๐ŸŒ
cyberangles
cyberangles.org โ€บ blog โ€บ using-a-java-library-from-python
How to Use a Java Library from Python: An Elegant Guide for Beginners (Beyond Command Line Invocation) โ€” CyberAngles.org
Python: Installed (version 3.6 or higher). Verify with python --version. A Sample Java Library: Weโ€™ll use a simple MathUtils library with two methods:
๐ŸŒ
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 - So far, we have seen how we can call Java using Python using Jpype. Itโ€™s clear that this will be beneficial for us as weโ€™ll be able to utilize the advantages of both languages. We can enjoy the straightforward syntax of Python while also having access to the extensive libraries and framework support of Java.
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.

๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ integrating-java-with-python
Integrating Java with Python - GeeksforGeeks
July 23, 2025 - In our case, an object of GFG serves as an entry point (line 10). Here it is important to mention that a Python program will be able to use a Java program only if it (a Java program) is ready to accept incoming requests.