0

My code is like

SJCL.js

function encrypt(data, key){
    ......
}

abcd.java

public String callJavascript(String data,String key)
{
    // i want to call the encrypt method here with data,key  value passing to it
}

any help????

using java5 only

Varun
  • 5,001
  • 12
  • 54
  • 85
  • _pls help me and give me the code to do the same_ does not sound good, does it? Can you tell us what you have tried till now? – Nivas Dec 06 '10 at 05:39
  • sorry for sounding "not good".. but i am looking for java to js communication from last 3 days and came across Rhino, but the code is not working for me i tried this ScriptEngineManager factory = new ScriptEngineManager(); ScriptEngine engine = factory.getEngineByName("JavaScript"); try { engine.eval("print('Hello, World')"); } catch (ScriptException ex) { ex.printStackTrace(); } even this didnt worked... – Varun Dec 06 '10 at 05:47
  • possible duplicate of [I am looking forward to call some javascript method from a java class](http://stackoverflow.com/questions/4344661/i-am-looking-forward-to-call-some-javascript-method-from-a-java-class) – Stephen C Dec 07 '10 at 06:44

2 Answers2

1

I'm sure you checked this one: http://download.oracle.com/javase/6/docs/technotes/guides/scripting/programmer_guide/index.html

What you are probably looking for is under the 'Invoking Script Functions and Methods' (an example to call your 'crypt()' from java)

ok, just a thought on feasibility though: you can always pass a 'java.io.Reader' pointing your js file, to engine.eval() but if this is a web application then you're heading for disaster. You'd be better off keeping the encrypt() functionality from sjcl.js in a separate file (say encrypt.js), including this file into sjcl.js.

You can then read encrypt.js once and cache its contents in a static String in your java class. You can then pass this string to engine.eval() without I/O performance impact.

Community
  • 1
  • 1
Ryan Fernandes
  • 8,238
  • 7
  • 36
  • 53
  • even this page doesnt have wat i m looking for class EvalFile { public static void main(String[] args) throws Exception { ScriptEngineManager factory = new ScriptEngineManager(); ScriptEngine engine = factory.getEngineByName("JavaScript"); engine.eval(new java.io.FileReader(args[0])); } } Let us assume that we have the file named "test.js" with the following text: println("This is hello from test.js"); We can run the above Java as java EvalFile test.js where to place this js file??? – Varun Dec 06 '10 at 05:44
  • got it... but simple alert is also not working for me :( public static void javaScriptExecute(String data) throws ApplicationException{ ScriptEngineManager factory = new ScriptEngineManager(); ScriptEngine engine = factory.getEngineByName("JavaScript"); try { engine.eval("print('Hello, World')"); } catch (ScriptException ex) { ex.printStackTrace(); } } this method doesnt show any alerts to me :( – Varun Dec 06 '10 at 05:56
  • @varun: if it doesnt print Hello, World on the console, then you have other problems. What does your stack trace say? – Ryan Fernandes Dec 06 '10 at 06:09
  • got the problem this feature is there in java6 not in java5 i have to use java5 only so will need something related to "Rhino" only – Varun Dec 06 '10 at 06:23
0

I think following articles will help you:

EDIT:

Rhino:

I think reading the documentation will help you to implement this.

There are also some other options:

Naveed
  • 41,517
  • 32
  • 98
  • 131
  • this 1 is using applets, but u can never b sure that applet will work correctly... as mentioned in disadvantages they are slow... so cant be sure... – Varun Dec 06 '10 at 05:10
  • @varun: Are you not developing web application ? – Naveed Dec 06 '10 at 05:14
  • @ Naveed - yes it will be webapplication thing is that js is already written i dont wanna rework on converting js to java and want to explore java js communication i have worked on applet js communication before but it didnt ensured me a correct response every time and required JRE on client side – Varun Dec 06 '10 at 05:17
  • pls also check http://stackoverflow.com/questions/4344661/i-am-looking-forward-to-call-some-javascript-method-from-a-java-class they have described Rhino a bit but i cant understand that... – Varun Dec 06 '10 at 05:23
  • Oh. I think I did not understand your problem. Anyway there some more links. – Naveed Dec 06 '10 at 05:41