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

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.

🌐
Jython
jython.org
Home | Jython
There is work towards a Python 3 in the project’s GitHub repository. Jython implementations are freely available for both commercial and non-commercial use. They are distributed with source code under the PSF License v2. Jython is complementary to Java and is especially suited for the following tasks:
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
Where to start with learning how to use API's?
And if you have a suggestion as to an easier API I could learn to use as my first one then that would be awesome too! Google Maps API is very easy. I recommend starting with the with the distance calculator web service API , then try experimenting with some of the JavaScript and Android APIs if you're feeling adventurous. More on reddit.com
🌐 r/learnprogramming
62
397
October 12, 2016
HackerRank - Array Pairs Problem Help
Note that you don't need to print the actual pairs - you can alter the array to optimize. You should be able to find the maximum element of the array in a single pass (O(n)). Then, for each element, you want to check with how many elements after it it would give a product of less than or equal that amount with. However, looking through all cases would result in O(n2) complexity, which is just slightly too much. Now, consider you reorder the array such that the smaller elements are at the front and the larger ones at the end - now, you can find the largest number such that when multiplied by the current one, it yields a number less than or equal to the maximum (note that a special case is needed for 0). If you reordered the array, you could then search for the position up to which all the elements are less than or equal to the maximum factor (using binary search) - that would be O(log n) per element or O(n log n) in total, which should fit nicely in the given bounds. (All the elements before that position should give a product less than or equal to the maximum, while all the ones after should give a product larger than the maximum). More on reddit.com
🌐 r/learnprogramming
3
1
March 11, 2020
Eclipse Java Github Copilot Integration!
Eclipse Java Github Copilot Integration! More on reddit.com
🌐 r/github
1
14
September 1, 2022
🌐
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%
🌐
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.

🌐
GitHub
github.com › manishpandey0626 › java-python-converter
GitHub - manishpandey0626/java-python-converter: A Gen AI-powered service that converts partial Java classes into clean, idiomatic Python 3 code using GitHub Copilot and OpenAI models. · GitHub
✅ Converts Java class skeletons (fields, constructors, and inner classes) ✅ Handles nested inner classes recursively with proper outer references · ✅ Supports class inheritance and interface translation to Python multiple inheritance · ✅ Preserves structure and naming in idiomatic Python style · ✅ Leverages both GitHub Copilot and OpenAI chat models for prompt-based code generation · ✅ Exposed via REST API for easy integration and testing
Author   manishpandey0626
🌐
GitHub
github.com › amemifra › Spring-Jython
GitHub - amemifra/Spring-Jython: Java integration of Python script and classes into a Spring Boot Web Application. Powered by Jython · GitHub
Java integration of Python script and classes into a Spring Boot Web Application. Powered by Jython - amemifra/Spring-Jython
Starred by 11 users
Forked by 11 users
Languages   Python 99.3% | Java 0.7%
Find elsewhere
🌐
GitHub
github.com › hpi-swa-lab › graalpython-java-example
GitHub - hpi-swa-lab/graalpython-java-example: Java and Python Integration Example for GraalVM · GitHub
This example demonstrates how to integrate Python on GraalVM with a Java application.
Starred by 21 users
Forked by 4 users
Languages   Java 64.4% | Python 35.6%
🌐
Readthedocs
jython.readthedocs.io › en › latest › JythonAndJavaIntegration
Chapter 10: Jython and Java Integration — Definitive Guide to Jython latest documentation
The Java language added scripting ... Jython code into our Java applications. Similarly, the PythonInterpreter can be used from within Java code to invoke Jython. Also keep an eye on projects such as Clamp (http://github.com/groves/clamp/tree/master) that are on the ...
🌐
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.
🌐
Medium
medium.com › graalvm › supercharge-your-java-apps-with-python-ec5d30634d18
Supercharge Your Java Apps with Python | by Tim Felgentreff | graalvm
May 5, 2023 - In this article we look at an example application developed by students at the Hasso Plattner Institute (HPI) in Potsdam which can be used as a template for using Python libraries from Java on GraalVM. The full source code is available at the GitHub repository of the HPI Software Architecture Group.
🌐
SourceForge
jpype.sourceforge.net
JPype - Java to Python integration
Links: Python Java Jython/JPython · Similar Projects : JEP JPE
🌐
GitHub
github.com › bcdev › jpy
GitHub - bcdev/jpy: A bi-directional Python-Java bridge used to embed Java in CPython or the other way round. · GitHub
August 5, 2021 - A bi-directional Python-Java bridge used to embed Java in CPython or the other way round. - bcdev/jpy
Starred by 188 users
Forked by 37 users
Languages   C 54.6% | Java 25.9% | Python 18.7%
🌐
GitHub
github.com › mohdzain98 › JavaPython-Integration
GitHub - mohdzain98/JavaPython-Integration: Running python code inside java
Running python code inside java. Contribute to mohdzain98/JavaPython-Integration development by creating an account on GitHub.
Author   mohdzain98
🌐
Robert Peng's Blog
mr-dai.github.io › embedding-jython
Embedding Python in Java using Jython - Robert Peng's Blog
September 10, 2018 - In the past few days, I managed to find a way to dynamically load and run Python code in a Java program. In this post, I will briefly explain how I achieve this.
🌐
GitHub
github.com › ghajba › JaPy
GitHub - ghajba/JaPy: Repository for Java and Python integration
Repository for Java and Python integration. Contribute to ghajba/JaPy development by creating an account on GitHub.
Starred by 2 users
Forked by 2 users
Languages   Java 79.3% | Python 20.7% | Java 79.3% | Python 20.7%
🌐
Baeldung
baeldung.com › home › java › how to call python from java
How to Call Python From Java | Baeldung
August 27, 2025 - In this tutorial, we’ve learned about some of the most popular technologies for calling Python code from Java. The code backing this article is available on GitHub.
🌐
GitHub
github.com › topics › jython
jython · GitHub Topics
... JPyxie is a Java integration for executing Python scripts efficiently, with support for REST, gRPC, Process API, JEP, Jython, and GraalPy. The project goal is to make Python integration as ...
🌐
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 - Py4J Official Documentation Learn ... https://www.py4j.org/ JPype GitHub Repository Explore the JPype project, which allows Python code to run Java classes natively using JNI....
🌐
GitHub
github.com › natural › java2python
GitHub - natural/java2python: Simple but effective library to translate Java source code to Python. · GitHub
Simple but effective library to translate Java source code to Python. - natural/java2python
Starred by 577 users
Forked by 246 users
Languages   Python 94.3% | GAP 2.9% | Java 2.7%
🌐
Python
wiki.python.org › moin › IntegratingPythonWithOtherLanguages
IntegratingPythonWithOtherLanguages
July 29, 2020 - Integrating Python, C and C++, presented at the ACCU conference by Duncan Booth (2003) Embedding Python in Multi-Threaded C/C++ Applications in LinuxJournal (2000) Related · AppsWithPythonScripting · Python4Delphi - Python for Delphi is a set of free components that wrap up the Python Dll into Delphi. (https://github.com/pyscripter/python4delphi) F2PY - Fortran to Python Interface Generator (http://cens.ioc.ee/projects/f2py2e/) PyFort - The Python-Fortran connection tool (http://pyfortran.sourceforge.net/) CLPython - Python implemented in Common Lisp ·