28

Java has a ScriptEngine system that allows you to run/evaluate statements in a different language.
I know for a fact that JavaScript is supported, but I couldn't find any other languages to work with it.
Is, for example, Ruby implemented?

5 Answers5

17

Here is a script to determine all languages on your system:

import java.util.List;
import javax.script.ScriptEngineManager;
import javax.script.ScriptEngine;
import javax.script.Bindings;
import javax.script.ScriptContext;
import javax.script.ScriptEngineFactory;


public class Test {

public static void main(String[] args)
{
    ScriptEngineManager mgr = new ScriptEngineManager();
    List<ScriptEngineFactory> factories = mgr.getEngineFactories();
    for (ScriptEngineFactory factory : factories)
    {
        System.out.println("ScriptEngineFactory Info");
        String engName = factory.getEngineName();
        String engVersion = factory.getEngineVersion();
        String langName = factory.getLanguageName();
        String langVersion = factory.getLanguageVersion();
        System.out.printf("\tScript Engine: %s (%s)\n", engName, engVersion);
        List<String> engNames = factory.getNames();
        for (String name : engNames)
        {
            System.out.printf("\tEngine Alias: %s\n", name);
        }
        System.out.printf("\tLanguage: %s (%s)\n", langName, langVersion);
        }
    }

}

Hope this helps.

hjk321
  • 437
  • 1
  • 5
  • 10
12

..I know for a fact that JavaScript is supported,..

ECMAscript, technically.

.. but I couldn't find any other languages to work with it. Is, for example, Ruby implemented?

No. The ECMAscript engine is the only one included by default, the last time I heard.

Update

The comments of Pointy below, suggest that the Nashorn engine has been deprecated and will be removed 'soon'.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • accepting the answer as the most straightforward one with all question answered. +1 on mentioning ECMAscript (I called it JS for simplicity and because Java calls it that (see http://docs.oracle.com/javase/7/docs/api/javax/script/ScriptEngine.html ) –  Nov 18 '13 at 08:47
  • 1
    Even the JavaScript version typically shipped in JREs is [not required](http://stackoverflow.com/questions/6089773/javascript-engine-can-not-be-found-scripting-for-the-java-platform/6089959) and you should have low expectations about compatibility across versions. – McDowell Dec 14 '13 at 11:23
  • On Mac OS X, you may also have AppleScript, depending on your OS and Java version. For whatever reason, in some cases you have to instantiate `apple.applescript.AppleScriptEngineFactory` directly and call the `getScriptEngine()` method, since `javax.script.ScriptEngineManager.getEngineByName()` doesn't always know about AppleScript – Simon Kissane Sep 14 '16 at 10:41
  • 2
    Note that JavaScript support is deprecated and will go away with (I think) release 13 of the JDK/JVM. – Pointy Aug 03 '20 at 19:11
  • @Pointy Huh.. I found [this](https://docs.oracle.com/en/java/javase/11/docs/api/jdk.scripting.nashorn/module-summary.html) which suggests you're right, but it doesn't cover interesting things like .. Why? When? [Of interest to me, given some methods of classes were deprecated in Java 1.2, but are still in the API, whereas all the applet / webstart functionality was deprecated then removed within 2(?) API versions.] Will it be replaced with a different ECMAscript engine? Will/have other engines be/been supported? .. If you can track down further links, please share. – Andrew Thompson Aug 03 '20 at 23:57
  • 2
    Well I am the "canary" at work using JDK 11 (we're really conservative) and our build process uses some Nashorn-based JavaScript, and the runtime spits out "it's going away" messages. I think I read that they got very little feedback from people relying on JavaScript support and so it dropped out of the roadmap. – Pointy Aug 04 '20 at 00:02
  • 1
    I remember it was just a few years ago when Nashorn was such a big deal; I saw a presentation about the architecture at a Strange Loop conference. I guess they decided it was a waste of resources (by their accounting of value). – Pointy Aug 04 '20 at 00:04
  • @Pointy Thanks for the detail. It's a pity (IMO). – Andrew Thompson Aug 04 '20 at 00:08
  • 3
    As of Java 15 the Nashorn engine is not part of the JDK anymore. With the deprecation of this engine, afaik there is no scripting engine included in the JDK. The engine SPI is supported, it is just the built-in sample implementation that is not there. – Peter Verhas Oct 25 '20 at 10:33
8

The Java ScriptEngine API will work with all JSR-223 Scripting Languages. I haven't found a well documented complete list of these before but this post does a pretty good job, Where can I find a list of available JSR-223 scripting languages? Here is one list from that post,

JSR-223 script engines

  • AWK
  • BeanShell
  • ejs
  • FreeMarker
  • Groovy
  • Jaskell
  • Java
  • JavaScript
  • JavaScript (Web Browser)
  • Jelly
  • JEP
  • Jexl
  • jst
  • JudoScript
  • JUEL
  • OGNL
  • Pnuts
  • Python
  • Ruby
  • Scheme
  • Sleep
  • Tcl
  • Velocity
  • XPath
  • XSLT

JSR 223 script engines maintained elsewhere

  • JavaFX Script
  • ABCL
  • AppleScript
  • Bex script
  • OCaml Scripting Project
  • PHP
  • PHP (another one)
  • Python
  • Smalltalk
  • CajuScript
  • MathEclipse

Most have a special implementation for it to work. For example python alone will not work you need the Jython jar added to the class path. Same for Ruby you'll need JRuby.

Community
  • 1
  • 1
Kyle Bridenstine
  • 6,055
  • 11
  • 62
  • 100
  • Scala is supported too I think? – Nicofisi Mar 07 '18 at 16:51
  • ScriptEngine languages are self-registering via the `java.util.ServiceLoader` mechanism (Service Discovery / Service Provider Interface). This means that in a library in `META-INF\services` a file is created that points to an implementation of the `ScriptEngineFactory` interface. It depends on the specific language if they add this in the "core" jar of the scripting language, or if this is done via a separate jar. For example beanshell add this in the core `org.apache-extras.beanshell:bsh` jar since version 2.0. Adding this jar to your classpath is sufficient to be able to use in ScriptEngine. – slindenau Jul 19 '22 at 12:20
5

There are several other languages available. For instance, Jython (Python implementation in Java). The way to use other languages is by adding the JAR file to CLASSPATH and making a reference to the right name.

For Ruby, there is JRuby. See the following: https://github.com/jruby/jruby/wiki/JavaIntegration

ScriptEngineManager m = new ScriptEngineManager();
ScriptEngine rubyEngine = m.getEngineByName("jruby");
Akira
  • 4,001
  • 1
  • 16
  • 24