Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I continue to use Javascript in Java 15 onwards

In my Java application a small put important feature is to be able to rename audio files based on their metadata (e.g album/artist -title) and the mask is specified using Javascript, this makes for a very flexible and powerful renaming feature.

I knew Javascript was being deprecated but it now seems it is actually going to be removed from Java 15 onwards. This loss of functionality could be a major problem for me so I ask even if it is officially removed is there a way I can continue to use Javascript within a Java 15 application, i.e will it be available as an opensrc plugin that I can use at my own risk. The fact that development has stopped at Ecmascript 5.1 is not a major issue for me.

There was mention of using GraalVM but without explanation how to do this and I don't see that fits in with my scenario, specifically my application is available on some 32bit Arm environments and GraalVM is not available for 32bit.

I have already moved from Java 11 to Java 14 to resolve some other issues, so I cannot really go back to Java 11, and since Java 14 is not a LTS release I cannot really stick with Java 14 over long period of time.

This is the bulk of my Javascript code

try
{
    mask = includeUserDefinedFunctions(mask);
    ScriptEngine engine = manager.getEngineByName("JavaScript");
    for(SongFieldName next:SongFieldName.values())
    {
        if(next.getScriptVar()!=null && next.getSongFieldKey()!=null)
        {  
            engine.put(next.getScriptVar(), cleanValue(song.getFieldValueSpaceSeparatedOrEmptyString(next.getSongFieldKey()), song));
        }
    }
    String result = (String)engine.eval(mask);
    return result;
}
catch(ScriptException se)
{
    throw se;
}
like image 460
Paul Taylor Avatar asked Jun 08 '20 09:06

Paul Taylor


People also ask

Can Java and JavaScript be used together?

You can call JavaScript from Java using the Java Scripting Framework, but in this case, it's probably easiest to just translate your colleague's code to Java. It's not really long, is it? Java has a 'scripting engine' for evaluating for example JavaScript from Java.

What can I use instead of nashorn?

The Nashorn engine has been deprecated in JDK 11 as part of JEP 335 and and has been removed from JDK15 as part of JEP 372. GraalVM can step in as a replacement for JavaScript code previously executed on the Nashorn engine. GraalVM provides all the features for JavaScript previously provided by Nashorn.

Why was nashorn deprecated?

With the release of Java 11, Nashorn was deprecated citing challenges to maintenance, and has been removed from JDK 15 onwards. Nashorn development continues on GitHub as a standalone OpenJDK project and the separate release can be used in Java project from Java 11 and up.


2 Answers

You can run GraalVM's JavaScript Engine on any JVM as a bunch of dependencies pulled from Maven central. Here's an example: https://github.com/graalvm/graaljs/blob/master/docs/user/RunOnJDK.md

It will be slower without the GraalVM compiler, because the language implementation framework for GraalVM needs certain api from the compiler which afaik is not in other JITs.

If you will look into migrating your application to use GraalVM's JavaScript, if you use certain Nashorn features it might not work out of the code, consider starting with the --nashorn-compatibility-mode (here are more details about it: https://github.com/graalvm/graaljs/blob/master/docs/user/NashornMigrationGuide.md).

like image 132
Oleg Šelajev Avatar answered Oct 11 '22 22:10

Oleg Šelajev


You can use a standalone version of Nashorn: https://github.com/openjdk/nashorn.

Nashorn is a JPMS module, so make sure it and its transitive dependencies (Nashorn depends on several ASM JARs) are on your application's module path, or appropriately added to a module layer, or otherwise configured as modules.

While standalone Nashorn is primarily meant to be used with Java 15 and later versions, it can also be used with Java versions 11 to 14 that have a built-in version of Nashorn too. See this page for details on use when both versions are present.

like image 44
ZhekaKozlov Avatar answered Oct 11 '22 23:10

ZhekaKozlov