Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

SKP's Java/Java EE Gotchas: Revisiting Java SE 6 Features!

DZone's Guide to

SKP's Java/Java EE Gotchas: Revisiting Java SE 6 Features!

We continue our trip through the past by examining what Java 6 brought to the table, namely integrated web services and scripting language support.

· Java Zone ·
Free Resource

Get the Edge with a Professional Java IDE. 30-day free trial.

So, preparing for an interview? Want to revisit Java SE 6 features? Trying to recollect or revise a Java SE programming construct? Let me take you back in time to what was introduced first in Java SE 6. Join me for this tutorial series on Java as we all eagerly await the official release of Java SE 9!

Java SE 6 Release Date: 23-12-2006

Java SE 6 Code Name: Mustang

Java SE 6 Highlights

This release added many enhancements in the fields of web services, scripting, databases, pluggable annotations, and security, as well as quality, compatibility, and stability. JConsole was officially supported, and Java DB support has been added.

I have provided some of the most important core language enhancements for JDK 6.0, along with code samples. The examples provided below can be directly pasted into your IDE, and you may name the class as provided.

Integrated Web Services

From JDK6 onward, Java allowed the creation, publishing, and looking up of web services through a mixture of annotations and command-line utilities. The below is located in package 'jdk6.features':

 package jdk6.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 jdk6_WebServices.java  
  * java -cp sample jdk6.features.jdk6_WebServices  
  * wsimport -p client -keep http://localhost:8080/calculator?wsdl  
  */  
 @WebService  
 public class jdk6_WebServices {  
      @WebMethod  
      public int add(int a, int b) {  
           return (a+b);  
      }  
      public static void main(String[] args) {  
           jdk6_WebServices calculator = new jdk6_WebServices();  
           Endpoint endpoint = Endpoint.publish("http://localhost:8080/calculator", calculator);  
      }  
 }  


In the same folder as the root of the above source file/package, you should type the following:

apt -d sample jdk6_WebServices.java

Then type the following to deploy the web service to the specified URL:

java -cp sample jdk6.features.jdk6_WebServices

Now, it is time to create the client. You may use the following command to create the client stubs:

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

We can now import the generated sources from the 'client' folder to Eclipse. Using the imported client stubs, you can create a client application using the following code. Execute it either on the command line or from Eclipse (Run As > Java Application)

 package client;  

 public class JDK6WebServicesClientApp {  
      public static void main(String[] args) {          
           Jdk6WebServicesService service = new Jdk6WebServicesService();  
           Jdk6WebServices serviceProxy = service.getJdk6WebServicesPort();  
           int result = serviceProxy.add(12, 12);  
           System.out.println("Sum: " + result);  
      }  
 }  


Java Compiler API

A powerful feature in JDK6 that really helped create artifacts like as plugins, filters, and extensions for dynamic (customer-based or client-based) execution is Dynamic Compilation using the Java Compiler API.

 public class jdk6_DynamicCompilation {  

      public static void main(String[] args) throws Exception {            
           System.out.println("Testing Dynamic Compilation....");  
           System.out.println("plugin\\Plugin.java");       
           String fileToStream = "plugin" + java.io.File.separator + "Plugin.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 'plugin'. You may name it 'Plugin.java'

 package plugin;  

 public class Plugin {  
      public boolean filter(String data) {            
           boolean flag=false;  
           if(data.contains("fish")) {                 
                flag=true;  
           }  
           return flag;  
      }  

     public static void main(String[] args) {  
      }  
 }  


You may now run the jdk6_DynamicCompilation and obtain Plugin.class as a dynamically compiled class under the 'plugin' folder. Now execute the generated class either using reflection (which you may use in real-world products or solutions) or from the command line:

java -cp . plugin.Plugin

Scripting Language Support

With JDK6, you may execute JavaScript (Tcl, Ruby, Groovy, etc.) from within Java. This is possible through ScriptEngine. The example below shows the usage of JavaScript within Java.

 package jdk6.features;  
 import java.io.File;  
 import java.io.FileReader;  
 import javax.script.Invocable;  
 import javax.script.ScriptEngine;  
 import javax.script.ScriptEngineManager;  

 public class jdk6_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("market.js");              
             jsEngine.eval(reader);              
             ((Invocable) jsEngine).invokeFunction("current");  
         }  
      }  


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

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


Happy programming with JDK6!

Ready Reference on DZone

Get the Java IDE that understands code & makes developing enjoyable. Level up your code with IntelliJ IDEA. Download the free trial.

Topics:
java ,java 6 ,scripting ,dynamic applications ,tutorial

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}