java.lang.ClassNotFoundException: javax.faces.webapp.FacesServlet


Problem: Deploying a newly created JSF 2.1 Application on Tomcat 7 and while starting the server, gets the following error, which made me to scratch my head. While working with JSF 1.2, on other IDE’s like MyEclipse, i do not remember coming across this issue.

java.lang.ClassNotFoundException: javax.faces.webapp.FacesServlet

Solution:

I give credit to the author at

http://blog.v-s-f.co.uk/2010/09/jsf-2-1-project-using-eclipse-and-maven-2/

and also i added my own findings in this post.

We need to change the below few files so that the jar files are deployed with the application.

So now open the file org.eclipse.wst.common.component which is in the .settings folder. Delete the lines:

<wb-resource deploy-path=”/WEB-INF/classes” source-path=”/src/test/java” />
<wb-resource deploy-path=”/WEB-INF/classes” source-path=”/src/test/resources” />

Replace them with:

<dependent-module deploy-path=”/WEB-INF/lib”>
<dependency-type>uses</dependency-type>
</dependent-module>

Open .classpath and replace the line: <classpathentry exported=”true” kind=”con” path=”org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER”/> with:

<classpathentry exported=”true” kind=”con” path=”org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER”>
<attributes>
<attribute name=”org.eclipse.jst.component.dependency” value=”/WEB-INF/lib”/>
</attributes>
</classpathentry>

Apart from doing the above steps, without doing this step, still throwed the exception for me. So, do this as well, right click on project -> properties -> java build path and look for the tab Libraries. Make sure that JavaSE 1.6 is available. If not, remove the old one (default is 1.4 in eclipse)  if any, click Add Library on right and add the JavaSE 1.6.

Navigate to Order and Export and select JavaSE 1.6 along with Maven Dependencies.

After selecting Maven Dependencies, navigate to Project Facets, if you do not happen to see the Project Facets list, close the properties dialog box refresh your project and then revisit the Project Facets.

 

In Eclipse, .project should look like

<?xml version=”1.0″ encoding=”UTF-8″?>
<projectDescription>
<name>projname</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.wst.jsdt.core.javascriptValidator</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.common.project.facet.core.builder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.validation.validationbuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.maven.ide.eclipse.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jem.workbench.JavaEMFNature</nature>
<nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.maven.ide.eclipse.maven2Nature</nature>
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
<nature>org.eclipse.wst.jsdt.core.jsNature</nature>
</natures>
</projectDescription>

.classpath should look like

<?xml version=”1.0″ encoding=”UTF-8″?>
<classpath>
<classpathentry kind=”src” output=”target/classes” path=”src/main/java”/>
<classpathentry excluding=”**” kind=”src” output=”target/classes” path=”src/main/resources”/>
<classpathentry kind=”src” output=”target/test-classes” path=”src/test/java”/>
<classpathentry excluding=”**” kind=”src” output=”target/test-classes” path=”src/test/resources”/>
<classpathentry kind=”src” path=”src/main/webapp”/>
<classpathentry exported=”true” kind=”con” path=”org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER”>
<attributes>
<attribute name=”org.eclipse.jst.component.dependency” value=”/WEB-INF/lib”/>
</attributes>
</classpathentry>
<classpathentry exported=”true” kind=”con” path=”org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6″/>
<classpathentry kind=”con” path=”org.eclipse.jst.j2ee.internal.web.container”/>
<classpathentry kind=”con” path=”org.eclipse.jst.j2ee.internal.module.container”/>
<classpathentry kind=”output” path=”target/classes”/>
</classpath>

org.eclipse.wst.common.component as

<?xml version=”1.0″ encoding=”UTF-8″?>
<project-modules id=”moduleCoreId” project-version=”1.5.0″>
<wb-module deploy-name=”projname”>
<wb-resource deploy-path=”/” source-path=”/WebContent”/>
<wb-resource deploy-path=”/WEB-INF/classes” source-path=”/src/main/java”/>
<wb-resource deploy-path=”/WEB-INF/classes” source-path=”/src/main/resources”/>
<dependent-module deploy-path=”/WEB-INF/lib”>
<dependency-type>uses</dependency-type>
</dependent-module>
<wb-resource deploy-path=”/WEB-INF/classes” source-path=”/src/main/webapp”/>
<property name=”context-root” value=”projname”/>
<property name=”java-output-path” value=”/projname/target/classes”/>
</wb-module>
</project-modules>

pom.xml for JS2.1 App

<project xmlns=”http://maven.apache.org/POM/4.0.0&#8243; xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221; xsi:schemaLocation=”http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd”&gt;
<modelVersion>4.0.0</modelVersion>
<groupId>com.proj</groupId>
<artifactId>projname</artifactId>
<version>1.0</version>
<packaging>war</packaging>
<name>clm prowise</name>
<description>    </description>
<repositories>
<repository>
<id>maven2-repository.dev.java.net</id>
<name>Java.net Repository for Maven</name>
<url>http://download.java.net/maven/2</url&gt;
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>2.1.0-b03</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>2.1.0-b03</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>

</project>

  1. #1 by Anonymous on August 26, 2011 - 2:04 am

    I think java.lang.ClassNotFoundException comes because ClassLoader fails to load the class requested at runtime.This is mostly an environmental issue which depends upon whether request class is present in Classpath or not.to solve this error best approach is to first find the jar file in which problematic class exists and then examine your classpath.also don’t confuse this with NoClassDefFoundError which is entirely different , for details see here NoClassDefFoundError vs ClassNotFoundException in Java

  2. #2 by Luciano Ortiz on September 20, 2011 - 2:34 am

    ,Bah tu é Deus! Valeu, segui os passos e funcionou!
    Luciano Ortiz

  3. #3 by muhibb on August 2, 2012 - 9:18 am

    In order to solve ClassNotFoundException, you can add “Java Build Path Entries” to “Project Properties” -> “Deployment Assembly”.

  4. #4 by Eduardo on February 6, 2013 - 2:23 am

    A solução do Muhibb cabe muito bem ao caso.

    Funcionou!

    In order to solve ClassNotFoundException, you can add “Java Build Path Entries” to “Project Properties” -> “Deployment Assembly”.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: