🌐
Jython
jython.org
Home | Jython
The seamless interaction between ... 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 implementation of the Python interpreter
Jython Logo
Jython is an implementation of the Python programming language designed to run on the Java platform. It was known as JPython until 1999. Jython programs can import and use any Java class. … Wikipedia
Factsheet
Initial release January 17, 2001; 25 years ago (2001-01-17)
Stable release 2.7.4
/ 18 August 2024; 19 months ago (18 August 2024)
Written in Python and Java
Factsheet
Initial release January 17, 2001; 25 years ago (2001-01-17)
Stable release 2.7.4
/ 18 August 2024; 19 months ago (18 August 2024)
Written in Python and Java
🌐
Tabnine
tabnine.com › home page › code › java › org.python.util.pythoninterpreter
Java Examples & Tutorials of PythonInterpreter.get (org.python.util) | Tabnine
PythonInterpreter interpreter = new PythonInterpreter(); // Append directory containing module to python search path and import it interpreter.exec("import sys\n" + "sys.path.append(pathToModule)\n" + "from bar import foo"); PyObject meth = ...
🌐
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 "); } }
🌐
Baeldung
baeldung.com › home › java › how to call python from java
How to Call Python From Java | Baeldung
August 27, 2025 - @Test public void givenPythonInterpreter_whenNumbersAdded_thenOutputDisplayed() { try (PythonInterpreter pyInterp = new PythonInterpreter()) { pyInterp.exec("x = 10+10"); PyObject x = pyInterp.get("x"); assertEquals("x: ", 20, x.asInt()); } } In ...
🌐
GitHub
github.com › certik › jython › blob › master › src › org › python › util › PythonInterpreter.java
jython/src/org/python/util/PythonInterpreter.java at master · certik/jython
import java.util.*; · /** * The PythonInterpreter class is a standard wrapper for a Jython · * interpreter for use embedding in a Java application. * * @author Jim Hugunin · * @version 1.0, 02/23/97 · */ · public class PythonInterpreter { PyModule module; protected PySystemState systemState; PyObject locals; ·
Author   certik
🌐
Java Tips
javatips.net › api › org.python.util.pythoninterpreter
Java Examples for org.python.util.PythonInterpreter
PySystemState.add_extdir(jarDir); } } PythonInterpreter interpreter = new PythonInterpreter(); interpreter.exec(script); return null; } ... /** * The main thread for this class invoked by Thread.run() * * @see java.lang.Thread#run() */ public void run() { PythonInterpreter p = new ...
Find elsewhere
🌐
Readthedocs
jython.readthedocs.io › en › latest › JythonAndJavaIntegration
Chapter 10: Jython and Java Integration — Definitive Guide to Jython latest documentation
In the constructor, a new instance of the PythonInterpreter is created which we then utilize the interpreter to obtain a reference to our Jython object and stores it into our PyObject. Next, there is a static method named create that will be called in order to coerce our Jython object into Java and return the resulting class.
🌐
MIT
web.mit.edu › jython › jythonRelease_2_2alpha1 › Doc › javadoc › org › python › util › PythonInterpreter.html
PythonInterpreter (Jython API documentation)
public static void initialize(java.util.Properties preProperties, java.util.Properties postProperties, java.lang.String[] argv) Initialize the jython runtime. This method should only be called once, and should be call before any other python objects are created (included a PythonInterpreter).
🌐
Tabnine
tabnine.com › home page › code › java › org.python.util.pythoninterpreter
Java Examples & Tutorials of PythonInterpreter.set (org.python.util) | Tabnine
PythonInterpreter py = new PythonInterpreter(); String dataFolder,prepFolder; py.execfile("filename.py"); py.set("df", dataFolder); py.set("pf", prepFolder); py.exec("prep = Preprocess(df, pf)"); //if the preprocess method does not return anything, ...
🌐
Program Creek
programcreek.com › java-api-examples
org.python.util.PythonInterpreter Java Examples
/** * The main thread for this class invoked by Thread.run() * * @see java.lang.Thread#run() */ public void run() { PythonInterpreter p = new PythonInterpreter(); for (String name : this.locals.keySet()) { p.set(name, this.locals.get(name)); } URL jarUrl = JythonServer.class.getProtectionD...
🌐
DZone
dzone.com › coding › java › using python libraries in java
Using Python Libraries in Java
May 16, 2025 - Here is an extended example: Java · import org.python.util.PythonInterpreter; import org.python.core.PyInteger; public class JythonExampleWithInput { public static void main(String[] args) { PythonInterpreter interpreter = new PythonInterpreter(); int a = 15; int b = 25; interpreter.set("a", new PyInteger(a)); interpreter.set("b", new PyInteger(b)); interpreter.exec( "def multiply_numbers(x, y):\n" + " return x * y\n" + "\n" + "result = multiply_numbers(a, b)" ); int result = interpreter.get("result").asInt(); System.out.println("Result: " + result); } } In the next example, an external Python file named script.py is placed in the src/main/resources directory of the Maven project.
🌐
Tabnine
tabnine.com › home page › code › java › org.python.util.pythoninterpreter
Java Examples & Tutorials of PythonInterpreter.execfile (org.python.util) | Tabnine
public boolean before(Authentication authentication, MethodInvocation mi, PreInvocationAttribute preAttr) { PythonInterpreterPreInvocationAttribute pythonAttr = (PythonInterpreterPreInvocationAttribute) preAttr; String script = pythonAttr.getScript(); PythonInterpreter python = new PythonInterpreter(); python.set("authentication", authentication); python.set("args", createArgumentMap(mi)); python.set("method", mi.getMethod().getName()); Resource scriptResource = new PathMatchingResourcePatternResolver() .getResource(script); try { python.execfile(scriptResource.getInputStream()); } catch (IOException e) { throw new IllegalArgumentException("Couldn't run python script, " + script, e); } PyObject allowed = python.get("allow"); if (allowed == null) { throw new IllegalStateException("Python script did not set the permit flag"); } return (Boolean) Py.tojava(allowed, Boolean.class); }
🌐
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 - Four lines of code, and we have a python3.9 Interpreter in Java! Now you will see how to run the Python functions from the document you’ve already seen. Example of how to run python3.9 functions from Java Jep
🌐
Tabnine
tabnine.com › home page › code › java › org.python.util.pythoninterpreter
org.python.util.PythonInterpreter.exec java code examples | Tabnine
PythonInterpreter py = new PythonInterpreter(); String dataFolder,prepFolder; py.execfile("filename.py"); py.set("df", dataFolder); py.set("pf", prepFolder); py.exec("prep = Preprocess(df, pf)"); //if the preprocess method does not return anything, ...
🌐
GitHub
github.com › jythontools › jython › blob › master › src › org › python › util › PythonInterpreter.java
frozen-mirror/src/org/python/util/PythonInterpreter.java at master · jython/frozen-mirror
* The PythonInterpreter class is a standard wrapper for a Jython interpreter for embedding in a · * Java application. */ public class PythonInterpreter implements AutoCloseable, Closeable { · // Defaults if the interpreter uses thread-local state ·
Author   jython
🌐
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 ...
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.

🌐
GitHub
github.com › ninia › jep
GitHub - ninia/jep: Embed Python in Java · GitHub
Jep embeds CPython in Java through JNI.
Starred by 1.5K users
Forked by 163 users
Languages   C 52.1% | Java 30.2% | Python 17.7%