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

Answer from Voo on Stack Overflow
🌐
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%
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.

Discussions

Using Python from within Java - Stack Overflow
That being said, there is a project to allow calling CPython from Java, called Jepp: ... I've never used any of these project, so I cant't vow for their quality. ... Jython is a Python implementation running on the JVM. You can find information about embedding Python in an existing Java app ... More on stackoverflow.com
🌐 stackoverflow.com
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
Running python 3 code in java
This is not what you asked for, but you can use operating system commands to run python scripts from java. More on reddit.com
🌐 r/java
38
40
May 21, 2019
Can I write code in obsidian?

You can execute python and many other languages using the Execute Code Plugin: https://www.reddit.com/r/ObsidianMD/comments/wct6z2/execute_code_in_your_notes_update/

More on reddit.com
🌐 r/ObsidianMD
14
11
October 9, 2020
🌐
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.
🌐
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())) Ready to get started? Head over to Downloads. Or you could read a quick overview of features specific to Jython. A more detailed introduction and reference can be found in the Jython Book. Jython is embedded in lots of projects.
🌐
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.

🌐
GraalVM
graalvm.org › python
GraalPy
Embed Python in JVM Applications · import org.graalvm.polyglot.Context; try (Context context = Context.newBuilder() .allowAllAccess(true) // See documentation for options .build()) { context.eval("python", "print('Hello from GraalPy!')"); } Use Python packages directly in Java, Kotlin, or Scala ·
🌐
pytz
pythonhosted.org › javabridge › java2python.html
Calling Python from Java — python-javabridge 1.0.12 documentation
The following functions let a Java caller refer to a Python value (which can be a base type or an object) via a token which may be exchanged for the value at any time. The Java code is responsible for managing the reference’s lifetime. Example: import javabridge cpython = javabridge.JClassWrapper('org.cellprofiler.javabridge.CPython')() d = javabridge.JClassWrapper('java.util.Hashtable')() result = javabridge.JClassWrapper('java.util.ArrayList')() d.put("result", result) cpython.execute( 'import javabridge\n' 'x = { "foo":"bar"}\n' 'ref_id = javabridge.create_and_lock_jref(x)\n' 'javabridge.JWrapper(result).add(ref_id)', d, d) cpython.execute( 'import javabridge\n' 'ref_id = javabridge.to_string(javabridge.JWrapper(result).get(0))\n' 'assert javabridge.redeem_jref(ref_id)["foo"] == "bar"\n' 'javabridge.unlock_jref(ref_id)', d, d)
Find elsewhere
🌐
Jython
jython.org › jython-old-sites › archive › 21 › docs › embedding.html
Embedding Jython
There are several options for embedding Jython in a Java application. Sometimes the nicest approach is to make a real Java class out of a Python class and then just use that Python class from Java code. The simplest approach to embedding Jython is to use the PythonInterpreter object · JavaDoc ...
🌐
GitHub
github.com › bytedeco › javacpp-embedded-python
GitHub - bytedeco/javacpp-embedded-python: With this library, you can embed Python to your Java or Scala project. The main purpose of this library is to use Python libraries from Java or Scala.
With this library, you can embed Python to your Java or Scala project. The main purpose of this library is to use Python libraries from Java or Scala. - bytedeco/javacpp-embedded-python
Starred by 12 users
Forked by 5 users
Languages   Java 100.0% | Java 100.0%
🌐
O'Reilly
oreilly.com › library › view › python-programming-with › 0201616165 › 0201616165_app04lev1sec5.html
Embedding Jython in Java - Python Programming with the Java™ Class Libraries: A Tutorial for Building Web and Enterprise Applications with Jython [Book]
Embedding Jython in Java What follows is an example of embedding Jython in the Java code that ships with Jython's standard distribution. I've added comments. import org.python.util.PythonInterpreter; import org.python.core.*; public … - Selection from Python Programming with the Java™ Class Libraries: A Tutorial for Building Web and Enterprise Applications with Jython [Book]
🌐
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 - So what are we going to do? What kind of solutions do we apply to this task? One of the working solutions is to run this service in docker. Or we can find another, more effective production-friendly solution: to execute python code directly from Java code via the Jep library (Java Embedded Python).
🌐
SourceForge
sourceforge.net › projects › jepp
Java Embedded Python download | SourceForge.net
August 15, 2014 - Download Java Embedded Python for free. Jepp (Java Embedded Python) embeds CPython in Java. It is stable and thread-safe, and is suitable for many different scripting needs.
🌐
Baeldung
baeldung.com › home › java › how to call python from java
How to Call Python From Java | Baeldung
August 27, 2025 - Continuing with Jython, we also have the possibility of embedding Python code directly into our Java code.
🌐
Tech
galina.tech.blog › 2022 › 07 › 11 › how-to-execute-python-modules-from-java
How To Execute Python Modules From Java. – A technical blog about Data Science
July 15, 2022 - So what are we going to do? What kind of solutions do we apply to this task? One of the working solutions is to run this service in docker. Or we can find another, more effective production-friendly solution: to execute python code directly from Java code via the Jep library (Java Embedded Python).
🌐
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.
🌐
Readthedocs
jython.readthedocs.io › en › latest › JythonAndJavaIntegration
Chapter 10: Jython and Java Integration — Definitive Guide to Jython latest documentation
import javax.script.ScriptEngine; ... '/System/Library/Java/Extensions/QTJava.zip' sys module 42 x: 4 · A similar technique to JSR-223 for embedding Jython is making use of the PythonInterpreter directly. This style of embedding code is very similar to making use of a scripting engine, but it has the advantage of working with Jython 2.5. Another advantage is that the PythonInterpreter enables you to make use of PyObjects directly. In order to make ...
🌐
TutorialsPoint
tutorialspoint.com › jython › jython_java_application.htm
Jython - Java Application
import org.python.util.PythonInterpreter; import org.python.core.*; public class SimpleEmbedded { public static void main(String []args) throws PyException { PythonInterpreter interp = new PythonInterpreter(); System.out.println("Hello, world from Java"); interp.execfile("hello.py"); interp.set("a", new PyInteger(42)); interp.exec("print a"); interp.exec("x = 2+2"); PyObject x = interp.get("x"); System.out.println("x: "+x); System.out.println("Goodbye "); } }
🌐
The SW Developer
theswdeveloper.com › post › java-python-jep
How to execute Python code in Java - The SW Developer
September 10, 2021 - In this tutorial we will show you how to integrate Java and Python code in an almost seamless manner with just a few easy steps.What is jep?Jep is an open source library which gives you the ability to execute Python code in Java and vice versa.Let's ...