Java Scripting: One VM, Many Languages
Java Scripting: One VM, Many Languages
Sang Shin
sang.shin@sun.com
javapassion.com
Sun Microsystems, Inc.
Agenda
• Quick overview
• Scripting API
• Java SE 6 Scripting Support
• Demo
• Future Directions
• Resources
Quick Overview
Scripting Languages
• Typically dynamically typed languages
> No need to define variables before you use them
> Many type conversions happen automagically
> Can be good...
> Can be bad...
• Most scripting languages are interpreted
> Perform the script compilation and execution within the
same process
• Very good for fast results for small jobs
> Write application faster, execute commands repeatedly
Different Languages, different jobs
• Perl
> Text processing, report generation
• Bash, sh, ksh
> job control
• Ruby
> Web based applications
Java Programming Language
and Ruby Compared
public class Filter {
public static void main(String[] args) {
List list = new java.util.ArrayList();
list.add(“Tim"); list.add(“Ike"); list.add(“Tina");
Filter filter = new Filter();
for (String item : filter.filterLongerThan(list, 3)) {
System.out.println( item );
}
}
public List filterLongerThan(List list, int length) {
List result = new ArrayList();
for (String item : list) {
if (item.length() >= length) { result.add( item ); }
}
return result;
}
}
Java Programming Language
and Ruby Compared
Ruby!
=> ‘Tina’
Scripting Over
Java Platform
Why Scripting Languages & Java
together?
• Combining scripting languages with the Java
platform provides developers and end-users an
opportunity to leverage the abilities of both
environments
• Use scripting languages for quick and easy
development & testing for certain parts of your
applications
• Use Java programming language and platform for
what it is known for
> Scalable and highly performing business logics
Why Scripting Languages & Java
together?
• Allows end-users to customize the applications
further
Java Platform Supports Scripting
Languages Well!
• Java Language != Java Platform
> Java VM runs “language-neutral” bytecode
> Rich set of Class libraries are “language-neutral”
> “Write once run anywhere” applies to Platform
> Leverage programmer skills and advantages of particular
languages
• Time-tested technologies
> Open-source projects for various languages
> Jakarta BSF
The Virtual Machine
and more...
Java Libraries
VIRTUAL VIRTUAL VIRTUAL MACHINE
MACHINE MACHINE JAVA VIRTUAL MACHINE
= You do = We do
Scripting Framework
& API over Java Platform
Scripting framework
• JSR 223 defines the scripting framework
• It supports pluggable framework for third-party script
engines
> Resembles BSF ActiveX Scripting
> “Java application runs script programs” scenario
• javax.script package
• Optional javax.script.http package for Web scripting
• Part of Java SE 6
• Available for Java 5.0
Scripting API
• ScriptEngine
• ScriptContext, Bindings
• ScriptEngineFactory
• ScriptEngineManager
Interfaces
• ScriptEngine interface—required
> Execute scripts—“eval” methods
> Map Java objects to script variables (“put” method)
• Invocable interface—optional
> Invoke script functions/methods
> Implement Java interface using script functions/methods
• Compilable interface—optional
> Compile Script to intermediate form
> Execute multiple times without recompilation
ScriptEngine API
• ScriptEngine (Interface)
> eval()
> put()
> get()
> getBindings()/setBindings()
> createBindings()
> getContext()/setContext()
> getFactory()
• AbstractScriptEngine
> Standard implementation of several eval() methods
ScriptEngineManager
• Provides the ScriptEngine discovery mechanism
> getEngineByName()
> getEngineByExtension()
> getEngineByMimeType()
> getEngineFactories()
• Developers can add script engines to a JRE
> with the JAR Service Provider specification
Example – Hello world
import javax.script.*;
public class Main {
public static void main(String[] args) throws ScriptException {
// Create a script engine manager
ScriptEngineManager factory = new ScriptEngineManager();
// Evaluate script
engine.eval(script);
// call methods that are defined in Person class public void greet() {
p.work(); System.out.println("Hello, World!");
p.greet(); }
// call methods that are not defined in Person public Object invokeMethod(String name,
Object args) {
// or it's superclass
System.out.println("Why are you calling " +
p.surfTheNet(); name + "?"); }}
p.writeBlog(); }}
Server-side scripting – Phobos
• http://phobos.dev.java.net
• Borrows from Ruby on Rails
> Speed of development
> Well-organized application structure
• Access to enterprise Java
• Javascript libraries
• Support for other technologies
> AJAX
> RSS / Atom
Resources
Resources - scripting.dev.java.net
• BSD License
• Scripting Engines
> jruby, groovy, beanshell, jacl, jaskell, java,
jawk,jelly,jexl,jruby,javascript,jython,ognl,pnuts,scheme,sl
eep,xpath,xslt
• Applications
> NetBeans Scripting module
• Also see coyote.dev.java.net
> NetBeans Scripting IDE
> Jython, groovy support
Resources - references
• JSR-223
> http://jcp.org/en/jsr/detail?id=223
• A. Sundararajan's Blog
> http://blogs.sun.com/sundararajan
• Roberto Chinnici's Blog (serverside scripting)
> http://weblogs.java.net/blog/robc/
• JavaScript Developer Connection
> http://java.sun.com/javascript
Java Scripting:
One VM, Many Languages
Sang Shin
sang.shin@sun.com
javapassion.com
Sun Microsystems, Inc.