Why won't MATLAB find the java nashorn ScriptEngine?

3 ビュー (過去 30 日間)
jackson k
jackson k 2016 年 5 月 9 日
コメント済み: raym 2023 年 9 月 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
  1 件のコメント
raym
raym 2023 年 9 月 30 日
This really solved my problem! Thanks

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCall Java from MATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by