nashorn

how to access methods from my class inside javascript in Nashorn

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-02 05:22:41
In Nashorn it is possible to access predefined classes of java , var ArrayList = Java.type('java.util.ArrayList'); var list = new ArrayList(); list.add('a'); list.add('b'); list.add('c'); Like wise it is possible to access my classes in java script , If so how can it be done and should i be adding my jar in the classpath for referencing it ???? var ArrayList = Java.type('com.example.exa'); You must use the -classpath option of jrunscript or jjs . -cp, -classpath (-cp path. Specify where to find user class files.) The Java class: package de.lhorn.so; public class Foo { public final static int

Troubleshooting Nashorn “Method code too large!” exception

主宰稳场 提交于 2019-12-01 09:31:00
Running jjs or ScriptEngine#eval on my JavaScript ( https://gist.github.com/also/005fd7c200b20f012e10 ) crashes with this exception and no more details: Exception in thread "main" java.lang.RuntimeException: Method code too large! at jdk.internal.org.objectweb.asm.MethodWriter.getSize(MethodWriter.java:2065) at jdk.internal.org.objectweb.asm.ClassWriter.toByteArray(ClassWriter.java:856) at jdk.nashorn.internal.codegen.ClassEmitter.toByteArray(ClassEmitter.java:577) at jdk.nashorn.internal.codegen.CompilationPhase$8.transform(CompilationPhase.java:396) at jdk.nashorn.internal.codegen

Debug dynamically loaded JavaScript code with IntelliJ and the Nashorn engine

风格不统一 提交于 2019-12-01 05:31:12
问题 I am dynamically loading and running JavaScript code that is stored on disk in a YAML file. I would like to know if it is possible (using intelliJ) to debug the JS code even though I am not loading it from a standalone JS file. To simplify the problem description, consider the following Java code: NashornScriptEngineFactory nashornFactory = new NashornScriptEngineFactory(); ScriptEngine engine = nashornFactory.getScriptEngine(); engine.eval("var a = 1 + 1;\nprint(a);"); How do I set a

Java8 JS Nashorn convert array to Java array

主宰稳场 提交于 2019-11-30 17:31:05
How can I convert JS array to native array ? In Rhino conversion looked like (Scala code): val eng = (new javax.script.ScriptEngineManager).getEngineByName("JavaScript") val obj = eng.eval("[1,2,3,4]") val arr = obj.asInstanceOf[sun.org.mozilla.javascript.internal.NativeArray] In Nashorn NativeArray absent, and I can't find any documentation on conversion. From Java (and Scala), you can also invoke convert method on jdk.nashorn.api.scripting.ScriptUtils class. E.g. from Java: import jdk.nashorn.api.scripting.ScriptUtils; ... int[] iarr = (int[])ScriptUtils.convert(arr, int[].class) my Scala is

Nashorn bug when calling overloaded method with varargs parameter

五迷三道 提交于 2019-11-30 07:08:23
问题 Assume the following API: package nashorn.test; public class API { public static void test(String string) { throw new RuntimeException("Don't call this"); } public static void test(Integer... args) { System.out.println("OK"); } } The following Nashorn JavaScript snippet will fail: var API = Java.type("nashorn.test.API"); API.test(1); The first method will be called instead of the second. Is this a bug in the Nashorn engine? For the record, this issue was previously reported on the jOOQ User

How can I start coding with Oracle's Nashorn JS Engine and when will it replace Rhino in the OpenJDK?

风格不统一 提交于 2019-11-30 06:13:20
I'm looking for a way to start playing around with Oracle's new Nashorn JavaScript Engine. I've DL'd the latest OpenJDK 8 (b65) and it appears that Rhino is still the only included script engine. Anyone know when (or in which build) Nashorn will replace Rhino in the OpenJDK? Or even better, where I can get a JDK with it included already? I know Netbeans has already written a debugger to use it, just not sure where they got the libraries/code to start writing it. Anyone have some links? Thanks. It looks like there is no sign of Nashorn on OpenJDK yet. The most recent comment from Jim Laskey in

Java8 JS Nashorn convert array to Java array

纵饮孤独 提交于 2019-11-30 01:14:00
问题 How can I convert JS array to native array ? In Rhino conversion looked like (Scala code): val eng = (new javax.script.ScriptEngineManager).getEngineByName("JavaScript") val obj = eng.eval("[1,2,3,4]") val arr = obj.asInstanceOf[sun.org.mozilla.javascript.internal.NativeArray] In Nashorn NativeArray absent, and I can't find any documentation on conversion. 回答1: From Java (and Scala), you can also invoke convert method on jdk.nashorn.api.scripting.ScriptUtils class. E.g. from Java: import jdk

Capturing Nashorn's Global Variables

限于喜欢 提交于 2019-11-29 20:00:03
问题 I have a Java 7 program, that loads thousands of objects (components), each with many parameters (stored in a Map ), and executes various Rhino scripts on those objects to calculate other derived parameters which get stored back in the object's Map . Before each script is run, a Scope object is created, backed by the object's map, which is used as a JavaScript's scope for the duration of the script. As a simple example, the following creates a HashMap with a=10 and b=20, and executes the

Nashorn under Java 7

删除回忆录丶 提交于 2019-11-29 14:44:18
I'm trying to get Oracle's Nashorn running under Java 7. I've cloned the repo and compiled the source. All I had to do to get it to compile was: Change jdk.internal.org.objectweb.asm.* to org.objectweb.asm.* Change one line that had Long.hashCode() static. Everything seems happy, and Java 7 sees Nashorn as a scripting engine; but when I try and get an instance: NashornScriptEngineFactory factory = new NashornScriptEngineFactory(); ScriptEngine engine = factory.getScriptEngine(); I get the exception: java.lang.RuntimeException: java.lang.ClassNotFoundException: jdk.nashorn.internal.objects

Can Nashorn startup slowness be overcome?

核能气质少年 提交于 2019-11-29 06:32:23
I've used Rhino for a scripting component inside graphics. In the project there are about 200 small scripts running independantly. Immediately when starting the application the scripts should be at full speed. Rhino's performance was sufficient, but since Oracle advices to migrate to Nashorn, i'm facing a dilema. Below a picture showing the load difference between Rhino and Nashorn at approximayely 15,000 invocations of the scripts. The Startup slowness of Nashorn is my biggest issue. Note, this was back on JDK 1.8.0. JDK 1.8u5 is similar I hope the picture is clear. This is an outline of how