Integrating Python with Java

Share
  • April 21, 2020

Generally speaking, combining system programming and scripting languages is no strange new concept, it just isn’t commonly done. It is possible for programming languages that share a common application binary interface to be combined into a single library or executable, and while it may be a bit difficult to get two languages’ signatures inside each other, tools do exist to help.

One example of programming languages working together can be found in polyglot code. For example, the 404 page on Stack Overflow can print “404” in Python, Ruby, Perl, C, and a few others.

As for Python and Java specifically, these two languages actually complement each other. You could for example use one for typical subtasks, while using the other as a scripting language for expanding the customization of the application you’re developing.

SEE ALSO: Python Package Index now has automatic malware checks on board

While JavaScript is not commonly used outside of web browsers, at least in comparison to other scripting languages like Perl, Tcl, Visual Basic, and Python, all of these languages are commonly paired with other languages. Python traditionally goes alongside C and C++, whereas Visual Basic is often the choice scripting language for C++ on the Windows platform, and both Perl and Tcl are typically used on C / Unix.

Python is used a bit more on Windows, as it is seamlessly integrated with the Microsoft Scripting Host architecture, as well as COM. It’s also great on Mac as well, with a good selection of native MacOS toolboxes.

For more resources on Python and the possibilities of Java integration, it’s a good idea to check out online courses for Python.

Why Java and Python?

Python is an object-oriented scripting language, which automatically makes it a good pair for Java. But when combined with a Python interpreter written entirely in Java, like Jython, you could do things like write entire applets in Python which can run in any JDK-compliant browser, with code execution nearly as fast as C / CPython.

What a Python-Java interpreter like Jython does is translate Python source code directly into Java bytecode, which gives it incredible speed as it isn’t a Python interpreter written in Java, which would be much slower. What other Java scripting solutions typically do (Java/TCL, Java/Perl, etc) is attach JVM to the C implementation in those languages, which not only introduces portability issues, but isn’t as seamless as one would hope.

How can you best use Java and JPython together?

There are several reputable tools that implement Python in Java or vice versa, so that you can execute one language’s commands in the other. Here is a quick list of some of the top-rated Python + Java integration tools:

  • Jython – Python implemented in Java
  • JPype – Allows Python to run java commands
  • Jepp – Java embedded Python
  • JCC – a C++ code generator for calling Java from C++/Python
  • Javabridge – a package for running and interacting with the JVM from CPython
  • py4j – Allows Python to run java commands.
  • voc – Part of BeeWare suite. Converts python code to Java bytecode.
  • p2j – Converts Python code to Java. No longer developed.

There are several approaches to using these tools, with their own benefits and drawbacks. So for example, you could build a prototype of the entire application in Jython, and after several testing and redesign cycles, rewrite everything in Java. This allows you to take advantage of increased flexibility and development speed of the scripting languages in the early part of your project, and your final product should be a bit more fleshed-out than if you had simply started in just Java.

And if you’re worried about user interface libraries, Jython can hook into the same UIL’s as basic Java, so the final Java rewrite should go without a hitch.

However, it’s not quite as simple if you’re trying to build a larger system. You’ll probably encounter the drawback of different components and layers having independent development cycles, such as top-level components evolving while lower layers become frozen. Of course, you could rewrite the individual components in Java as you approach design stability, or simply write some of your components exclusively in Java from the beginning.

SEE ALSO: Python data visualization with Bokeh

Another option is to focus on rewriting only the components where performance is critical. So you could leave high-level components in Jython, which means only the lower-level components would need to be rewritten – and in some cases, you may not need to rewrite anything at all.

So if parts of your released system are written in Jython, end-users can write their own customizations through Jython scripts – not to mention that Jython apps can be distributed as Java bytecode, which means you don’t really have to worry about the end-user screwing up components that shouldn’t be messed with.

The post Integrating Python with Java appeared first on JAXenter.

Source : JAXenter