Error while using Java objects/primitive types in java script task

Hi,
Whenever I use any java object or a java primitive type either in my script task or in of the listeners as a script, I get the following error stating ‘Java is not defined’.
However, the same code works in a few other developers machine.
All our developers use wildfly 18.0.1 server to deploy the process applications.

I get the following error when i run the below code snippet as java script:

var ArrayList = Java.type(“java.util.ArrayList”);
var docsReqd = new ArrayList;
docsReqd.push(“Doc1”);
docsReqd

Is there any other specific maven dependency that i have to include OR a specific version of any of the libraries , or any specific java version …etc.

Thanks!

@Kiran_Balakrishna,

First things first: Can you format your question to be more in line with the Style Guidelines .

Now, Seems you’re using GraalVM Java Runtime. Are you using GraalVM RC11 or RC12 or RC15?


ScriptEngine interface does not provide a default way to set options. As a workaround, GraalJSScriptEngine supports setting some Context options through Bindings.

While starting the application run with below jvm args:

java -Dpolyglot.js.allowAllAccess=true MyApplication

(or) through the code:

Bindings bindings = scriptEngine.getBindings(ScriptContext.ENGINE_SCOPE);
bindings.put("polyglot.js.allowAllAccess", true);

If you want to use nashorn script engine in GraalVM, then by using a system property when starting a Java application:

java -Dpolyglot.js.nashorn-compat=true MyApplication

It is highly recommended not to use the Nashorn compatibility mode in production code if you execute less trusted user code.


In GraalVM JavaScript you can create and access Java Lists, e.g. java.util.ArrayList.

var ArrayList = Java.type('java.util.ArrayList');
var list = new ArrayList(); //don't forget to include the brackets
list.add(42);
list.add("23");
list.add({});

for (var idx in list) {
    print(idx);
    print(list.get(idx));
}

Map access:

In GraalVM JavaScript you can create and access Java Maps, e.g. java.util.HashMap.

var HashMap = Java.type('java.util.HashMap');
var map = new HashMap();
map.put(1, "a");
map.get(1);

Hi @aravindhrs,

Thanks for the suggestion. How can i find the GraalVM scriptengine version?
I’m using Wildfly 18.0.1 server for application deployment and 7.120.0 version of Camunda.
Also where can i set there GraalJSScriptEngine bindings?

Thanks,
Kiran B~

This is the easiest way to set the bindings through JVM args