Maximo JS 自动化脚本:"importPackage" 未定义

Maximo JS automation script: "importPackage" is not defined

我正在尝试使用来自 Maximo 76 脚本功能 (PDF download) 的 JS 脚本。

importPackage(java.util)
importPackage(Packages.psdi.server)
var ctx = new HashMap();
ctx.put("url","http://localhost:7001/maximo/oslc/script/countryapi?_lid=wilson&_lpwd=wilson");
service.invokeScript("LIB_HTTPCLIENT",ctx);
var jsonResp = ctx.get("response");
var countries = JSON.parse(jsonResp);

当我执行脚本时出现此错误:

ReferenceError: "importPackage" is not defined in <eval> at line number 1

为什么会出现此错误?

将此添加到脚本的开头:

load("nashorn:mozilla_compat.js");

详情:

来自Automation Scripts: Compatibility with Maximo 7.6.1

...the Rhino JavaScript engine was replaced with Nashorn (V8). It turns out that Nashorn does not permit the import of whole Java packages which sheds light on why I was getting the error.

Add the following line to the beginning of your script:

load("nashorn:mozilla_compat.js");

This article references how to properly construct your script to take advantage of the new script engine.

来自 Maximo 76 脚本功能 (PDF download)。

Java 8 and Nashorn engine:

Some of the above example is written using the jdk 7 based rhino js engine. In jdk 1.8, the rhino engine has been replaced with the Nashorn (V8) engine. For example the importPackage command will not work there. You would need to use the JavaImporter function to do the same in Nashorn. You can look into this Whosebug link for more details on what all changed from Rhino to Nashorn that may impact your script code in js:

根据 Rhino Migration Guide 你可以尝试这样的事情:

var HashMap = Java.type("java.util.HashMap");

因为它像导入一样工作,所以您可以在之后使用它:

var ctx = new HashMap();