Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different Nashorn engine for Java < 15 and >= 15?

I'm trying to find a workaround for Nashorn to be compatible with every version of Java from 1.8 upwards as said in another question I asked earlier.
I'm currently trying to catch UnsupportedClassVersionError in order to find out if the system is able to run the standalone Nashorn for Java 15 like this (Kotlin code):

scriptEngine = try {
            
    // Java >= 15

    org.openjdk.nashorn.api.scripting.NashornScriptEngineFactory().scriptEngine
} catch(e: UnsupportedClassVersionError) {

    // Java < 15

    @Suppress("DEPRECATION")
    jdk.nashorn.api.scripting.NashornScriptEngineFactory().scriptEngine
}

However it looks like the error is not caught. The stack trace is as follows:

java.lang.UnsupportedClassVersionError: org/openjdk/nashorn/api/linker/NashornLinkerExporter has been compiled by a more recent version of the Java Runtime (class file version 59.0), this version of the Java Runtime only recognizes class file versions up to 55.0

I also have tried to catch NoClassDefFoundError inverting the previous try/catch (load Java < 15 Nashorn, if it doesn't exist load the standalone one) but the error is the same.

Edit: looks like the error is thrown by the Java < 15 Nashorn itself.

like image 996
iamgio Avatar asked Oct 21 '25 06:10

iamgio


2 Answers

Nashorn maintainer here! I just released Nashorn 15.2, which is identical to 15.1.1 but is now compiled with Java 11. This means you can now use standalone Nashorn with Java 11 and above and don't have to switch for < 15 and >= 15. I wrote a page to explain how to use it on Java 11-14 where both built-in and standalone Nashorn are present.

like image 134
Attila Szegedi Avatar answered Oct 23 '25 19:10

Attila Szegedi


How about something like this (sorry, I'm not a Kotlin developer, so pure Java):

public static ScriptEngine getJSScriptEngine() {
    if (Double.parseDouble(System.getProperty("java.specification.version")) < 15) {
        return new ScriptEngineManager().getEngineByName("js");
    } else {
        return new org.openjdk.nashorn.api.scripting.NashornScriptEngineFactory()
            .getScriptEngine();
    }
}
like image 31
ZhekaKozlov Avatar answered Oct 23 '25 19:10

ZhekaKozlov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!