Why won't MATLAB find the java nashorn ScriptEngine?
2 views (last 30 days)
Show older comments
MATLAB seems to not want to recognize the nashorn javascript engine that is embedded in java.
First, let me make it clear that the nashorn engine is only available in java 1.8. I am using MATLAB R2015b and I have set the MATLAB_JAVA environment variable to C:\Program Files\Java\jdk1.8.0_91\jre. This has been verified with
version -java
ans =
Java 1.8.0_91-b14 with Oracle Corporation Java HotSpot(TM) 64-Bit Server VM mixed mode
manager = javaObject('javax.script.ScriptEngineManager');
engine = manager.getEngineByName('nashorn');
After executing this code, engine is empty (null). I also tried
list = manager.getEngineFactories();
This returns an empty list, meaning that it cannot find any javascript engines. I'm thinking this might be a classloader issue? ScriptEngineManager has an overloaded constructor that allows you to pass in your own classloader but I haven't gotten that to work either. Any help is greatly appreciated.
2 Comments
Stav
on 8 Dec 2016
I am able to reproduce on a Win7 x64 sp1 machine. MATLAB 2016a with Java 1.8.0_112.
@jackson k, where you able to make any progress on this. Thanks
Answers (1)
Stav
on 10 Dec 2016
Apparently this problem stems from the fact MATLAB's dynamic class loader makes it appear as if there are no scripting engines (including Nashorn..) available. So I was able to fix this by adding the nashorn JAR to the static classpath:
Add the following line to a file named `javaclasspath.txt` in your preferences directory (you can find out what your preference directory is by running `prefdir` in the MATLAB command line):
[JRE_INSTALL_DIR]\lib\ext\nashorn.jar
(replace [JRE_INSTALL_DIR] with the path to your JRE installation)
For some hints about how the static classpath can be modified otherwise (including during runtime) see the following article: http://undocumentedmatlab.com/blog/static-java-classpath-hacks
See Also
Categories
Find more on Call Java from MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!