简体   繁体   English

从java执行javascript函数

[英]Execute javascript function from java

I want to know if it's possible to execute a javascript function from java. 我想知道是否可以从java执行javascript函数。 Basically I have a javascript function to which I want to pass a string and then get the output from that function. 基本上我有一个javascript函数,我想传递一个字符串,然后从该函数获取输出。

If it's possible, how do I accomplish it? 如果有可能,我该如何完成它?

EDIT - 编辑 -

I couldn't find a solution to this, what I did was re-write all the javascript code in Java. 我无法找到解决方案,我所做的是用Java重写所有的javascript代码。 But I am guessing, Rhino ported on Android should do the trick. 但我猜,在Android上移植的Rhino应该可以解决问题。 If someone has tried it out, please post a solution. 如果有人尝试过,请发布解决方案。

You probably want to take a look at the ScriptEngine . 您可能想看一下ScriptEngine There are plenty of samples out there on how to use it. 关于如何使用它的样本很多。 Works on anything but Mac where they for some reason selected to include AppleScript instead of JavaScript by default. 适用于除Mac之外的任何内容,默认情况下,由于某些原因,它们选择包含AppleScript而不是JavaScript。

Edit: Take a look at this page , there seems to be a Rhino port for Android out there. 编辑:看看这个页面 ,似乎有一个Android的Rhino端口。

Javascript is not natively supported in java. Java本身不支持Javascript。 If you need it, you may implement the Rhino javascript engine to do this. 如果您需要它,您可以实现Rhino javascript引擎来执行此操作。

"Rhino is an open-source implementation of JavaScript written entirely in Java. It is typically embedded into Java applications to provide scripting to end users." “Rhino是一个完全用Java编写的JavaScript的开源实现。它通常嵌入到Java应用程序中,为最终用户提供脚本。”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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