DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Using Lombok Library With JDK 23
  • Bridging JavaScript and Java Packages: An Introduction to Npm2Mvn
  • Simplifying Database Operations With HarperDB SDK for Java
  • Taming the Virtual Threads: Embracing Concurrency With Pitfall Avoidance

Trending

  • Immutable Secrets Management: A Zero-Trust Approach to Sensitive Data in Containers
  • From Zero to Production: Best Practices for Scaling LLMs in the Enterprise
  • AI's Dilemma: When to Retrain and When to Unlearn?
  • Why Database Migrations Take Months and How to Speed Them Up
  1. DZone
  2. Coding
  3. Java
  4. Using Python Libraries in Java

Using Python Libraries in Java

This article discusses how Python libraries can be integrated into Java by presenting real-world examples and Jython samples.

By 
Constantin Kwiatkowski user avatar
Constantin Kwiatkowski
·
May. 16, 25 · Tutorial
Likes (0)
Comment
Save
Tweet
Share
2.7K Views

Join the DZone community and get the full member experience.

Join For Free

Advantages and Disadvantages of Python and Java

Java and Python are among the most widely used languages in the world (see Figure 1 below).

I diagram that shows that Python and Java are widely used languages, followed distantly by JavaScript, C/C++, C#, R, and PHP.

Figure 1: Overview of programming languages from statista.com

Both languages have their strengths and weaknesses and are popular in different fields. Unlike other articles, such as those from  Tom Radcliffe which analyzes which language is technically better implemented, this article focuses on presenting practical use cases with explicit examples. Let us check the following table that provides a brief overview, of which language is best suited for which field (✅ = advantage, and ❌ = disadvantage). 

Use Cases Python Java

Web

✅

✅

Data Science / KI

✅✅✅

❌

Enterprise Software

❌

✅✅✅

Apps (Android)

❌

✅

Games

✅

✅

APIs / Microservices

✅

✅✅

Prototypes

✅✅✅

❌

High-Performance Systems

❌

✅✅✅


Combining Python and Java For Better Results

In practice, more and more use cases are emerging where both languages are combined to produce better results. For example, in a microservices architecture:

  • Java is used for core business logic and high-performance services (e.g., with Spring Boot).
  • Python is used for data processing microservices or small utility tools.

All services operate independently, allowing both languages to be used in parallel without issues and interruptions.

The following table provides a brief overview of when and how Java should be combined with Python.

situation recommendation

A lot of data

✅ Python + Java

Performance

✅ Java as the base

KI/ML

✅ Python for models

Architecture

✅ Microservices


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.
  • Rapid prototyping: Python enables faster development of prototypes, which can later be integrated into Java.
  • Reusing existing codebases: Companies often use existing Python modules integrated into new Java applications.

Approaches to Integrating Python Into Java 

Jython

Jython is an implementation of Python that runs on the Java Virtual Machine (JVM). This allows Python modules to be used directly in Java.

Advantages of Jython:

  • Seamless integration with the JVM
  • Direct access to Java classes from Python and vice versa

Disadvantages of Jython:

  • Supports only Python 2.x (no support for Python 3.x)
  • Many modern libraries do not work because they require Python 3.x

ProcessBuilder (External Script Execution)

A simple, yet effective way to use Python libraries in Java is by running Python scripts as external processes.

Advantages of ProcessBuilder:

  • Full support for all Python versions and libraries
  • There are no dependencies on bridges or special APIs

Disadvantages of ProcessBuilder:

  • Performance overhead due to process start
  • Error handling is more complex

Py4J

Py4J is another bridge between Python and Java. Originally developed for Apache Spark, it allows Python to manipulate Java objects and execute Java methods.

Advantages of Py4J:

  • Lightweight and easy to integrate
  • Supports bidirectional communication

Disadvantages of Py4J:

  • There is more overhead during setup
  • Better suited for scenarios where Python is the driver

Alternatives: Java Libraries as Replacements for Python 

If the integration is too complicated, many powerful Java libraries offer similar functionalities to Python libraries:

Python Library Java Alternative Use case
NumPy ND4J, Apache Commons Math Calculation
Pandas Tablesaw, Apache Arrow Data analysis
Scikit-Learn Weka, Deeplearning4j MML
TensorFlow TensorFlow for Java, DL4J Deep Learning
Matplotlib/Seaborn JFreeChart, XChart Data visualization


Jython Examples for a Maven Project 

You need to include the Jython dependency in your pom.xml to use Jython in a Maven project. The latest version of Jython can be downloaded from https://www.jython.org/download and included as a JAR file. Alternatively, the library can be integrated via dependency management. To do this, the project's POM file must be extended as follows, using version 2.7.2: 

Plain Text
 
<dependency>
    <groupId>org.python</groupId>
    <artifactId>jython-standalone</artifactId>
    <version>2.7.2</version>
</dependency>


We will create a simple example where a Python script performs a calculation, and the result is processed further in Java.

Python code (inline within Java code):

Java
 
import org.python.util.PythonInterpreter;
import org.python.core.PyObject;

public class JythonExample {
    public static void main(String[] args) {
        PythonInterpreter interpreter = new PythonInterpreter();
		interpreter.exec(
            "def add_numbers(a, b):\n" +
            "    return a + b\n" +
            "\n" +
            "result = add_numbers(10, 20)"
        );

        PyObject result = interpreter.get("result");
		int sum = result.asInt();
        System.out.println("Result: " + sum);
    }
}


In this example, the Python interpreter is first initialized:  

Java
 
PythonInterpreter interpreter = new PythonInterpreter();


And then the Python code is executed directly executed within the Java class:

Java
 
interpreter.exec(
            "def add_numbers(a, b):\n" +
            "    return a + b\n" +
            "\n" +
            "result = add_numbers(10, 20)"
        );


Here, a simple Python function add_numbers is defined, which adds two numbers. The function is called with the values 10 and 20, and the result is stored in the variable result.

The result is retrieved with the following command:

Java
 
PyObject result = interpreter.get("result");
int sum = result.asInt();


With interpreter.get("result"), the result from the Python script is retrieved and converted into a Java variable (int).

The result can, for example, be output as follows:

Java
 
System.out.println("Result: " + sum);


Java variables can also be passed to the Python script. 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.

CSS
 
my-maven-project/
│
├── pom.xml
│
├── src
│   ├── main
│   │   ├── java
│   │   │   └── your/package/main.java
│   │   └── resources
│       └── script.py  
│
└── target/


Why src/main/resources?

  • Everything in the src/main/resources directory is copied to the target/classes folder when the project is built
  • You can load these files at runtime using the ClassLoader

This is a Maven convention commonly used in Java projects to store non-Java resources like configuration files, properties, XML, JSON, or other static data needed at runtime.

The script is defined as follows:

Python
 
def add_numbers(a, b):
    return a + b
result = add_numbers(5, 15)
print("Result: {}".format(result))


The corresponding Java class is defined as follows:

Java
 
import org.python.util.PythonInterpreter;
import java.io.FileReader;
import java.io.IOException;

public class JythonExternalScriptExample {
    public static void main(String[] args) {
        
        PythonInterpreter interpreter = new PythonInterpreter();
        try {
            FileReader pythonScript = new FileReader("script.py");
            interpreter.execfile(pythonScript);
            pythonScript.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}


Initialization of the interpreter:

Java
 
PythonInterpreter interpreter = new PythonInterpreter();


Loading the external script:

Embedded Javascript
 
FileReader pythonScript = new FileReader("script.py");


Executing the script:  

Java
 
interpreter.execfile(pythonScript);


Closing the FileReader:  

Java
 
pythonScript.close();


Conclusion 

With Jython, you can easily execute external Python scripts from Java. The integration works well for simple scripts, especially when passing parameters from Java to Python. However, for more complex Python 3 libraries, you should consider alternatives like Py4J or directly executing Python processes using ProcessBuilder.

Jython offers a simple but outdated solution, while Py4J and JPype create more flexible bridges between the two languages. However, executing external Python scripts is the simplest method in many cases.

For long-term projects or when performance is a major concern, it may be worth looking for Java alternatives that offer native support and can be more easily integrated into existing Java projects.

Apache Maven Jython Java (programming language) Python (language)

Opinions expressed by DZone contributors are their own.

Related

  • Using Lombok Library With JDK 23
  • Bridging JavaScript and Java Packages: An Introduction to Npm2Mvn
  • Simplifying Database Operations With HarperDB SDK for Java
  • Taming the Virtual Threads: Embracing Concurrency With Pitfall Avoidance

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: