简体   繁体   中英

Calling Java method from JavaScript using ScriptEngine

I'm using ScriptEngine, running JavaScript.

I want the JavaScript script to be able to call myFunction(), where myFunction() is a method inside a given class of mine.

I know it's possible to use importPackage for standard Java stuff, what about my own methods?

Please advise :)

According to this , when you do

importPackage(java.awt)

in a script, that's actually a shortcut for a built-in global variable called Packages -- "java.awt" is actually a shortcut for "Packages.java.awt". So you might try and see whether, if you create your own package called "com.mypackage.hello", the scripting engine can access Packages.com.mypackage.hello . I haven't been able to find any documentation on that, but it's worth trying.

Not sure what you are actually trying to do but typically, you would make an HTTP Request from your JavaScript using XHR to talk to backend service exposed through HTTP.

You will have to make your Java Class function accessible through a servlet/HTTP interface.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM