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. Answer from belayon40 on reddit.com
๐ŸŒ
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.

๐ŸŒ
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.
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. Answer from belayon40 on reddit.com
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.

๐ŸŒ
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.
๐ŸŒ
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:
Find elsewhere
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
Quora
quora.com โ€บ Can-Java-and-Python-be-used-together-in-any-interesting-projects-other-than-web-development-or-server-side-programming
Can Java and Python be used together in any interesting projects other than web development or server side programming? - Quora
Answer: Yes! The majority of the software that runs the primary computer/communications system of the United Statesโ€™ National Weather Service was written in Java. Some of the components have Python APIs. When I still worked for the NWS, I wrote, among many other things, a Python program that ...
๐ŸŒ
Evrone
evrone.com โ€บ home โ€บ blog โ€บ java versus python: when, why, and how to make the switch
How to switch from Java to Python and when it makes sense - Tips from Evrone
March 10, 2025 - If your project is enterprise-oriented in its scope, and has a heavy reliance on Java-specific infrastructure. Python components can be integrated into your Java program using tools like Jython or by using Python scripts in Java.
๐ŸŒ
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.
๐ŸŒ
Medium
medium.com โ€บ @kiarash.shamaii โ€บ integrating-a-simple-python-model-with-a-spring-boot-application-ddaf372831a7
Integrating a Simple Python Model with a Spring Boot Application | by kiarash shamaii | Medium
September 1, 2024 - public interface PythonService { public double predict(String input); } and two implementation for it with ApacheCommon and ProcssBuilder : package kia.shamaei.serverapp.service; import kia.shamaei.serverapp.controller.dto.PredictResponseDto; import org.apache.commons.exec.CommandLine; import org.apache.commons.exec.DefaultExecutor; import org.apache.commons.exec.PumpStreamHandler; import org.springframework.stereotype.Service; import java.io.BufferedReader; import java.io.ByteArrayOutputStream; import java.io.InputStreamReader; /** * Service class for making predictions using a Python script.
๐ŸŒ
Kotlin Discussions
discuss.kotlinlang.org โ€บ native
Integrating a python code with kotlin - Native - Kotlin Discussions
April 16, 2022 - Hello, I recently wrote a python program and I wanted to integrate the program with my Kotlin application and anyone could tell me how can I do that ?
๐ŸŒ
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.
๐ŸŒ
codecentric
codecentric.de โ€บ en โ€บ knowledge-hub โ€บ blog โ€บ java-classes-python
How to use Java classes in Python
November 15, 2021 - But this comes with a few drawbacks for the usage of native Python libraries โ€” since we will use numpy and tensorflow, this is not an option for us. Py4J is on the opposite side of the spectrum. It starts a socket in the Java code, over which it can communicate with Python programs.
๐ŸŒ
DaniWeb
daniweb.com โ€บ programming โ€บ software-development โ€บ threads โ€บ 173851 โ€บ integrating-python-script-with-java-application
integrating python script with Java application | DaniWeb
February 6, 2009 - Jython is slower than Python. Hence, I am hesitant to use Jython as top layer program which can call the python script and the java application sequentially. And SWIG is for the integration of C/C++ Applications with scripting languages like python/perl and also with java