aster.cloud aster.cloud
  • /
  • Platforms
    • Public Cloud
    • On-Premise
    • Hybrid Cloud
    • Data
  • Architecture
    • Design
    • Solutions
    • Enterprise
  • Engineering
    • Automation
    • Software Engineering
    • Project Management
    • DevOps
  • Programming
    • Learning
  • Tools
  • About
  • /
  • Platforms
    • Public Cloud
    • On-Premise
    • Hybrid Cloud
    • Data
  • Architecture
    • Design
    • Solutions
    • Enterprise
  • Engineering
    • Automation
    • Software Engineering
    • Project Management
    • DevOps
  • Programming
    • Learning
  • Tools
  • About
aster.cloud aster.cloud
  • /
  • Platforms
    • Public Cloud
    • On-Premise
    • Hybrid Cloud
    • Data
  • Architecture
    • Design
    • Solutions
    • Enterprise
  • Engineering
    • Automation
    • Software Engineering
    • Project Management
    • DevOps
  • Programming
    • Learning
  • Tools
  • About
  • Programming
  • Tech

Jython: The Love Child of Python and Java

  • root
  • April 26, 2021
  • 5 minute read

A couple of weeks ago, I was working on a Java application that creates directories and files based on the parameters given. Like with all of my applications, I want to know what’s going on while they are running. This got me thinking about my previous article that talked about a small logging module I wrote in Python. This module contains different levels for logging that get inserted into a database. The problem is that it was written in Python and that obviously doesn’t mix with Java. Or does it?

It took less than five minutes of Googling, to discover Jython. In a nutshell, Jython is an implementation of Python that runs on Java. For the most part, it is compatible with Python 2.7. Keep in mind that Python 2.7 is no longer receiving support, but the Jython team is working to get it compatible with Python 3.


Partner with aster.cloud
for your next big idea.
Let us know here.



From our partners:

CITI.IO :: Business. Institutions. Society. Global Political Economy.
CYBERPOGO.COM :: For the Arts, Sciences, and Technology.
DADAHACKS.COM :: Parenting For The Rest Of Us.
ZEDISTA.COM :: Entertainment. Sports. Culture. Escape.
TAKUMAKU.COM :: For The Hearth And Home.
ASTER.CLOUD :: From The Cloud And Beyond.
LIWAIWAI.COM :: Intelligence, Inside and Outside.
GLOBALCLOUDPLATFORMS.COM :: For The World's Computing Needs.
FIREGULAMAN.COM :: For The Fire In The Belly Of The Coder.
ASTERCASTER.COM :: Supra Astra. Beyond The Stars.
BARTDAY.COM :: Prosperity For Everyone.

 

The Struggles

Initially, I was hoping to leverage the Python logging class that I had already written. Essentially, this would require importing the module, creating a class object, and then calling one of the class functions (Depending on which level of logging I wanted to do). It would have looked like something similar to this:

from my_logger import MyLogger
myLogger = MyLogger("project_creator", "mysql://<USER>:<PASSWORD>@<HOST>/<DATABASE>")
myLogger.LogInfo("This is some logging information.")

Using Jython, this would loosely translate to:

PythonInterpreter interp = new PythonInterpreter();
interp.exec("from my_logger import MyLogger");
PyInstance loggingObject = (PyInstance)interp.eval("MyLogging('project_creator', 'mysql://<USER>:<PASSWORD>@<HOST>/<DATABASE>')");
System.out.println(loggingObject.invoke("LogInfo()").__tojava__(String.class));

Writing the Java code seemed fairly quick and easy. With my hopes high, I started researching what needs to be done to get this project to build and run. It was after quite a lot of time and experimenting where I realized that my original plan wasn’t going to work. Before I realized this, I needed to get Jython installed first.

Read More  Huawei Launches 5G Microwave Long-Reach E-band Solution To Scale Up 5G Deployment

Ubuntu has a package in its store called Jython that can be used. However, this package is for Jython 2.5. All of the modern documentation recommends downloading the Jython 2.7 installer which will give you the most recent version. Once downloaded, to run the installer:

java -jar jython-installer-2.7.2.jar

Selecting the second option which is the Standard edition will install the .jar file that you need along with demos and documentation. I tried the ninth option, which is supposed to install just the .jar file failed during installation every time I tried. While installing, you will be prompted to specify which directory you want the .jar file to install to.

Once installed, I was able to run this command that, in theory, would build my Java application:

javac -cp ~/jython/jython.jar jythontest/*.java

Unfortunately, that was wishful thinking. It was here that I started seeing errors for missing Python modules. After more research, I learned that I need to install the missing modules for Jython using the .jar file that I installed. Normal Python uses “pip” to install packages. From the research, I’ve done, Jython did have something called “jip”, but documentation for that is scarce. It was a little bit of a pain, but I had to download the missing modules from PyPi and unzip them. The unzipped module directories contain a setup.py file Using a command similar to this, I was able to install them:

java -jar ~/jython/jython.jar setup.py install

All seemed well until I tried building the project for a second time. Again, it was another missing module (_mysql to be exact). This led to another round of research as to why my project wouldn’t build. As it turns out, the driver in the connection string I was using for SQLAlchemy was causing the issue. Since Jython was built using Java, it relies on the JDBC for connections to MySQL. In short, the JDBC (Java Database Connectivity)is an API (Application Programming Interface)that tells the client how it can connect to a database.

Read More  Streamline Open-Source Security Compliance On Kubernetes With Tanzu Application Catalog

JayDeBeApi and zxJDBC are a couple of modules that could be used to fix the problem, however, they don’t mix well with SQLAlchemy’s create_engine() function. By this, I mean when I tried using the recommended driver, “jdbc:mysql”, that the function fails to parse the URL. This is probably because these modules construct the connection in a different way than SQLAlchemy and are more designed for parameterized querying.

The last hope that I had was in the documentation found here. Reading through, it was very detailed and well written. There is an SQLAlchemy example, but it’s connecting to an Oracle database using the module zxoracle. After another trip to Google to look for something similar, I came across the Github repo for zxoracle. Scanning through this code, I think it would be possible to modify this code to be used for MySQL. In the end, I decided that I needed to alter the course I was taking with this project.

 

The Solution

It wasn’t an easy decision by any means to change the implementation plan. Instead of importing the logging class and calling its functions, I decided to create an API for the logging class and create a client class that Jython will use. Since I already intended on making an API for logging, this wasn’t any extra work. Below is what the client class looked like and the Jython code in Java to call it:

Looking at the client.py file, a very simple function was written that creates a POST request that gets sent to the logging API. A dictionary that contains the application name, log message, and log event type gets sent along with the request. The Jython code in jython.java admittedly looks a little weird. What it basically does is import the Python file, create a function object, and pass the necessary parameters to that function. Just like earlier, the command I had to run that builds the Java project was:

javac -cp ~/jython/jython.jar jythontest/*.java

At last, I was finally able to build without getting any errors. To run the project, this command was used. Note that because my project required command-line parameters, it was necessary to add it in (the part that says “python”).

java -cp ~/jython/jython.jar:. jythontest.StartingPoint "python"

Once the project had finished running, I checked the database to make sure my log event had been inserted.

I think it was safe to say that at this point, I finally had a working project.

Read More  Leveling Up Your Data Analysis Skills As A Student

 

Final Thoughts

Reflecting on the events that occurred while trying to get the project to work, made me realize that maybe Jython isn’t the approach I’m looking for. On previous Java projects, I used the ProcessBuilder class to run all sorts of scripts. I am also very aware that Python has its own methods for calling APIs. I guess I was just hoping that Jython would save me a little more development time. In the end, I don’t regret the experience I obtained. If you happen to know what I did wrong while trying to get the SQLAlchemy driver to work, or notice any other mistakes I might have made, feel free to leave a comment. Happy coding and cheers!

This article is republished from hackernoon.com


For enquiries, product placements, sponsorships, and collaborations, connect with us at [email protected]. We'd love to hear from you!

Our humans need coffee too! Your support is highly appreciated, thank you!

root

You May Also Like
Getting things done makes her feel amazing
View Post
  • Computing
  • Data
  • Featured
  • Learning
  • Tech
  • Technology

Nurturing Minds in the Digital Revolution

  • April 25, 2025
View Post
  • Tech

Deep dive into AI with Google Cloud’s global generative AI roadshow

  • February 18, 2025
Volvo Group: Confidently ahead at CES
View Post
  • Tech

Volvo Group: Confidently ahead at CES

  • January 8, 2025
zedreviews-ces-2025-social-meta
View Post
  • Featured
  • Gears
  • Tech
  • Technology

What Not to Miss at CES 2025

  • January 6, 2025
View Post
  • Tech

IBM and Pasqal Plan to Expand Quantum-Centric Supercomputing Initiative

  • November 21, 2024
Black Friday Gifts
View Post
  • Tech

Black Friday. How to Choose the Best Gifts for Yourself and Others, Plus Our Top Recommendations.

  • November 16, 2024
zedreviews-Apple-iPhone-16-Pro-finish-lineup-240909
View Post
  • Featured
  • Gears
  • Tech
  • Technology
  • Tools

Apple debuts iPhone 16 Pro and iPhone 16 Pro Max

  • September 10, 2024
zedreviews-Apple-iPhone-16-Apple-Intelligence-240909
View Post
  • Featured
  • Gears
  • Tech
  • Technology

Apple introduces iPhone 16 and iPhone 16 Plus

  • September 10, 2024

Stay Connected!
LATEST
  • oracle-ibm 1
    Google Cloud and Philips Collaborate to Drive Consumer Marketing Innovation and Transform Digital Asset Management with AI
    • May 20, 2025
  • notta-ai-header 2
    Notta vs Fireflies: Which AI Transcription Tool Deserves Your Attention in 2025?
    • May 16, 2025
  • college-of-cardinals-2025 3
    The Definitive Who’s Who of the 2025 Papal Conclave
    • May 7, 2025
  • conclave-poster-black-smoke 4
    The World Is Revalidating Itself
    • May 6, 2025
  • oracle-ibm 5
    IBM and Oracle Expand Partnership to Advance Agentic AI and Hybrid Cloud
    • May 6, 2025
  • 6
    Conclave: How A New Pope Is Chosen
    • April 25, 2025
  • Getting things done makes her feel amazing 7
    Nurturing Minds in the Digital Revolution
    • April 25, 2025
  • 8
    AI is automating our jobs – but values need to change if we are to be liberated by it
    • April 17, 2025
  • 9
    Canonical Releases Ubuntu 25.04 Plucky Puffin
    • April 17, 2025
  • 10
    United States Army Enterprise Cloud Management Agency Expands its Oracle Defense Cloud Services
    • April 15, 2025
about
Hello World!

We are aster.cloud. We’re created by programmers for programmers.

Our site aims to provide guides, programming tips, reviews, and interesting materials for tech people and those who want to learn in general.

We would like to hear from you.

If you have any feedback, enquiries, or sponsorship request, kindly reach out to us at:

[email protected]
Most Popular
  • 1
    Tokyo Electron and IBM Renew Collaboration for Advanced Semiconductor Technology
    • April 2, 2025
  • 2
    IBM Accelerates Momentum in the as a Service Space with Growing Portfolio of Tools Simplifying Infrastructure Management
    • March 27, 2025
  • 3
    Tariffs, Trump, and Other Things That Start With T – They’re Not The Problem, It’s How We Use Them
    • March 25, 2025
  • 4
    IBM contributes key open-source projects to Linux Foundation to advance AI community participation
    • March 22, 2025
  • 5
    Co-op mode: New partners driving the future of gaming with AI
    • March 22, 2025
  • /
  • Technology
  • Tools
  • About
  • Contact Us

Input your search keywords and press Enter.