For js functions, you can define a file, say globals.js, where globals are initialized and added to the JavaScript global object :
globalThis.someFunction = function() {...}
and set the option js.commonjs-global-properties to load this file at context creation time :
Context context = Context.newBuilder("js")
.option("js.commonjs-global-properties", "./globals.js")
.build();
In order to bind java options to JS, you need to enable polyglot.js.allowHostAccess via Bindings :
Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);
bindings.put("polyglot.js.allowHostAccess", true);
bindings.put("polyglot.js.allowHostClassLookup", (Predicate<String>) s -> true);
bindings.put("javaObj", new Object());
or with Context :
Context context = Context.newBuilder("js")
.allowHostAccess(true)
.build();
context.getBindings("js").putMember("a", GlobalMethods.a);
Or you can use the @HostAccess.Export decorator in the class definition :
@HostAccess.Export
public void a() {...}
Though I'm not sure you can access a static method like GlobalMethods.a() without referencing the classname at least one time, maybe passing the function output directly is sufficient to suit your needs, or passing the class definition, followed by an eval globalThis.a = GlobalMethods.a (not tested).