Chapter 18. Using Java with Other Languages

18.0 Introduction

Java has several methods of running programs written in other languages. You can invoke a compiled program or executable script using Runtime.exec(), as I’ll describe in Recipe 18.1. There is an element of system dependency here, because you can only run external applications under the operating system they are compiled for. Alternatively, you can invoke one of a number of scripting languages (or dynamic languages)—running the gamut: awk, bsh, Clojure, Ruby, Perl, Python, Scala—​using javax.script, as illustrated in Recipe 18.3. Or you can drop down to C level with Java’s native code mechanism and call between Java and compiled functions written in C/C++; see Recipe 18.6 and Recipe 18.7. Java 22 improves on this with the Foreign Function & Memory Interface (FFM), as discussed in Recipe 18.5. From native code, you can call out to functions written in just about any language. Not to mention that you can contact programs written in any language over a socket (see Chapter 15), with HTTP services (see Chapter 15), or with Java clients in RMI or CORBA clients in a variety of languages.

There is a wide range of other JVM languages, including these:

  • BeanShell, a general scripting language for Java.

  • Groovy, a Java-based scripting language that pioneered the use of closures in the Java language ecosystem. It also has a rapid-development web package called Grails and a build tool called Gradle (see Recipe 2.5).

  • Jython

Get Java Cookbook, 5th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.