Thursday, September 13, 2007

Invoke JS functions through J2SE6.0

The The Java SE 6 installation comes with sample applications to demonstrate many of the release's new features. One of these samples, ScriptPad, is a 99 percent JavaScript application that loads and executes other scripts through the Java scripting API. It uses Java to load the Rhino JavaScript engine and execute the five script files that are built into ScriptPad. Included in the sample/scripting/scriptpad folder within your Java SE 6 installation root folder, ScriptPad comes complete with a NetBeans 5.5 project file, which you can use to easily build and execute it.

You can also use the application (through the Java Scripting API) to run other scripts that you've written.For instance, the script shown within the ScriptPad window in Figure 1 accesses Java's static System object, invokes its method (getProperties), uses the resulting Properties object to get the name of the host operating system, and outputs the result in an alert window.

This is a sample code to combine the scripting and Java. It demonstrate how to integration really is.The following Java code loads the Rhino JavaScript engine, loads the script contained within the file browse.js, and executes the function named browse

Code:

import java.util.*;
import java.io.*;
import javax.script.*;

public class Main
{
public Main()
{
try {
ScriptEngineManager m = new ScriptEngineManager();
ScriptEngine engine = m.getEngineByName("javascript");
if ( engine != null )
{
InputStream is = this.getClass().getResourceAsStream("browse.js");
Reader reader = new InputStreamReader(is);
engine.eval(reader);
Invocable invocableEngine = (Invocable)engine;
invocableEngine.invokeFunction("browse");
}
}
catch ( Exception e ) {
e.printStackTrace();
}
}

public static void main(String[] args)
{
Main m = new Main();
}
}

This browse.js functions uses new Swing Desktop API in JDK 6. To use this with scriptpad, open this in scriptpad and use "Tools->Run Script" menu.

browse.js

function browse() {
var guiPkgs = new JavaImporter(java.awt, java.awt.event,
javax.swing, javax.swing.undo,
javax.swing.event, javax.swing.text);
with (guiPkgs) {
var desktop = null;
// Before more Desktop API is used, first check
// whether the API is supported by this particular
// virtual machine (VM) on this particular host.
if (Desktop.isDesktopSupported()) {
desktop = Desktop.getDesktop();
}
else
{
alert("no desktop support");
return;
}
if (desktop.isSupported(Desktop.Action.BROWSE)) {
desktop.browse(new java.net.URI("www.ericbruno.com"));
}
else
{
alert("no browser support");
}
}
}
if (this.application != undefined) {
// add "Browse" menu item under "Tools" menu
this.application.addTool("Browse", browse);
}

This paradoxical example where Java invokes a script that in turn invokes Java to load an HTML page demonstrates just how dynamic the scripting API can be.

3 comments:

adam said...

Hi, this is not so related to your page, but it is the site you asked me 1 month ago about the abs diet. I tried it, worked well. Well here is the site

schooltiger said...

look at these designs
http://www.free-css-templates.com/css-templates/

Vaibhav said...

Nice blog !

Yes JDK6 is going powerful and hope 7 to me more than 6.

Scripting is one of the feature which developers are demanding.