-2

Possible Duplicates:
I am looking forward to call some javascript method from a java class
can anyone give a complete example of how to call a js using java file???

HI,

i have a javascript file script.js and a class file callScript.java i want to make a call to encrypt() residing in script.js

how to do it using Rhino? i am using java5

NOTE : would be better if i get some code snippet.

Community
  • 1
  • 1
Varun
  • 5,001
  • 12
  • 54
  • 85
  • 4
    "would be better if i get some code snippet." - polite way of saying "pls sendz teh codez" ? – Mitch Wheat Dec 07 '10 at 06:23
  • 1
    You have asked this question three times now, in various forms, and the answer is the same as before. Go read the tutorial pages that various people have provided links to. – Stephen C Dec 07 '10 at 06:46
  • sorry for creating multiple posts.. but reading the tutorial was not helping me. Sorry again @Mitch - sorry for harsh language will be polite on future posts :) – Varun Dec 07 '10 at 07:07
  • your language was not harsh: you are basically asking someone to do your work for you. Show us what you have done so far; people are more willing to help if they see you are trying to solve your own problem... – Mitch Wheat Dec 07 '10 at 07:49
  • @Mitch - thanks a lot will keep that in mind. was frutrated with my 2 days development so needed a quick solution to it. sorry once again – Varun Dec 07 '10 at 07:53

1 Answers1

0
String javaScriptExpression = "sayHello(name);";
Reader javaScriptFile = new StringReader(
    "function sayHello(name) {\n"
        + "    println('Hello, '+name+'!');\n" + "}");

ScriptEngineManager factory = new ScriptEngineManager();
ScriptEngine engine = factory
    .getEngineByName("JavaScript");
ScriptContext context = engine.getContext();
context.setAttribute("name", "JavaScript",
    ScriptContext.ENGINE_SCOPE);

engine.eval(javaScriptFile);
engine.eval(javaScriptExpression);

In this example. create a Reader object by just reading .js file by BufferedReader. Hope this will help you.

Reese Moore
  • 11,524
  • 3
  • 24
  • 32
UVM
  • 9,776
  • 6
  • 41
  • 66