Sunday, March 16, 2014

Running GlassFish 4 on Java 8: Experiment with Java 8 Functionality in Java EE 7 Applications

Do you want to use the features of Java 8 with your Java EE 7 application?  It is possible to run GlassFish 4.0 under JDK 8.  To explicitly define which JDK GlassFish 4.0 uses, set the AS_JAVA property within your <glassfish-path>\glassfish\config\asenv.conf configuration file equal to your JDK 8 installation.  On OS X, this would look like the following:

AS_JAVA="/Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home"

Once you've set this property and then start the GlassFish 4.0 server, it should be running under JDK 8, allowing any of the applications to utilize new Java 8 features, such as streams or lambdas.



Note:  This is for experimental use only, as Java EE 7 has not been sanctioned for use with Java 8 at the time of this post.  


I have successfully configured GlassFish 4.0 to run under Java 8 in my environment, and tested the use of streams within a Java EE 7 application without issue.  Here are some sources demonstrating basic use of streams within a CDI bean:

@Named
@SessionScoped
public class PoolController implements Serializable {
    List pools = new ArrayList();
    
    public PoolController(){
        pools.add("Pool One");
        pools.add("Pool Two");
        pools.add("Pool Three");
    }
    
    public String getPool(){
        System.out.println("The number of pools " + pools.stream().count());
        return  pools.stream().findFirst().toString();
    }

}

No comments:

Post a Comment

Please leave a comment...