Why not use Jython? The only downside I can immediately think of is if your library uses CPython native extensions.

EDIT: If you can use Jython now but think you may have problems with a later version of the library, I suggest you try to isolate the library from your app (e.g. some sort of adapter interface). Go with the simplest thing that works for the moment, then consider JNI/CPython/etc if and when you ever need to. There's little to be gained by going the (painful) JNI route unless you really have to.

Answer from Jon Skeet on Stack Overflow
Top answer
1 of 12
38

Why not use Jython? The only downside I can immediately think of is if your library uses CPython native extensions.

EDIT: If you can use Jython now but think you may have problems with a later version of the library, I suggest you try to isolate the library from your app (e.g. some sort of adapter interface). Go with the simplest thing that works for the moment, then consider JNI/CPython/etc if and when you ever need to. There's little to be gained by going the (painful) JNI route unless you really have to.

2 of 12
21

Frankly most ways to somehow run Python directly from within JVM don't work. They are either not-quite-compatible (new release of your third party library can use python 2.6 features and will not work with Jython 2.5) or hacky (it will break with cryptic JVM stacktrace not really leading to solution).

My preferred way to integrate the two would use RPC. XML RPC is not a bad choice here, if you have moderate amounts of data. It is pretty well supported โ€” Python has it in its standard library. Java libraries are also easy to find. Now depending on your setup either Java or Python part would be a server accepting connection from other language.

A less popular but worth considering alternative way to do RPCs is Google protobuffers, which have 2/3 of support for nice rpc. You just need to provide your transport layer. Not that much work and the convenience of writing is reasonable.

Another option is to write a C wrapper around that pieces of Python functionality that you need to expose to Java and use it via JVM native plugins. You can ease the pain by going with SWIG SWIG.

Essentially in your case it works like that:

  1. Create a SWIG interface for all method calls from Java to C++.
  2. Create C/C++ code that will receive your calls and internally call python interpreter with right params.
  3. Convert response you get from python and send it via swig back to your Java code.

This solution is fairly complex, a bit of an overkill in most cases. Still it is worth doing if you (for some reason) cannot afford RPCs. RPC still would be my preferred choice, though.

๐ŸŒ
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.

Why not use Jython? The only downside I can immediately think of is if your library uses CPython native extensions.

EDIT: If you can use Jython now but think you may have problems with a later version of the library, I suggest you try to isolate the library from your app (e.g. some sort of adapter interface). Go with the simplest thing that works for the moment, then consider JNI/CPython/etc if and when you ever need to. There's little to be gained by going the (painful) JNI route unless you really have to.

Answer from Jon Skeet on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ integrating-java-with-python
Integrating Java with Python - GeeksforGeeks
July 23, 2025 - Packages like Py4j, Pyjnius, Jpype, javabridge, and JCC help invoke Java programs from Python. 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.
๐ŸŒ
Jython
jython.org
Home | Jython
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()))
๐ŸŒ
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.
๐ŸŒ
Readthedocs
jython.readthedocs.io โ€บ en โ€บ latest โ€บ JythonAndJavaIntegration
Chapter 10: Jython and Java Integration โ€” Definitive Guide to Jython latest documentation
This chapter will focus on integrating Java and Python, but it will take several different vantage points on the topic. You will learn several techniques to make use Jython code within your Java applications. Perhaps youโ€™d like to simplify your code a bit, this chapter will show you how to write certain portions of your code in Jython and others in Java so that you can make code as simple as possible.
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ how to call python from java
How to Call Python From Java | Baeldung
August 27, 2025 - 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.
Find elsewhere
๐ŸŒ
LinkedIn
linkedin.com โ€บ pulse โ€บ integrating-python-java-guide-developers-myexamcloud-7g5bc
Integrating Python and Java: A Guide for Developers
December 26, 2023 - Jython code can call Java libraries, and Java code can also use Python libraries. To integrate Jython in your Java project, you need to add the Jython JAR file to the project's classpath and then import the package "org.python.util.PythonInterpreter." Here's a simple Java code snippet to invoke a Python code and print its output:
๐ŸŒ
Medium
medium.com โ€บ @chamlinid โ€บ integrate-java-and-python-code-bases-1c4819fe19da
Integrate Java and Python code bases | by Chamlini Dayathilake | Medium
February 14, 2025 - The method used for integration: Using ProcessBuilder in Java. (java.lang.ProcessBuilder) One thing that you need to do when using this method is, simplify parameter values and returns as much as you can. [Do not use complex objects, rather define them as primitives or Strings]. The base story is trying to use powerful machine learning libraries from Python inside a Java program.
๐ŸŒ
SourceForge
jpype.sourceforge.net
JPype - Java to Python integration
Links: Python Java Jython/JPython ยท Similar Projects : JEP JPE
๐ŸŒ
YouTube
youtube.com โ€บ watch
Integrating Python and Java in Eclipse2 - YouTube
Python (specifically CPython) is heavily used in science, in part thanks to its fast powerful libraries such as numpy and scipy. It is still one of the best ...
Published ย  October 30, 2015
๐ŸŒ
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.
๐ŸŒ
O'Reilly
oreilly.com โ€บ library โ€บ view โ€บ beginning-python-r-using โ€บ 9780470414637 โ€บ ch21.html
21. Integrating Java with Python - Beginning Pythonยฎ: Using Python 2.6 and Python 3.1 [Book]
Using Java Classes in Jython21.7.2. Accessing Databases from Jython21.7.2.1. Working with the Python DB API21.7.2.2. Setting Up a Database21.7.3. Writing Java EE Servlets in Jython21.7.3.1. Setting Up an Application Server21.7.3.2. Adding the PyServlet to an Application Server21.7.3.3.
๐ŸŒ
DZone
dzone.com โ€บ coding โ€บ java โ€บ using python libraries in java
Using Python Libraries in Java
May 16, 2025 - Here are some practical examples for combining Python and Java: Data analysis and machine learning: Libraries like NumPy, Pandas, TensorFlow, or Scikit-Learn are leading in Python.
๐ŸŒ
Medium
medium.com โ€บ graalvm โ€บ supercharge-your-java-apps-with-python-ec5d30634d18
Supercharge Your Java Apps with Python | by Tim Felgentreff | graalvm
May 5, 2023 - Learn how you can use Python and Python libraries in your Java applications based on the example app with data visualization libraries. This article describes the essential parts of the setup and important patterns in the Java code to use Python in it effectively
๐ŸŒ
Python
wiki.python.org โ€บ moin โ€บ IntegratingPythonWithOtherLanguages
IntegratingPythonWithOtherLanguages
July 29, 2020 - p2j - Converts Python code to Java. No longer developed. ActiveState supports Python .NET. Python for .NET is a near-seamless integration of the CPython runtime with the .NET Common Language Runtime (CLR). IronPython is an implementation of Python for .net, which allows you to import .net class libraries seamlessly in Python.
๐ŸŒ
Java Code Geeks
javacodegeeks.com โ€บ home โ€บ core java
Mixing Java and Python: Building Polyglot Apps for AI and Data Science - Java Code Geeks
October 15, 2025 - JPype offers the best raw performance since it runs Java within the same process as Python using JNI (Java Native Interface). Py4J provides near-native performance with minimal integration effort and is ideal for data-intensive systems such as Apache Spark.
๐ŸŒ
SSSgram
analyticsinsight.net โ€บ home โ€บ latest news โ€บ integrating python with java: a detailed guide
Integrating Python with Java: A Detailed Guide
October 11, 2023 - Using Jython, a Python interpreter created in Java, is one well-liked technique. Jython enables smooth interaction with Java libraries by allowing Python code to run on the Java Virtual Machine (JVM).
๐ŸŒ
Quora
quora.com โ€บ How-can-I-integrate-a-Java-application-with-a-Python-application
How to integrate a Java application with a Python application - Quora
Answer: Frankly most ways to somehow run Python directly from within JVM don't work. They are either not-quite-compatible (new release of your third party library can use python 2.6 features and will not work with Jython 2.5) or hacky (it will ...