Search code examples
javascriptjavajava-8nashorncapacity

JDK 8 Nashorn Javascript Engine limitation


I am moving to JDK 8 and had to use Nashorn to process JS files.

I used to be able to process a 300MB file having 1 huge function using JDK 7 Rhino JS engine. It should be noted that the 300MB is a generated file that is processed by my tool, I know this is not good practice to have a single huge function in a huge file.

Now I am not able to and I am getting java.lang.runtimeException: Class file too large!, with the following stacktrace:

 at  jdk.nashorn.internal.codegen.CompilationPhase$BytecodeGenerationPhase.transform(CompilationPhase.java:431)
 at  jdk.nashorn.internal.codegen.CompilationPhase.apply(CompilationPhase.java:624)
 at  jdk.nashorn.internal.codegen.Compiler.compile(Compiler.java:655)
 at  jdk.nashorn.internal.runtime.RecompilableScriptFunctionData.compileTypeSpecialization(RecompilableScriptFunctionData.java:725)
 at  jdk.nashorn.internal.runtime.RecompilableScriptFunctionData.getBest(RecompilableScriptFunctionData.java:905)
 at  jdk.nashorn.internal.runtime.ScriptFunctionData.getGeneric(ScriptFunctionData.java:384)
 at  jdk.nashorn.internal.runtime.ScriptFunctionData.createGenericInvoker(ScriptFunctionData.java:282)
 at  jdk.nashorn.internal.runtime.ScriptFunctionData.getGenericInvoker(ScriptFunctionData.java:276)
 at  jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:620)
 at  jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:494)
 at  jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:393)
 at  jdk.nashorn.api.scripting.ScriptObjectMirror.callMember(ScriptObjectMirror.java:199)
 at  jdk.nashorn.api.scripting.NashornScriptEngine.invokeImpl(NashornScriptEngine.java:386)
 at  jdk.nashorn.api.scripting.NashornScriptEngine.invokeFunction(NashornScriptEngine.java:190)

I understand that the file size is ridiculous but it used to run on Rhino, I want to know what are Nashorn size limitations and if there is any way to overcome this.

I am using JDK 8u181. Is this fixed in newer JDK versions, now that 9 and 10 are available?


Solution

  • Nashorn is being deprecated in Java 11.

    The best alternative today being GraalVM.