Sunday, August 15, 2010

Rhino JavaScript Tutorial, Rhino js using java code, JavaScript interface

Download js-1.6R7.jar from net. Or you can use this pom.xml to download this jar file
<dependency>
<groupId>rhino</groupId>
<artifactId>js</artifactId>
<version>1.6R7 </version>
</dependency>

How to setup rhino js in console:

I have put my this above jar into D:\.m2\repository\rhino\js\1.6R7\js-1.6R7.jar

creat one bat file like rhino.bat and put this command
java -classpath .;D:\.m2\repository\rhino\js\1.6R7\js-1.6R7.jar org.mozilla.javascript.tools.shell.Main

save it.

Now double click on that batch file you will get
js> prompt.

now you can check bit code here
js> print('Binod')
Binod
js> 37+57
94
js>

for quiting:

quit();

js> quit();


You can open windows application using js
js> runCommand("notepad"); // It will open notepad
0
js> runCommand("mspaint");
0
js> runCommand("calc");
0
js> runCommand("cmd", "/C", "date /T")
Fri 08/13/2010

create one java script like abc.js
function check(name) {
for(i=0;i<10;i++){
print(name);
}

}


js> load("abc.js");
js> check("Binod");
Binod
Binod
Binod
Binod
Binod
Binod
Binod
Binod
Binod
Binod
js>

How to use the Rhino javascript embedded into java code



import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

/**
* @author Binod Suman
*/
public class Demo {


public static void main(String[] args) {
new Demo().check();
}

public void check(){
ScriptEngineManager engineMgr = new ScriptEngineManager();
ScriptEngine engine = engineMgr.getEngineByName("JavaScript");
try {
engine.eval("function sayHello(s,c) {" +" println('Hello, world! '+s+' '+c);" +"}");
Invocable invocableEngine = (Invocable) engine;
invocableEngine.invokeFunction("sayHello","Binod","Tecnotree");
} catch (ScriptException ex) { ex.printStackTrace();}
catch(Exception e){e.printStackTrace();}
}

}

Useful link:
http://www.mozilla.org/rhino/tutorial.html
Good Tutorial : http://www.mozilla.org/rhino/ScriptingJava.html
http://java.sun.com/developer/technicalArticles/J2SE/Desktop/scripting/

24 comments:

  1. Thanks for the sample code. Have you found a method to load and execute an external JavaScript file from Rhino embedded in Java code?

    ReplyDelete
    Replies
    1. Hi Sutch,

      Revoluza Anmeldung. Jetzt bei Revoluza anmelden und Dein Online Business am Adult Entertainment starten!

      visit my site : online geld verdienen


      Delete
  2. can someone explain me the scenarios where i can use javascript in java program ??

    ReplyDelete
  3. Malaysia & Singapore & brunei ultimate online blogshop for wholesale & supply korean accessories, accessories, earstuds, choker, rings, hair, bracelet & trinket accessories.
    Offer 35 % wholesale discount. Ship Worldwide
    my site - binary option brokers

    ReplyDelete
  4. Unquestionably imagine that which you said. Your favourite justification appeared to be
    at the web the easiest thing to be mindful of. I say to you, I definitely get irked whilst people
    think about issues that they just don't know about. You controlled to hit the nail upon the top and defined out the entire thing with no need side effect , other folks could take a signal. Will probably be back to get more. Thanks

    My web site :: Geld Verdienen im Internet
    My page: schnell geld verdienen

    ReplyDelete
  5. Thanks for sharing your info. I truly appreciate your efforts and I will be waiting for your further
    write ups thanks once again.

    Feel free to surf to my web site binary options india
    my website > one touch binary options

    ReplyDelete
  6. Yesterday, while I was at work, my cousin
    stole my iphone and tested to see if it can survive a thirty foot drop, just so she can
    be a youtube sensation. My iPad is now destroyed and she has
    83 views. I know this is completely off topic but I had to share it with someone!


    My webpage - binary options trading system

    ReplyDelete
  7. I am sure this post has touched all the internet
    people, its really really good article on building up new blog.


    My site; Geld verdienen Im Internet

    ReplyDelete
  8. Hello! Someone in my Myspace group shared this site with us so I came to take
    a look. I'm definitely enjoying the information. I'm bookmarking and will be tweeting this to my followers!
    Excellent blog and wonderful design and style.


    Feel free to surf to my blog; how to make money free online
    My webpage :: earn Money Writing reviews

    ReplyDelete
  9. Helpful info. Fortunate me I found your site unintentionally, and I'm surprised why this coincidence didn't happened
    in advance! I bookmarked it.

    My blog post - online Geld verdienen

    ReplyDelete
  10. Thanks for finally talking about > "Rhino JavaScript Tutorial, Rhino js using java code, JavaScript interface" < Liked it!

    My homepage ... make
    money online for free

    ReplyDelete
  11. It іѕ apрrοpriаte time to maκе ѕome plans fοr thе future anԁ it іs tіme to be happy.

    I've read this post and if I could I desire to suggest you some interesting things or tips. Perhaps you can write next articles referring to this article. I wish to read even more things about it!

    My blog post: custom galaxy s3 case

    ReplyDelete
  12. It іs pегfect tіme tο make
    sοme ρlаns fог the futuге
    and іt is timе to be happy. ӏ've read this post and if I could I wish to suggest you some interesting things or tips. Perhaps you could write next articles referring to this article. I want to read even more things about it!

    Here is my web blog - iphone 5 case top 10

    ReplyDelete
  13. I all the time emailеd thiѕ wеbpаge pοst pagе to all mу associates,
    fοr thе reason that іf liκе to reаd it after that my lіnκѕ will too.


    Αlѕo vіsit my homepage iphone accessories

    ReplyDelete
  14. I just like the helpful infο уou provide for your artіcles.
    I will bookmark yοur wеblog and tаκe a look at onсe more right here regularly.
    I'm moderately sure I'll learn many new ѕtuff propег here!
    Goοd luck for the next!

    Also visit my ѕite: www.Arab-Face.com

    ReplyDelete
  15. I wаѕ suggestеd this web site
    by my сοuѕin. I'm no longer sure whether or not this publish is written through him as no one else recognize such designated approximately my problem. You are incredible! Thank you!

    My web-site - iphone 4 cases

    ReplyDelete
  16. I really like what you guys tend to be up too. This type of
    clever work and coverage! Keep up the great works guys I've added you guys to blogroll.

    Here is my website ... Going at whitekidneybeanreview.com

    ReplyDelete
  17. Hi, i read your blog from time to time and i own a similar one and i was just wondering
    if you get a lot of spam remarks? If so how do you reduce it, any plugin or
    anything you can recommend? I get so much lately it's driving me crazy so any support is very much appreciated.

    my weblog: how to get pregnant fast

    ReplyDelete
  18. I am sure this paragraph has touched all the internet users, its really really good article on
    building up new webpage.

    Visit my blog; Buy lift serum

    ReplyDelete
  19. This design is wicked! You certainly know how to keep a reader amused.
    Between your wit and your videos, I was almost moved to start
    my own blog (well, almost...HaHa!) Fantastic job. I really enjoyed what you had
    to say, and more than that, how you presented it.
    Too cool!

    Here is my web blog :: uy Le derme luxe

    ReplyDelete
  20. hop over to these guys replica bags from china click to find out more dior dolabuy have a peek at these guys replica louis vuitton bags

    ReplyDelete