How to call javascript function from java code

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


How to call javascript function from java code



I'm trying to call a javascript function from a java class, but I get these errors:


-Could not find method sun.misc.Service.installedProviders, referenced from method javax.script.ScriptEngineManager.initEngines

-Could not find method sun.misc.Service.providers, referenced from method javax.script.ScriptEngineManager.initEngines

-Could not find method sun.misc.Service.installedProviders, referenced from method javax.script.ScriptEngineManager.initEngines

-java.lang.VerifyError: javax.script.ScriptEngineManager



Here the code:


public void sendResult(){
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("js");

String script = "function send() {"+"var id_result = window.MyPGP.getResult();"+
"document.getElementById('id_result').value = id_result;"+"console.log("change the box value");";

try {
engine.eval(script);

Invocable invocableEngine = (Invocable) engine;

invocableEngine.invokeFunction("send");


} catch (ScriptException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}



What I'm trying exactly to do is that when the payment is finished, this function is called in other methods in this class to get the result of the payment and it is printed in a box in the main html.



This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.





What has that to do with android?
– Anders Metnik
Jul 31 '12 at 11:45





It is a phonegap application based on android.
– luperxe
Jul 31 '12 at 11:49





@AndersMetnik Actually it is related to android. As we have to do code in different manner depending upon the platform.
– Coder_sLaY
Jul 31 '12 at 11:52





@luperxe You want to call a java script function which is in your .html file? Do you want to run that function in the background?
– Coder_sLaY
Jul 31 '12 at 12:01


.html





@Coder_sLaY the javascript function I want to call is written in the code I posted. String script = "function send() {"+"var id_result = window.MyPGP.getResult();"+"document.getElementById('id_result').value = id_result;"+"console.log("change the box value");"; Yes, I want to run it in the background.
– luperxe
Jul 31 '12 at 12:04






3 Answers
3


ScriptEngine engine = manager.getEngineByName("JavaScript");



Does this work?





I've tested it and does not work...
– luperxe
Jul 31 '12 at 12:00





You can do something like this


super.loadUrl("file:///android_asset/www/index.html", 20000);
super.loadUrl("javascript: { var pageFlag = '" + flag + "';}"); // Your Javascript function here





But in that case, the javascript code would be executed when the application starts. What I want is to call the javascript code after a java execution
– luperxe
Aug 1 '12 at 6:44





What does you function do in background?
– Coder_sLaY
Aug 1 '12 at 6:50





My application consists on a paypal payment. I click a button in order to pay, some javascript methods are called, then, the java code is executed, and I obtain the result of the payment in a variable. What I want is to pass this variable to the html code, so, I need to call a javascript function that gets that value and prints it on the html. I've already done it, but I need to click in another button to see the result. What I want now is to see the result without clicking a button.
– luperxe
Aug 1 '12 at 7:10



use this code


import javax.script.*;

public class InvokeScriptFunction {
public static void main(String args) throws Exception {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("JavaScript");

// JavaScript code in a String
String script = "function hello(name) { print('Hello, ' + name); }";
// evaluate script
engine.eval(script);

// javax.script.Invocable is an optional interface.
// Check whether your script engine implements or not!
// Note that the JavaScript engine implements Invocable interface.
Invocable inv = (Invocable) engine;

// invoke the global function named "hello"
inv.invokeFunction("hello", "Scripting!!" );
}
}



good luck





When giving an answer it is preferable to give some explanation as to WHY your answer is the one.
– Stephen Rauch
Feb 16 '17 at 3:37






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Arduino Mega cannot recieve any sketches, stk500_recv() programmer is not responding

Visual Studio Code: How to configure includePath for better IntelliSense results

C++ virtual function: Base class function is called instead of derived