FAQ Database Discussion Community


Is it possible to have Nashorn load scripts from classpath?

java,clojure,nashorn
Is it possible to have Nashorn's load method use the project's classpath when resolving URIs? Here's what I'm attempting to do: (defn create-engine "Creates a new nashorn script engine and loads dependencies into its context." [dependencies] (let [nashorn (.getEngineByName (ScriptEngineManager.) "nashorn") scripts (map #(str "load('" % "');") dependencies)] (.eval nashorn...

Nashorn and ClassFilter

java,nashorn
Many examples, including Oracle's own tutorial refers to some classes in the package jdk.hashorn.api.scripting. However it is not part of the standard Java 8 release. And when I tried to add it to my code, it tells me: Access restriction: The type 'NashornScriptEngineFactory' is not API (restriction on required library...

Is it possible to use Nashorn to convert Java code to Javascript?

java,javascript,java-8,nashorn
Oracle recently released Java 8, which includes Oracle Nashorn as a JavaScript engine. Does Nashorn only works one way in that you can convert JavaScript code into Java bytecode? Or is there any way to use it to convert Java code into JavaScript code?

Find JMX MBeans using Nashorn javascript jjs

java,javascript,jmx,nashorn
I am trying to change the value of the proxyName in Catalina/Connector/8009/* connector. The problem is that I have the following exception when trying to find the MBean name. Exception in thread "main" java.lang.ClassCastException: Cannot cast java.lang.String to javax.management.QueryExp at java.lang.invoke.MethodHandleImpl.newClassCastException(MethodHandleImpl.java:361) at java.lang.invoke.MethodHandleImpl.castReference(MethodHandleImpl.java:356) at jdk.nashorn.internal.scripts.Script$jmx_test_jjs.:program(jmx-test.jjs:32) at...

running nashorn using Shebang does not accept -cp option

java,java-8,nashorn
I am trying to use a class into a jar using a Nashorn Shebang script with the -cp option (java version "1.8.0_31"). However it does not works. I have perform some test. The following shebang line works: #!/usr/bin/jjs -scripting #!/usr/bin/jjs -fv (returns nashorn full version 1.8.0_31-b13) while the following not:...

Coercion of Enums in Nashorn works differently than in Rhino

java-8,nashorn
It seems that Java enums are no longer coerced into their string value: java.math.RoundingMode.UP == "UP" evaluates to true in Java 7 but to false in Java 8 when executed in the JavaScript engine. Does anybody know if this is actually a bug or just something in the specification? Thanks!...

Command line scripting with javascript (nashorn) on windows

javascript,windows,shell,nashorn
I would like to use javascript (specifically nashorn) for command line scripting under windows. By command line scripting I mean using javascript instead of a .bat file, for executing various command line utilities and processing their output. An official example is here at oracle. There they show how you can...

a nashorn engine bug?

java,javascript,java-8,nashorn
i try to execute this simple short scripts to test nashorn : basedir="/cassandara2/cassandra" ba="/cassandara2/cassandra/lib/ST4-4.0.8.jar"; lib = ""; lib += ' <root url="jar://' + ba.replace(basedir, "$PROJECT_DIR"); print(lib); it works perfectly but when i add sign $ after "$PROJECT_DIR" ==> "$PROJECT_DIR$" , i got this error: > java.lang.StringIndexOutOfBoundsException: String index out of...

Nashorn ability to pass undefined variables to a function

java,undefined,nashorn
Here is a piece of very simple piece of Java code that evaluates some JavaScript code: public static class Logger { public log(String line) { System.out.println(line); } } public static void main(String[] args) { NashornScriptEngineFactory factory = new NashornScriptEngineFactory(); ScriptEngine engine = factory.getScriptEngine(new String[] { "-strict", "--no-java", "--no-syntax-extensions" }); engine.put("log",...

How can I access a static-method only class via ScriptEngine?

java,nashorn,scriptengine
I have a class Foo that has only one static method (of note), called bar(). I need to access bar() via a runtime script (in this case Javascript). final ScriptEngineManager factory; factory = new ScriptEngineManager(); factory.put("foo", new Foo()); The above works, but it seems a bit odd to me. Specifically...

Using external library with Nashorn Javascript Engine

java,javascript,jsf,nashorn
I try to use Google Maps Api with Nashorn. But it seems that it doesn't recognize the api. Bean ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn"); engine.eval(new FileReader(FacesContext.getCurrentInstance().getExternalContext().getResource("/resources/js/MapGoogle/functions.js").getFile())); Invocable invocable = (Invocable) engine; Object result = invocable.invokeFunction("myFunction","param1"); functions.js var myFunction = function(param1) { var directionsService = new google.maps.DirectionsService(); return "";...

Should I use a separate ScriptEngine and CompiledScript instances per each thread?

java,multithreading,nashorn,jsr223
My program uses Java Scripting API and can eval some scripts concurrently. They don't use shared script objects, Bindings or Context, but can use same ScriptEngine and CompiledScript objects. I see that Oracle Nashorn implementation in Java 8 is not multithreaded, ScriptEngineFactory.getParameter('THREADING') returns null about which the documentation says: The...

How to hide methods/fields from Nashorn

java,wrapper,nashorn
is it possible to hide methods/fields from the NashornScriptEngine for example it can only call methods/fields if a particular annotation is associated with it. I did find another question similar to this but unfortunately it was for Rhino how do I hide a class method from rhino script engine. I...

Why do I get error in JS script? [closed]

java,javascript,nashorn
I'm running a JS script, which is correct but I get an error when running in Nashorn. Why is this error? When only compiled the JS and out of Nashorn, it runs correctly and no errors are displayed but this only occurs in Nashorn. JS function process(val) { var isPeso...

Accessing Nashorn generated bytecode

java,javascript,bytecode,intercept,nashorn
Nashorn translates javascript source code directly into memory. Is there any way to access the bytecode just for reading purpose? (manipulation isn't required). If there is, kindly explain in detail as I have limited experience... I am aware of javap but it is showing only Java specific bytecode. I want...

Strange exception with Java8 Nashorn regex engine

regex,java-8,nashorn
I ran into a strange java.lang.ArrayIndexOutOfBoundsException: 64 error while parsing text using Nashorn Javascript engine. To reproduce the problem in JJS: var col0 = " xxxx, xxx xxxxxx xxxxxxxxx xxxxxxx, xxxx xxxxx xxxxx "; var name = /([^\s]+),(.*)+/.exec(col0); Some other variants of the test string also cause the same exception,...

Access object variable in Java Nashorn

java,javascript,nashorn
I have an object in my script, that contains fields and methods. I can call the methods in Java with invokeMethod() but can't seem to get the content of the fields of the object. I've got this JavaScript code: var Test = { TestVar: "SomeTest", TestFunc: function() { print("Hello"); }...

Java 8 ScriptEngine across ClassLoaders

java,java-8,classloader,nashorn
I need to execute some javascript code 'inside' different classloaders. If it is java, each task will run in separate class loader. Now I need this to be javascript. Do I need to create new instance of ScriptEngine in each classloader, or is it ok to share one across class...

Java code to be compiled into MethodHandle in Constant Pool

java,bytecode,decompiling,nashorn,methodhandle
I am trying to have Java 8 Nashorn with complete source (not instrumented). As you may know, it uses Nasgen to modify the .classes, and the output is shipped in JRE/lib/ext/nashorn.jar. On disassembling the output, using javap: 0: aload_0 1: ldc #24 // String Function 3: ldc #31 //...