Search code examples
javanashorn

Java Nashorn - template strings


Does Java Nashorn support JavaScript template strings ?

I get the following error when trying var a = `this is a \n multiline string \n `: javax.script.ScriptException: <eval>:1:22 Expected an operand but found error


Solution

  • With Nashorn "scripting mode" (enabled by -scripting option with jjs and also -Dnashorn.args system property can be used for javax.script to set options), there is a Nashorn specific extension for multiline strings and string interpolation.

    See also:

    https://wiki.openjdk.java.net/display/Nashorn/Nashorn+extensions#Nashornextensions-Multi-linestringliterals%28-scriptingmodeonly%29

    As mentioned, ES6 features are being added to jdk9. You can enable es6 features (subset) using --language=es6 option