adi
adi

Reputation: 3

Execute javascript code from java (with eclipse)

I need to call some javascript code from my java app, can I do such a thing?

thanks adi

Upvotes: 0

Views: 1380

Answers (3)

Ry-
Ry-

Reputation: 225125

LiveConnect does this nicely, see the references and examples for JSObject. You'll probably be interested in JSObject.eval, which will give you the ability to execute JavaScript code under the context of any JavaScript object.

Upvotes: 0

Pointy
Pointy

Reputation: 413916

Yes, you can, either by grabbing Rhino from Mozilla and using its integration libraries or by using the JDK 1.6 "ScriptEngine" facility.

The version of Rhino (the Mozilla-authored Java-implemented JavaScript engine) included with JDK 6 is pretty old and buggy, be warned.

Upvotes: 0

templatetypedef
templatetypedef

Reputation: 373052

You can do this using a third-party library like Rhino, but there is no straightforward way to invoke JavaScript code from Java. Though the two have similar names, they have about as much in common as a car and a caramel.

More generally, having programs written in one language interact with languages written in another is often tricky due to the internals of the two programming language implementations not being compatible with another. There are many exceptions to this rule and a lot of effort has been invested in making projects work in multiple languages, but there's often a high startup cost.

Upvotes: 2

Related Questions