为什么getEngineByName("js")返回null? [英] Why does getEngineByName(“js”) return null?
问题描述
无论我尝试什么,getEngineByName()
都将继续返回null.
Whatever I try, getEngineByName()
keeps returning null.
这是我的代码:
final ScriptEngineManager manager = new ScriptEngineManager();
final ScriptEngine engine = manager.getEngineByName("js");
但是engine
在这些行之后为空.
But engine
is null after these lines.
我也尝试过:
-
getEngineByName("javascript")
-
getEngineByName("nashorn")
getEngineByName("javascript")
getEngineByName("nashorn")
它们都返回null
.实际上,manager.getEngineFactories()
显示的是一个空数组-意味着根本没有工厂.
They all return null
. Actually, manager.getEngineFactories()
shows an empty array - meaning there are no Factories at all.
这2个答案建议将null
传递给构造函数,但这对我不起作用:
These 2 answers suggest passing null
to the constructor, but it didn't work for me:
- https://stackoverflow.com/a/25332851/900394
- https://stackoverflow.com/a/25332851/900394
这个答案说的是已修复的错误.
更新:
那是Eclipse中的 Android应用项目.
That was an Android Application Project in eclipse.
我不知道它与 Java项目有什么不同.
I didn't know it differs that much from a Java Project.
现在,我刚刚打开一个新的 Java项目,并写了以下几行,然后得到了一些结果:
Now I just opened a new Java Project, wrote these lines, and I'm getting some results:
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import javax.script.SimpleBindings;
public class TestClass {
public static void main(String[] args) throws ScriptException {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = scriptEngineManager.getEngineByName("js");
}
}
engine
不是空!
此外,javax.script.*
已成功导入,就好像它已经存在(内置)一样.这是因为我正在使用Java 8吗?
Also, javax.script.*
was imported successfully as if it's already there (built in). Is this because I'm using Java 8?
在先前的(android)项目中,我使用的是 javax.script ,该软件是从
In that previous (android) project I was using javax.script downloaded from here.
那么 Android项目中会出现什么问题,我该如何解决?
So what would be the problem in the Android Project and how do I solve it?
推荐答案
您必须使用大写字母写参数,如下所示:
You have to write the argument with Capital Letters, as shown below:
getEngineByName("JavaScript");
^ ^
这篇关于为什么getEngineByName("js")返回null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!