Rhino 和类过滤器

Nashorn and ClassFilter

很多例子,包括Oracle's own tutorial引用包jdk.hashorn.api.scripting中的一些类。但是它不是标准 Java 8 版本的一部分。当我试图将它添加到我的代码中时,它告诉我:Access restriction: The type 'NashornScriptEngineFactory' is not API (restriction on required library 'C:\Program Files\Java\jre1.8.0_25\lib\ext\nashorn.jar').

如何在我的 java 项目中使用 ClassFilter 功能?

使用当前版本 1.8.0_40,而不是 1.8.0_25From the release notes:

New Java class-access Filtering Interface

The jdk.nashorn.api.scripting.ClassFilter interface enables you to restrict access to specified Java classes from scripts run by a Nashorn script engine. See Restricting Script Access to Specified Java Classes in the Nashorn User's Guide and 8043717 (not public) for more information.

我遇到了同样的问题。甚至尝试更新我的 Java SDK 以查看是否修复了它。更新到 1.8 u65 并有同样的问题。如果您使用的是 Eclipse,请尝试进入

Windows->Preferences->Compiler->Errors/Warnings->Deprecated and restricted API 更改 禁止引用(访问规则)ErrorWarn.

它为我打开了通道,让我可以毫无问题地构建。