Simple JavaScripting from Java

Nothing like writing a "hello world" program with new Java APIs! A simple Java program to use JSR 223 (javax.script) API with Mustang is as follows:


//import package
import javax.script.\*; 

public class Main {
    public static void main(String[] args) {
        try {
            // create a script engine manager
            ScriptEngineManager manager = new ScriptEngineManager();
            // create script engine for JavaScript
            ScriptEngine jsengine = manager.getEngineByName("js");
            // evaluate JavaScript code from String
            jsengine.eval("print('hello world')");
        } catch (ScriptException se) {
            // Handle script exception here..
            // FIXME: do a better job here!
            se.printStackTrace();
        }
    }
}


Comments:

Is their any download available of the RI? :-)

Posted by Alex Goncalves on August 08, 2005 at 04:27 PM IST #

I've played with it, too, see this. Interesting session at Java One, btw.

Posted by Roman Strobl on August 08, 2005 at 06:59 PM IST #

Alex: Mustang build 40 or above contains javax.script API and JavaScript engine reference implementation. You can download Mustang builds from https://mustang.dev.java.net
Roumen: nice blog entry! Thanks.. I saw it after I wrote this entry. I am planning to write more on scripting [if I can overcome my laziness :-)]

Posted by Sundararajan on August 09, 2005 at 06:52 AM IST #

You can make it a bet shorter, esp since "hello world" should be short and sweet.
//import package
import javax.script.\*; 

public class Main {
    public static void main(String[] args)
        throws Throwable {
            // create a script engine manager
            ScriptEngineManager manager = new ScriptEngineManager();
            // create script engine for JavaScript
            ScriptEngine jsengine = manager.getEngineByName("js");
            // evaluate JavaScript code from String
            jsengine.eval("print('hello world')");
    }
}

Posted by David Spencer on August 09, 2005 at 02:21 PM IST #

This way it can be even shorter...
import javax.script.\*; 

public class Main {
    public static void main(String[] args) throws Throwable {
         ScriptEngineManager manager = new ScriptEngineManager();
         ScriptEngine jsengine = manager.getEngineByName("js");
         jsengine.eval("print('hello world')");
    }
}

Posted by Roman Strobl on August 09, 2005 at 04:48 PM IST #

And what about this?
public class Main {
    public static void main(String[] args) throws Throwable {
         new javax.script.ScriptEngineManager().getEngineByName("js").eval("print('hello world')");
    }
}

Posted by Pajosh on August 09, 2005 at 06:44 PM IST #

Hello, I was trying to write an intro blog entry on this -- with comments explaning the steps. Looks like Perl-style one-liners are preferred instead :-) BTW, we are adding a command line utility called "jrunscript" -- so, you will be able to say this:
<code>

jrunscript -e "print('hello world')"

</code>

Posted by Sundararajan on August 10, 2005 at 05:22 AM IST #

As usual I am trying to understand what the use of this is. Why would you need something like this ?

Posted by Mohan Radhakrishnan on August 10, 2005 at 11:15 AM IST #

Mohan: Are you asking about the use of 'jrunscript' tool? [or about this blog entry?]. Almost all script engines come with a shell/GUI tool to try out scripts interactively and/or run scripts from files in batch mode (eg. Rhino, BeanShell). jrunscript is script language independent shell -- user can select language with -l option (with default being JavaScript) and supports interactive, batch modes.

Posted by Sundararajan on August 11, 2005 at 02:36 AM IST #

dhgfhgfg

Posted by guest on September 16, 2005 at 12:14 AM IST #

i know FastScript - cross-platform multi-language scripting engine, (at present moment PascalScript, C++Script, BasicScript, JScript). http://www.yaodownload.com/software-development/components-libraries/fastscript/

Posted by joe on April 21, 2006 at 11:58 PM IST #

[Trackback] Sun Microsoft announced that they just released Java SE 6. Depending on who you ask, this is a great thing, or just so so. Joey deVilla, from Global Nerdy, was not impressed with Java 6. Of  course, he has made a significant effort to avoid getting su...

Posted by Teckie Life at Southwest MSU... on December 15, 2006 at 02:10 AM IST #

Post a Comment:
Comments are closed for this entry.
About

sundararajan

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today
Bookmarks
Links

No bookmarks in folder

Blogroll

No bookmarks in folder

News

No bookmarks in folder