Pawan
Pawan

Reputation: 32331

Maven : ClassNotFound Exception in Web Application

I have created a Web Application in Eclipse IDE using Maven Plugin as shown enter image description here

I have created a Servlet in that Project

The pom.xml generated is

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com</groupId>
  <artifactId>MTest</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>MTest Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <finalName>MTest</finalName>
  </build>
</project>

When i right clicked on pom.xml and executed Run as Maven Install

I couldn't see the class file of my servlet under

C:\Users\sai\OfficeWSApril14\MTest\target\MTest\WEB-INF\classes

So as a result its throwing ClassNotFoundException

Please let me know what I was doing wrong

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building MTest Maven Webapp 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.4.3:resources (default-resources) @ MTest ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ MTest ---
[INFO] No sources to compile
[INFO] 
[INFO] --- maven-resources-plugin:2.4.3:testResources (default-testResources) @ MTest ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\Users\sai\OfficeWSApril14\MTest\src\test\resources
[INFO] 
[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ MTest ---
[INFO] No sources to compile
[INFO] 
[INFO] --- maven-surefire-plugin:2.7.1:test (default-test) @ MTest ---
[INFO] Surefire report directory: C:\Users\sai\OfficeWSApril14\MTest\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
There are no tests to run.

Results :

Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

[INFO] 
[INFO] --- maven-war-plugin:2.1.1:war (default-war) @ MTest ---
[INFO] Packaging webapp
[INFO] Assembling webapp [MTest] in [C:\Users\sai\OfficeWSApril14\MTest\target\MTest]
[INFO] Processing war project
[INFO] Copying webapp resources [C:\Users\sai\OfficeWSApril14\MTest\src\main\webapp]
[INFO] Webapp assembled in [107 msecs]
[INFO] Building war: C:\Users\sai\OfficeWSApril14\MTest\target\MTest.war
[WARNING] Warning: selected war files include a WEB-INF/web.xml which will be ignored 
(webxml attribute is missing from war task, or ignoreWebxml attribute is specified as 'true')
[INFO] 
[INFO] --- maven-install-plugin:2.3.1:install (default-install) @ MTest ---
[INFO] Installing C:\Users\sai\OfficeWSApril14\MTest\target\MTest.war to C:\Users\sai\.m2\repository\com\MTest\0.0.1-SNAPSHOT\MTest-0.0.1-SNAPSHOT.war
[INFO] Installing C:\Users\sai\OfficeWSApril14\MTest\pom.xml to C:\Users\sai\.m2\repository\com\MTest\0.0.1-SNAPSHOT\MTest-0.0.1-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 7.536s
[INFO] Finished at: Mon Apr 29 10:10:06 IST 2013
[INFO] Final Memory: 5M/15M
[INFO] ---------------------

When i right click on Eclipse IDE Project create new Servlet , how can i
ensure that the source file is placed under the src/main/java ??

I have attached a screen shot ,please see

enter image description here

Upvotes: 1

Views: 1273

Answers (1)

Charlee Chitsuk
Charlee Chitsuk

Reputation: 9069

Since the Maven shows us as the following: -

[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ MTest ---
[INFO] No sources to compile

This mean there is no any known java class. Please review your code and ensure that the source file is placed under the src/main/java

Edit

There are 2 possible ways to achieve your question about how can i move the servelet under src/main/java ?

1 Convert the existing Maven Java Project to be a Dynamic Web Project as the following steps: -

  1. Right click at your project.

  2. Select the Properties from the context menu.

  3. At the Properties Windows select Project Facets.

  4. At the right hand panel please select the following: -

    1. Dynamic Web Module: 3.0
    2. Java: 1.6
    3. Java Script: 1.0
  5. Click OK button

2 Create new Maven Web Project by using the following step: -

  1. Go to menu File ---> New ---> Other.
  2. At the Select Wizard Windows, select Maven ---> Project.
  3. Click next and enter the required information so that we are at the New Maven Project.
  4. At the Filter textbox, enter webapp-javaee6.
  5. Select the org.codehaus.mojo.archetypes:webapp-javaee6:1.5.
  6. Click next and enter the required information so that the project creation is finished.

Please note, I would prefer the second way instead, since there will be the additional configuration at our pom.xml which is very useful for further development. You can also refer to my previous answer about the full story at How do I configure a Java EE maven project in Eclipse?

I hope this may help.

Upvotes: 1

Related Questions