dcsimg

History of Java: Core Features of Java 6

Today we continue our series on the history of Java as we dive into the new features of Java SE 6. We will be looking at some important changes from the release and look at how they work in code examples.

The release of Java 6 added many new features in the areas of scripting, web services, databases, pluggable annotations, and security. Compatibility, quality, and stability were also improved in this iteration of Java. In addition, JConsole became officially supported, alongside JavaDB - two great additions to the programming language.

In this article, I describe some of the more important core language enhancements for JDK 6. You can paste or type the examples below directly into your integrated development environment (IDE) or code editor. Following this, you can play around with the code and try out your own applications of the code/features. Note: The naming conventions included in these Java code snippets provided below are used to make understanding and organizing the examples much easier.

Read the previous article on Java 5.

Java SE 6 Release Details

  • Java SE 6 Release Date: 23-12-2006
  • Java SE 6 Code Name: Mustang

Java SE 6 New Features

Below are some of the highlights of new features added to Java SE 6, including integrated web services, the Java Compiler API, and scripting language support. I encourage you to type the code into your IDE to get a better feel for how the language works. Feel free to change values in data types or other sections of code to test out different results.

Integrated Web Services

Starting in version 6.0 of JDK, Java began allowing the creation, publishing and lookup of web services through annotations and command-line tools. The below is located in the package 'java6.features'

  package java6.features;   
  import javax.jws.WebMethod;   
  import javax.jws.WebService;   
  import javax.xml.ws.Endpoint;   
   
  /**   
  * @author sumith_puri   
  *   
  * credits: https://weblogs.java.net/blog/vivekp/archive/2006/12/webservices_in.html   
  *    
  * apt -d sample java6_WebServices.java   
  * java -cp sample java6.features.java6_WebServices   
  * wsimport -p client -keep http://localhost:8080/calculator?wsdl   
  */   
  @WebService   
  public class java6_WebServices {   
    @WebMethod   
    public int add(int a, int b) {   
       return (a+b);   
    }   
    public static void main(String[] args) {   
       java6_WebServices calculator = new java6_WebServices();   
       Endpoint endpoint = Endpoint.publish("http://localhost:8080/calculator", calculator);   
    }   
  }   


 

From the same directory as the root of the above source file package, you should type the following:

apt -d sample java6_WebServices.java  

Next, enter the following Java code to deploy the web services at the specified URL:

java -cp sample java6.features.java6_WebServices

After that, you can create the web client by entering the following command, which will create the client stubs:

wsimport -p client -keep http://localhost:8080/calculator?wsdl 

The generated sources can be Imported from the 'client' folder. Now, onto our client project.

With the imported client stubs, you can create a client application using the following Java code. Try and execute the code on the command-line, though you can also use Eclipse, an IDE, or a code editor of your choosing: Run As > Java Application

  package java6.features;   
  import javax.jws.WebMethod;   
  import javax.jws.WebService;   
  import javax.xml.ws.Endpoint;   
   
  /**   
  * @author sumith_puri   
  *   
  * credits: https://weblogs.java.net/blog/vivekp/archive/2006/12/webservices_in.html   
  *    
  * apt -d sample java6_WebServices.java   
  * java -cp sample java6.features.java6_WebServices   
  * wsimport -p client -keep http://localhost:8080/calculator?wsdl   
  */   
  @WebService   
  public class java6_WebServices {   
    @WebMethod   
    public int add(int a, int b) {   
       return (a+b);   
    }   
    public static void main(String[] args) {   
       java6_WebServices calculator = new java6_WebServices();   
       Endpoint endpoint = Endpoint.publish("http://localhost:8080/calculator", calculator);   
    }   
  }  

Java Compiler API

One of the most powerful and dynamic features in Java 6 that I consider game changing is the Java Compiler API. It helps to create artifacts - such as plugins, filters, and extensions for dynamic execution ( either customer-based or client-based). All of this is achieved using dynamic compilation from the Java Compiler API.

  public class java6_DynamicCompilation {   
   
    public static void main(String[] args) throws Exception {        
       System.out.println("Testing Dynamic Compilation....");   
       System.out.println("plugin\\Plugin.java");      
       String fileToStream = "extension" + java.io.File.separator + "Extension.java";   
       JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler();           
       FileOutputStream fileOutputStream = new FileOutputStream("errors.txt");   
       int compilationResult = javaCompiler.run(null, null, fileOutputStream,    
                              "-verbose", fileToStream);        
       System.out.println("Compilation Result..." + compilationResult);   
    }   
  }   

The following code should be provided under 'Extension'. You can name it 'Extension.java'

 package extension;   
     
  public class Extension {   
    public boolean filter(String data) {        
       boolean flag=false;   
       if(data.contains("fish")) {           
         flag=true;   
       }   
       return flag;   
    }   
    
    public static void main(String[] args) {   
    }   
  }   

Run java6_DynamicCompilation and obtain Extension.class as a dynamically compiled class under the 'extension' folder. Then, the generated class can be executed using Reflection (this can be used in real-world products or solutions) or from the command-line:

java -cp . extension.Extension

Java 6 Adds Scripting Language Support

Another powerful feature added in Java 6 is the fact that you can execute JavaScript - and other scripting languages like TCL, Ruby, Groovy, and so on - from within Java code. This was made possible through ScriptingEngine. The example code snippet below shows us how to use JavaScript within Java code:

 package java6.features;   
  import java.io.File;   
  import java.io.FileReader;   
  import javax.script.Invocable;   
  import javax.script.ScriptEngine;   
  import javax.script.ScriptEngineManager;   
     
  public class java6_Scripting {   
    public void todaysLogic() throws Exception {        
     // dynamically change script - invoke code in groovy, js - even tcl!   
     ScriptEngineManager manager = new ScriptEngineManager();   
     ScriptEngine jsEngine = manager.getEngineByName("JavaScript");   
     System.out.println(jsEngine.toString());   
     if (jsEngine instanceof Invocable) {   
        try {   
          File file = new File("market.js");   
        } catch (Throwable e) {   
          e.printStackTrace();   
        }         
        FileReader reader = new FileReader("forex.js");         
        jsEngine.eval(reader);         
        ((Invocable) jsEngine).invokeFunction("current");   
      }   
    }   
    public static void main(String[] args) throws Exception {   
       java6_Scripting scripting = new java6_Scripting();   
       scripting.todaysLogic();   
    }   
  }   

You must place the following JavaScript code in your classpath, preferably within the root folder of your Eclipse Project. You should name it 'forex.js'

 function current() {   
   var x = "", i;   
   for (i=0; i<100; i++) {   
     x="";   
     x = x + "The Number Is " + i;   
     println(x);   
   }    
  }   


 

GitHub Repository for Code Samples

https://github.com/sumithpuri/skp-code-marathon-thimphu

Look out for future articles, where we cover the new features made available in Java 7!

Sumith is a Principal Java/Java EE Architect, a Hard-Core Java // Jakarta EE Developer with 16y 04m of Progressive Work Experience. [Senior Member, ACM; Senior Member, IEEE, DZone Core, Member, CSI*; DZone MVB, Java Code Geek;]. He holds a Bachelor of Engineering (Information Science & Engineering) from Sri Revana Siddeshwara Institute of Technology, Bangalore, India. Also, He has Completed his Executive Program (Data Mining & Analytics) from Indian Institute of Technology, Roorkee. He has also completed his Executive Certificate Program (Entrepreneurship) from Indian Institute of Management, Kashipur. Additionally, He is a SCJP 1.4, SCJP 5.0, SCBCD 1.3, SCBCD 5.0, BB Spring 2.x*, BB Hibernate 3.x*, BB Java EE 6.x*, Quest C, Quest C++ and Quest Data Structures. He is an Ex-Yahoo, Symantec, Huawei, Oracle*, OpenText*, Finastra* (*Original Companies Acquired)