Dogs and cats may be good with with each other, but what about programming languages?

Can programming languages talk to each other? — A Java fanatic’s perspective

Rahul Pillai
4 min readJun 29, 2021

Hello readers, hope this pandemic has been treating you all well! It’s been a while since I’ve written and here I’m back at it.

Note: In course of this article, you’d find a bunch of occurrences of the term GTK which isn’t NASA’s new spacecraft but a framework that exposes a rich set of APIs to build modern user interfaces (typically on Linux).

More often than not, I’ve found the need to interface with another disparate programming language from my current application. Very succinctly, every language shines bright in it’s territory and is preferred to another for easily and efficiently completing tasks that other languages would likely have you run around for. Incidentally, writing a GTK application in Java — one of the most powerful languages with excellent innate GUI support — is, at best, horrendous primarily because there aren’t official GNOME bindings for for it. Meanwhile, Python is the showstopper in this department and your Java program had better interface with a Python script for seamless interplay with GTK. Moreover, even my colleague at workplace had recently asked me if it’s possible to integrate a Python function within her Vue.js web application. While there’s more than one way to get there, we’ll take a look at two broad approaches.

RESTful Web Services

Though the title might not sound quite germane to the context we’re operating in, you might be surprised to know that this is one of the most candid take on the subject. This could be the de facto solution to web developers hoping to interface with other programming languages. With support from versatile frameworks, it barely takes 3–4 lines of code to build a simple RESTful web service that would accept GET requests and return JSON responses. For instance, Javalin is a straightforward library that allows setting up a web service with just a few lines of code. What Javalin is to Java, is Flask to Python and HTTP to Node.js. Regardless of the nomenclature, in all cases, we’re talking about setting up a simple API endpoint at localhost that would be consumed by the primary application in question.

More generally, this is how web applications interact with resources outside the browser’s sandbox to achieve whatever a puny web app alone cannot. Lets say you’re working on one such web app that promises to cleanup config files on a user’s computer. Surmising from above, given the limited access bestowed on browsers, we’d take the web service route and setup one with Javalin as follows. The web app will then only have to make a GET request at http://localhost:7000 to get the job done:

// Java
Javalin app = Javalin.create().start(7000);
app.get("/", new Handler() {
@Override
public void handle(@NotNull Context context) throws Exception {
// Do cleanup activity ...
context.result("Cleanup success!");
}
});

Thus, a successful cleanup operation precipitates in a message echoed back to our web app which can then present it nicely to the user.

To the full-stack developers out there, this idea might’ve sounded rather banal since, come on, who doesn’t know about client-server communication through RESTful web services? Well, what’s less obvious, though, is that this well established architecture doesn’t really ring a bell to many of us since it doesn’t directly address the question “hey, how should I call this Python OCR function in my web app?” as much as it addresses “yo, how could I save input from this questionnaire form?”.

Ultimately, this is definitely one of the more commonplace approaches to achieve interaction between distinct programming languages.

Processes

The term might sound quite familiar to the operating system zealot in you. The big idea here is that most high level programming languages can create and manage processes quite efficiently. For instance, while Java brandishes the Runtime sword, Python aces it with subprocess and Node.js slays it with ChildProcess. While the names are evidently different, their purpose of existence is unilateral. They are all about spawning command-line sessions on the underlying operating system and issuing commands. Now, here’s where the true power of CLI and args can be potentially exploited since, technically, everything on a computer finds its inception in the command-line — that’s where every piece of software — including the browsers, games, etc. — starts! More simply put, we run all our Java, Python, etc. programs from the command-line. For starters, PowerShell for Windows, Bash for Linux and Terminal for MacOS are incarnations of the “command-line”. Therefore programming language support for command-line integration is all we need to run a piece of code in another language from our program.

While it’s not starkly different across languages, lets take a look at a Java code snippet that would run my show-gtk-dialog.py Python script that, well, opens a simple GTK dialog:

// Java
String msg = "HelloWorld! I'm from Java and Python.";
Process process = Runtime.getRuntime().exec("python show-gtk-dialog.py \"" + msg + "\"");
process.waitFor(); // waits until the dialog is closed by the user
System.out.println("Dialog closed!");

Very simply put, this snippet passes a command-line argument — the message to be displayed inside the GTK dialog — to the Python script which would then read it using sys.argv and use the GTK APIs to inflate a simple dialog with the message.

While the above example might appear inchoate, it illustrates the idea that processes and more specifically the command-line can, indeed, stage communication between programming languages.

Finally, it’s worth noting that neither approach is a one-size-fits-all as they have their respective preconditions, discussing which is beyond the scope of this article. Nevertheless, I hope this article aids in ideation of your cross-language project! Happy coding and take care.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Rahul Pillai
Rahul Pillai

Written by Rahul Pillai

A fervent software developer with a relentless thirst to learn more.

No responses yet

Write a response