add a own ScriptFunction to the nashorn global

A. Sundararajan sundararajan.athijegannathan at
Thu Dec 5 02:42:18 PST 2013

jdk.nashorn.internal classes are ... "internal" implementation classes. 
In fact, if you are running with security manager enabled, you'll get 
access exception when attempting to access classes from 
jdk.nashorn.internal.* packages.

For the specific problem you've - namely to expose your own script 
functions from java code, there are two solutions:

*) Implement any @FunctionalInterface interface in JDK (or your own 
@FunctionalInterface)  and pass/put object of the same in a 
javax.script.Bindings or even global scope. Script can access these as 
though these are functions.

*) Implement jdk.nashorn.api.scripting.JSObject in your class and 
implement "call" method on it. Again, nashorn's flexible dynalink based 
linker will treat your JSObject impl. as though it is a function. This 
can also be used to implement "constructor" (newObject method) in Java 
code and so on.

Hope this helps,


On Thursday 05 December 2013 11:13 AM, buddhi mihara wrote:
> hi, i need to add a script function like NativeBoolean written in nashorn jdk.nashorn.internal.objects; package , to javascript context.let say my function is work just fine when i added that ContextifyImpl class to jdk.nashorn.internal.objects package.i configure nasgen tool to generate $Constructor and $Prototype  and when i added my ContextifyImpl class to my own package let say jdk.nashorn.internal.Dilan it giving me error as follow.
> Exception in thread "main" java.lang.IllegalAccessError: tried to access method jdk.nashorn.internal.objects.ScriptFunctionImpl.<init>(Ljava/lang/String;Ljava/lang/invoke/MethodHandle;Ljdk/nashorn/internal/runtime/PropertyMap;[Ljava/lang/invoke/MethodHandle;)V from class jdk.nashorn.internal.Dilan.ContextifyImpl$Constructor	at jdk.nashorn.internal.Dilan.ContextifyImpl$Constructor.<init>(Unknown Source)	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)	at sun.reflect.NativeConstructorAccessorImpl.newInstance(	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(	at java.lang.reflect.Constructor.newInstance(	at java.lang.Class.newInstance(	at jdk.nashorn.internal.Dilan.contextify.initConstructors(	at jdk.nashorn.internal.Dilan.contextify.makeContextImpl(	at jdk.nashorn.internal.Dilan.contextify.makeContext(	at jdk.nashorn.internal.scripts.Script$buddhi.runScript(/home/buddhi/Desktop/buddhi.js:4)	at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(	at jdk.nashorn.internal.runtime.ScriptFunction.invoke(	at jdk.nashorn.internal.runtime.ScriptRuntime.apply(	at	at	at	at	at i need to send a javascript function to javascript exection environment which is written inside my own my projecr consider nashorn is an dependancy.for sake of maintainability i need to place all my code in my own package not in  nashorn could i do that thank.
> if u can please send me the proper way to configure javascript function using nashorn
> thanks and best regards 		 	   		

More information about the nashorn-dev mailing list