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  Countries Discuss Digital Tech, Sustainable Development At "Connected For Shared Prosperity" Forum

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  How Linkerd Retries HTTP Requests With Bodies

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  Qualcomm And Jacoti Optimize Hearing Experience For True Wireless Earbud Users

 

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
  • 1
    Apple services deliver powerful features and intelligent updates to users this autumn
    • June 11, 2025
  • By the numbers: Use AI to fill the IT skills gap
    • June 11, 2025
  • 3
    Crayon targets mid-market gains with expanded Google Cloud partnership
    • June 10, 2025
  • Apple-WWDC25-Apple-Intelligence-hero-250609 4
    Apple Intelligence gets even more powerful with new capabilities across Apple devices
    • June 9, 2025
  • Apple-WWDC25-Liquid-Glass-hero-250609_big.jpg.large_2x 5
    Apple introduces a delightful and elegant new software design
    • June 9, 2025
  • Robot giving light bulb to businessman. Man sitting with laptop on money coins flat vector illustration. Finance, help of artificial intelligence concept for banner, website design or landing web page 6
    FinOps X 2025: IT cost management evolves for AI, cloud
    • June 9, 2025
  • 7
    AI security and compliance concerns are driving a private cloud boom
    • June 9, 2025
  • 8
    Apple supercharges its tools and technologies for developers to foster creativity, innovation, and design
    • June 9, 2025
  • 9
    It’s time to stop debating whether AI is genuinely intelligent and focus on making it work for society
    • June 8, 2025
  • cookies-food-photographer-jennifer-pallian-OfdDiqx8Cz8-unsplash 10
    What is a cookie?
    • June 6, 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
  • person-working-html-computer 1
    8 benefits of AI as a service
    • June 6, 2025
  • 2
    Cloud breaches are surging, but enterprises aren’t quick enough to react
    • June 6, 2025
  • 3
    Where is the cloud headed?
    • June 6, 2025
  • 4
    Enterprises are keen on cloud repatriation – but not for all workloads
    • June 4, 2025
  • 5
    The Summer Adventures : Hiking and Nature Walks Essentials
    • June 2, 2025
  • /
  • Technology
  • Tools
  • About
  • Contact Us

Input your search keywords and press Enter.