Dynamic linker API for the Java platform (JEP 276) in JDK 9 Early Access defines dynamic linker API for the Java platform. Using this API, different language runtimes implemented for the Java platform can interoperate with each other.

Nashorn JavaScript engine has been a part of JDK platform since JDK 8. Jython is an implementation of the Python programming language designed to run on the Java platform. In this blog entry, I'll demonstrate

seamless access of Python objects from a Nashorn script!



I downloaded Jython 2.7 standalone jar from jython.org.

I downloaded Jython 2.7 standalone jar from jython.org. In Jython, all objects known to the Jython runtime are represented by instances of the class PyObject or one of its subclasses.

So, we've to write a pluggable dynalink linker that can link objects of PyObject and subclasses for easier access. JythonLinkerExporter.java

So, we've to write a pluggable dynalink linker that can link objects of PyObject and subclasses for easier access. JythonLinkerExporter.java dynalink uses java.util.ServiceLoader mechanism to load

pluggable linkers. We've to write a META-INF/services file jdk.dynalink.linker.GuardingDynamicLinkerExporter and put it linker jar.

pluggable linkers. We've to write a META-INF/services file jdk.dynalink.linker.GuardingDynamicLinkerExporter and put it linker jar. I wrote a nashorn script to compile the linker source and make a jar jython_linker.js

In addition, jython_linker.js also runs a sample mixed language script in which Nashorn script accesses Python objects - jython_sample.js

Steps:

My directory structure looks as follows:



jjs jython_linker.js



With the above files in place, we just to have run the sample with this command:

The output looks as follows:

With Dynalink, different language runtimes can interoperate with each other! Have fun with mixed language programming on the Java platform!