« listopad 2009
PoÚtStČtSoNe
      
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

Navigation

Speaker Profile
Roumen's Weblog
Login
Sun Bloggers
Technorati Profile

Am I popular?

Today's Page Hits: 381

Contacts

Name: Roman Strobl
E-mail: roman dot strobl
at sun dot com

NetBeans

Java Sites

Javalobby
The Server Side
Java Tips
Java Blogs
java.net
java.sun.com
java.cz

Blogs

NetBeans:
Geertjan
Brian Leonard
Gregg Sporar
Lukas Hasik
Ludovic Champenois
Vincent Brabant
Alexis Moussine-Pouchkine
Jullion-Ceccarelli
Tom Ball
Tim Boudreau
Jesse Glick
Petr Blaha
Ruth Kusterer
Jara Uhrik
xzajo
Jan Lahoda
James Branam
nbextras.org

Sun:
Kazem - bug cartoons ;-)
Tor Norbye
Romain Guy
James Gosling
Chief Gaming Officer
Bill Vass
Jim Grisanzio
Jonathan Schwartz

Planets:
Planet Netbeans
Planet Sun
Planet Eclipse

Other:
netbeans-blog.org
Joel Spolsky
Bruce Eckel

License info

Creative Commons License
This work is licensed under a Creative Commons License.

Recent Entries

Map of visits

Locations of visitors to this page
« NetBeans Quick Tip... | Main | Great Refactoring... »
20050705 Úterý červenec 05, 2005
Scripting Support in Mustang - An Example You Can Try

Well, it's 1:30 a.m. but feels like late afternoon. Time shift is a funny thing. I thought that if I can't sleep, I could try to play with the new scripting support in Mustang (JSR-223). Well, it's as simple as this:

Preparing JavaScript code (notice I am using java.lang.StringBuffer inside JavaScript - isn't that cool?):

        input.setText("var out = new java.lang.StringBuffer();\nfor (i=0; i<3; i++)\n   
                                    out.append('hello, world! ('+i+')\\n');");


Executing the code:

	ScriptEngineManager manager = new ScriptEngineManager();
	ScriptEngine jsengine = manager.getEngineByName("js");
        try {
            jsengine.eval(input.getText());
            String out = new String((StringBuffer)jsengine.get("out"));
            output.setText(out);
        } catch (javax.script.ScriptException e) {
            output.setText("ScriptException" + e);
        }

A screenshot:

Full source code is here. To execute, you need to install latest mustang build.

Well, if it's simple as this it may mean future Java code getting "polluted" by scripting code. Programming in scripting languages can be very productive for certain types of problems. Currently, Rhino JavaScript support is available by Sun as reference implementation and PHP support is being written, too. Any other languages may follow - the scripting framework enables any other languages to be plugged in. I really wonder what will be the impact of JSR 223 on web development...
Comments:

You can also use zamples.com, in place of installing last version of JDK. You can copy that code in the window of Zamples, and see result on your right: public class Test { public static void main(String argv[]) { String x = "var out = new java.lang.StringBuffer();\nfor (i=0; i<3; i++)\nout.append('hello, world! ('+i+')\\n');"; javax.script.ScriptEngineManager manager = new javax.script.ScriptEngineManager(); javax.script.ScriptEngine jsengine = manager.getEngineByName("js"); try { jsengine.eval(x); String out = new String((StringBuffer)jsengine.get("out")); System.out.println(out); } catch (javax.script.ScriptException e) { System.out.println("ScriptException" + e); } } }

Posted by Vincent Brabant on červenec 06, 2005 at 12:24 odp. CEST #

Interesting...

Posted by Roman Strobl on červenec 06, 2005 at 05:38 odp. CEST #

Post a Comment:

Comments are closed for this entry.


    Disclaimer: The contents of my blog represent my personal opinions which may differ from official views of my employer, Sun Microsystems.