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.
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.
Share Improve this question edited Jul 31, 2012 at 11:51 John Conde 220k99 gold badges463 silver badges502 bronze badges asked Jul 31, 2012 at 11:42 luperxeluperxe 711 gold badge2 silver badges6 bronze badges 6- What has that to do with android? – Anders Metnik Commented Jul 31, 2012 at 11:45
- It is a phonegap application based on android. – luperxe Commented Jul 31, 2012 at 11:49
- @AndersMetnik Actually it is related to android. As we have to do code in different manner depending upon the platform. – coderslay Commented Jul 31, 2012 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? – coderslay Commented Jul 31, 2012 at 12:01 - @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 Commented Jul 31, 2012 at 12:04
3 Answers
Reset to default 2use 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
ScriptEngine engine = manager.getEngineByName("JavaScript");
Does this work?
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