Why won't MATLAB find the java nashorn ScriptEngine?

1 次查看(过去 30 天)
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 个评论
Stav
Stav 2016-12-8
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
raym
raym 2023-9-30
I am able to reproduce on a Win8.1 x64 MATLAB 2016a with Java 1.8.0_211-b12

请先登录,再进行评论。

回答(1 个)

Stav
Stav 2016-12-10
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

类别

Help CenterFile Exchange 中查找有关 Call Java from MATLAB 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by