0

Just setup an eclipse project using Maven webapp architype. I created a servlet file and that got added to the resources folder under src/main/ When I run the app tomcat gives an error:

Error instantiating servlet class com.ABCompany.Demo.SampleDemo. java.lang.ClassNotFoundException: com.ABCompany.Demo.SampleDemo

The file is under the folder src/main/resources/com/ABCompany/Demo/SampleDemo.java

My web.xml is below.

<web-app>
 <display-name>Demo Example</display-name>
 <servlet>
 <servlet-name>SampleDemo</servlet-name>
 <display-name>SampleDemo</display-name>
 <description></description>
 <servlet-class>com.ABCompany.Demo.SampleDemo</servlet-class>
 </servlet>
 <servlet-mapping>
 <servlet-name>SampleDemo</servlet-name>
 <url-pattern>/SampleDemo</url-pattern>
 </servlet-mapping>
 <welcome-file-list>
 <welcome-file>index.jsp</welcome-file>     
 </welcome-file-list>    
</web-app>
user275157
  • 1,332
  • 4
  • 23
  • 45
  • 1
    have u built project successfully? Do you see target folder? – VedantK Oct 28 '15 at 09:38
  • New to maven/eclipse/Java. How do I build this eclipse maven project. I just keep hitting run on server. – user275157 Oct 28 '15 at 10:42
  • 1
    http://stackoverflow.com/a/28734984/1878022 First follow this answer, then rightclick runas->maven build->Goal=clean install – VedantK Oct 28 '15 at 10:46
  • Did that and now it gives me the target folder. How do I run this so that it shows me the web pages on tomcat. I click on Run -> Run on server select tomcat and it gives me the error. Should I run it another way. Apologies for noob questions. I am a .net programmer new to java – user275157 Oct 28 '15 at 10:50
  • Same class not found error – user275157 Oct 28 '15 at 10:53
  • http://www.mkyong.com/maven/how-to-create-a-java-project-with-maven/ I would recommend try this example and you will resolve your question yourself – VedantK Oct 28 '15 at 10:57

2 Answers2

2

Reading in the question:

The file is under the folder src/main/resources/com/ABCompany/Demo/SampleDemo.java

Java source files, should go under src/main/java

src/main/java/com/ABCompany/Demo/SampleDemo.java

Other files, like properties, HTML/CSS files, images, templates, etc. Are normally placed in the src/main/resources. As they do not need to be compiled, just copied over into the build directory. So they can be used for running the application, or for packaging (creating a jar).

TIP: Package names, should not contains capitals, by confention. So prevered is com.abcompany.demo.

TIP: Also read How to create a Web Application Project with Maven which uses a Maven archetype, for setting it all up!

Verhagen
  • 3,885
  • 26
  • 36
  • This is the default structure that eclipse/maven gives on a webapp archetype - putting the files under resources folder when I create a servlet. – user275157 Oct 28 '15 at 10:40
  • Not sure where you read that, or what did create the initial set up for you. See my updated answer. Also read some other stuff of setting up Maven projects for web development. – Verhagen Oct 28 '15 at 18:37
1

I had the same problem.

My solution is: When the project is created please create the java folder under src/main manually, it is because the maven-archetype-webapp doesn't create the src/main/java by default.

Before: src/main
After:  src/main/java

image:java folder manually created

When the first step is ready then you can create all the servlets that you need, please make sure that you create all your classes and servlets under src/main/java.

The problem is because the servlet was created under src/main/resources that means that all your .java files was packaged as a resource and not as .class compiled.

Daniel C.
  • 5,418
  • 3
  • 23
  • 26