Friday, December 7, 2012

Cool Feature of Java 6- JSR 223 & JSR 199


JSR 223- Scripting for the Java Platform

   Java introduced the support for scripting languages for java platform from Java- 6.

  1.  JSR 223 - Scripting for the Java Platform helps developers integrate the standard java code with the scripting language by a standard framework and API. With this API we can
    1. Access and control Java objects from a scripting environment
    2. Create web content with scripting languages
    3. Embed scripting environments within Java based applications
  1. Java 6 by default ships Javascript Engine Implementation called Rhino provided by the Mozilla
  2. New scripting engines that compliance JSR 223 can be plugged in to avail the feature. So now the Python, Ruby scripting advantages can also be availed.
  3.  With this new API, now the power of scripting can be availed from the java.

Example: In the below piece of code, a javascript function called printName(name) is evaluated from java and the argument is passed is the java object.
public static void main(String[] args) {
            // create Script-Engine-Manager object
            ScriptEngineManager factory = new ScriptEngineManager();
            // Get Script Engine from factory
            ScriptEngine engine = factory.getEngineByName("js");

            // Create binding variable to pass value to script
            Bindings bindings = engine.createBindings();
            bindings.put("name", "JAS Technologies");

            // evaluate JavaScript code from String
            try {
                  engine.eval("printName(name);" +
                                    "function printName(name){" +
                                          "print(name+' company');" +
                                    "};",
                              bindings);

            } catch (ScriptException e) {
                  e.printStackTrace();
            }
      }



JSR 199 – java Compiler API

1.       One of the cool features introduced in java 6 is to compile the java source file dynamically.
2.       As a part of JSR 199-JavaTM Compiler API, there is a standard java Compiler API which defines the interfaces for java compiler functions and a standard service provider framework by which vendor can provide implementation for the interfaces.
3.       The API for compilation is now available part of javax.tool package and ships with Java 6
4.       So having said above what a developer can do with this is
a.       Dynamically compile java source file
b.      Can compile multiple files
c.       Can compile a java source from a string object
d.      Also several advanced options are also available. Check the API for complete details.
  Below is the small example for compiling the java source file dynamically

      public static void main(String[] args) {
            //File to be compiled
            String fileName = "D:/jas/workspace/com/test/compile/CompileMe.java";
            //Get the java compiler
            JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler();
            //Compile the java file. Look at the API to find more on options
            int returnVal = javaCompiler.run(null, null, null, fileName);
      }

   Content from CompileMe.java used in the above example

package com.test.compile;

public class CompileMe {
      public static void main(String[] args) {
            System.out.println("The JAS Technologies");
      }
}

No comments:

Post a Comment