Monday, September 30, 2013

Java EE6 Totorial

I will make this quick, and summarize challenges faced while following JavaEE6 tutorial available on Oracle website. I was following all the steps mentioned in the tutorial, trying to use NetBeans with Glassfish as per the available instructions.

1) Update tool installed with NetBeans didn't work for me. I was trying to download the tutorial through updatetool, but consistently getting below exception.

I gave up after some trial and error, and thought of downloading the tutorial from SVN repositories. Thanks to the contributors of Stackoverflow.com, this URL contains all the tutorials. I switched to my old friend Eclipse and downloaded all the tutorials (you may need to set the proxy for subclipse to download the tutorials, configuration file is available at : C:\Users\<Windows user Id>\AppData\Roaming\Subversion in Windows7.

After downloading the tutorial, running 'ant' from command line for 'hello1' project failed with below error:

C:\glassfishv3\glassfish\docs\javaee-tutorial\examples\bp-project\command-line-ant-tasks.xml:112: c:\glassfish3\lib does not exist.

My glassfish installation was at 'C:\glassfishv3' and I'm lil surprised because this was default folder chosen by Glassfish installation. Anyways to get rid of this error I had to modify two files:

1) javaee-tutorial\examples\bp-project\app-server.properties (Modify javaee.home=c:\\glassfishv3) and
2) javaee-tutorial\examples\bp-project\app-server-ant.xml (change value of javaee.classpath.prefix property to ${javaee.home}/lib)

Original content
    <condition property="javaee.classpath.prefix" value="${javaee.home}/lib">
        <isfalse value="${is.glassfish.v3}"/>
    </condition>

Modified content
    <condition property="javaee.classpath.prefix" value="${javaee.home}/glassfish/lib">
        <isfalse value="${is.glassfish.v3}"/>
    </condition>

After making these changes ... it seems build is working:

C:\glassfishv3\glassfish\docs\javaee-tutorial\examples\web\hello1>ant
Buildfile: C:\glassfishv3\glassfish\docs\javaee-tutorial\examples\web\hello1\build.xml
-pre-init:
init:
default-ear:
-pre-compile:
bpp-actual-compilation:
     [echo] Compiling hello1
     [echo] javaee.classpath.prefix is: c:\glassfishv3/glassfish/modules
     [echo] is.glassfish.v3 is: true
    [javac] Compiling 1 source file to C:\glassfishv3\glassfish\docs\javaee-tutorial\examples\web\hello1\build\web\WEB-INF\classes
-post-compile:
compile:
package:
    [mkdir] Created dir: C:\glassfishv3\glassfish\docs\javaee-tutorial\examples\web\hello1\build\war
bp-copy-classes:
    [mkdir] Created dir: C:\glassfishv3\glassfish\docs\javaee-tutorial\examples\web\hello1\build\war\WEB-INF\classes
     [copy] Copying 1 file to C:\glassfishv3\glassfish\docs\javaee-tutorial\examples\web\hello1\build\war\WEB-INF\classes
     [copy] Copying 5 files to C:\glassfishv3\glassfish\docs\javaee-tutorial\examples\web\hello1\build\web
     [copy] Copying 5 files to C:\glassfishv3\glassfish\docs\javaee-tutorial\examples\web\hello1\build\war

bpp-package-persistence-unit-war:
    [mkdir] Created dir: C:\glassfishv3\glassfish\docs\javaee-tutorial\examples\web\hello1\build\war\WEB-INF\classes\META-INF
     [copy] Warning: Could not find file C:\glassfishv3\glassfish\docs\javaee-tutorial\examples\web\hello1\src\conf\persistence.xml to copy.
     [copy] Warning: Could not find file C:\glassfishv3\glassfish\docs\javaee-tutorial\examples\web\hello1\src\conf\persistence.xml to copy.

bpp-package-persistence-unit-ejb-jar:
package-persistence-unit:
copy-resource-file:
      [jar] Building jar: C:\glassfishv3\glassfish\docs\javaee-tutorial\examples\web\hello1\build\hello1.war
   [delete] Deleting directory C:\glassfishv3\glassfish\docs\javaee-tutorial\examples\web\hello1\build\war

bpp-copy-dist:
    [mkdir] Created dir: C:\glassfishv3\glassfish\docs\javaee-tutorial\examples\web\hello1\dist
     [copy] Copying 1 file to C:\glassfishv3\glassfish\docs\javaee-tutorial\examples\web\hello1\dist

default-not-ear:
default:

BUILD SUCCESSFUL

Drop a comment, if you've any question/suggestions. 

Wednesday, September 4, 2013

Setting up Tomcat7 for JSF2.0 and CDI (Core Java Server Faces 3rd edition)

In the book  "Core Java Server Faces 3rd edition", we've option to run our first example either in any of the J2EE6 compatible App-servers like Glassfish/JBoss7 etc or Tomcat.

Moreover you can use JSF annotations (@ManagedBean) or CDI (@Named). I was planning to use CDI to work on the basic example for JSF and trying to set up Tomcat7 with the help of this post . Earlier I thought weld-servlet.jar would be the only Jar file needed to make this work, but that didn't work.

At some point while setting up the app, I was getting this error:

Sep 4, 2013 11:05:12 PM org.apache.catalina.core.StandardContext listenerStart
SEVERE: Exception sending context initialized event to listener instance of class org.jboss.weld.environment.servlet.Listener
java.lang.NoClassDefFoundError: javax/enterprise/inject/spi/Extension
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
After some googling and inspecting available Jars, i downloaded Weld version 2.0.3 Final from Weld Distribution Download page  and copied following Jars in my application WEB-INF/lib folder 

cdi-api.jar
weld-api.jar
weld-core-impl.jar
weld-core-jsf.jar
weld-servlet.jar
weld-servlet-core.jar
weld-spi.jar 

Additionaly added jar for JSF2.0 (javax.faces-2.2.1.jar) also in WEB-INF/lib from http://javaserverfaces.java.net/download.html.

Finally things started working. I'm not attaching the source code used for running the application, any simple JSF application that needs CDI in Tomcat environment would need these changes and configuration.


References:
http://www.theserverside.com/tutorial/Working-with-CDI-and-JSF-20-on-Tomcat-7-Configuring-Weld