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>
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!
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.
Related
I have the following setup:
Project A - A Dynamic Web project, which depends on project B.
Project B - A Dynamic Web project, that defines a Test.jsp file.
If I launch project B on the server, or move the Test.jsp to project A and launch project A on the server. it works just fine, and I can access the .../Test page'.
But when the Test.jsp remains in project B and I launch project A, although I do see the project-b.jar in the war file and the classes from project B does load, which means most of the process works ok, and only the jsps are not added...
How can I solve this?
In Case anyone would get to this question, the solution described in here works well, though was not very clear at first:
In my project B, place your jsp files so:
src/main/webapp/META-INF/resources/${path-to-jsp}/file.jsp
In src/main/webapp/META-INF/ create a web-fragment.xml with your variation of the following content:
<web-fragment
metadata-complete="true"
version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-fragment_3_0.xsd">
<servlet>
<servlet-name>jspTest</servlet-name>
<jsp-file>/${path-to-jsp}/file.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>jspTest</servlet-name>
<url-pattern>/test</url-pattern>
</servlet-mapping>
</web-fragment>
and now if your buildpath is defined correctly, then clean-build both project B, and project A and run the server again... it works!
I am trying to create a base Spring project with Maven on Eclipse Kepler (with m2eclipse installed). No errors are reported by Eclipse but when I hit Run on Server (or run Maven Install) only the original files from META-INF and WEB-INF files are deployed. This does not include my dispatcher-servlet.xml config file thus causing a FileNotFoundException:
java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/dispatcher-servlet.xml]
How can I fix this?
This is my web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/dispatcher-servlet.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
And this is a screencap of my project's folder:
As you can see only selected files are shown in Deployed resources.
Since it is a maven project .
you might have converted maven project to dynamic web project I guess.
Follow the steps
Go to project properties (right click at last click peoperties)
click/select Deployment assembly at the left side
the root(/) deploy path may be pointing to src/main/webapp , select this and click remove button
click on add button , select a directive type folder, select your WebContent folder
Now try to clean and run your application on server.
Solution 2
Actually you need to copy/put all your WebContent files and folder to src/main/webapp
and restart your server.
Note that the maven dynamic web project it always look in to src/main/webapp by default.
Hope it helps
Add <sourceDirectory>src</sourceDirectory> in the <build> block.
Then Run Maven with "clean install" goal.
And refresh your project.
Now check your target folder. You should see war file or * extension file.
Hope it will solve
I am working on a GWT web application split across two Eclipse Projects (myclient & myservice).
The myclient project references the myservice project via a dependency in the POM.
<dependency>
<groupId>com.myproject</groupId>
<artifactId>myservices</artifactId>
<version>1.0.0</version>
</dependency>
The myclient project has a WAR directory src/main/webapp. The output folder for the myclient project is src/main/webapp/WEB-INF/classes.
The myclient project has a Spring descriptor application-context.xml with the following
<context:component-scan base-package="com.myproject.myclient, com.myproject.myservices"/>
and the web.xml
<web-app>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/application-context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
...
</web-app>
I have several files in the myservices project annotated as spring #Component, #Service, #Configuration but these are not picked up by the component scan when I run the GWT application in Eclipse. As a test I experimented with placing an #Component in the myclient project and this was successfully created.
I believe the following log entry during application startup indicates the source of the problem
org.springframework.core.io.support.PathMatchingResourcePatternResolver - Resolved location pattern [classpath*:com/myproject/myservices/**/*.class] to resources []
the location pattern for the myclient project resolves to all the resources on the classpath but for myservices no resources are found.
I experimented with building the myservices project JAR and placing this JAR into the src/main/webapp/WEB-INF/lib folder of the myclient project. When I do this the component scanning works. However for development I don't want to have to build and copy a JAR everytime I make changes to the myservices project. I imagine that the component scanning should work on a project referenced through the POM without having to the build that project but after much experimenting I have been unable to get this working.
Be sure that in the deployment assembly (right click your web project and select "deployment assembly" of your myclient project it is configured to deploy the jar that is outputted by the myservices project. If you are using maven, the m2e, m2e-wtp project configurators should do this deployment assembly setup automatically.
Once you have deployment assembly settings properly configured, now when you deploy a project to your server using the Eclispe server adapter publish mechanism, everything should get deployed and the myservices jar would get placed in the right spot for your myclient project.
But make sure you the latest version of m2e-wtp installed. This way your configuration in your pom.xml and deployment assembly will get correctly configured.
Try splitting your application-context.xml into 2 separate files:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:service-context.xml,classpath:client-context.xml</param-value>
</context-param>
myservices/src/main/resources/service-context.xml:
<context:component-scan base-package="com.myproject.myservices"/>
myclient/src/main/resources/client-context.xml:
<context:component-scan base-package="com.myproject.myclient"/>
Try this :
<context:component-scan base-package="com.myproject"/>
Please check the following things:
Is your serviceproject present "only" as jar or is the whole project available via workspace resolution. This can be checked via maven-context-menue (Disable/Enable Workspaceresolution)
The layout of the webapp under src/main/webapp is okay. BUT do i get you right that all classes are copied there? If so, you should make sure everything is under target directory. So please check whether a maven call "clean package" generates a webappstructure under the target folder and all required libs (e.g. myservice) exist under target\$your-webarchivename\WEB-INF\lib
Check that packaging in myservice pom.xml is set to jar (you probably have this, right?)
it's obvious that your service jar is not included in your client project's build path. this is the only root cause.
Make sure 3 points:
you have run mvn clean install under your service project which has correct pom.xml.
you have run mvn eclipse:eclipse under your client project. this will pull out all your dependency project.
check your client eclipse project's build path dialog. is there your service jar in the list? Make sure this
You better once look this tutorial
http://fusesource.com/docs/framework/2.2/deploy_guide/CXFServletDeploySpring.html
I have set up my maven project using the m2e plugin in eclipse indigo, and transformed it to an eclipse dynamic web project using mvn eclipse:eclipse -Dwtpversion=1.5. I have managed to get the project up and running in tomcat7, except for my servlets, for which I cannot create the servlet mappings.
I have tried modifying the web.xml file but it throws a ClassNotFoundException. Directory Structure and web.xml :
(ROOT)
+src
+main
+resources
+DrawInitialMap.java
+webapp
(WebContent here)
<web-app>
<servlet>
<servlet-name>DrawInitialMap</servlet-name>
<servlet-class>(groupId).(artifactId).src.main.resources.DrawInitialMap</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DrawInitialMap</servlet-name>
<url-pattern>/drawInitialMap.do</url-pattern>
</servlet-mapping>
(...)
</web-app>
While the #WebServlet annotation also fails to map the servlet :
#WebServlet(name="drawInitialMap", description="visualizes ttrp on html5 canvas", urlPatterns={"/drawInitialMap.do"})
Thank you in advance, and notify if you need any more of the code.
PS : Keep in mind that the servlet worked perfectly in Dynamic Web Project mode, without Maven
There are several issues.
You should stop using eclipse:eclipse. Instead, install WTP integration for M2E from Eclipse Marketplace
In Maven project, your DrawInitialMap should be in /src/main/classes folder. So, it will be compiled as per default Maven project conventions
The servlet-class element in web.xml requires full class name, i.e. no things like (groupId).(artifactId).src.main.resources.
I have a common GWT application that is using (trying to) Rocket-Framework to implement a CometServerServlet.
After I have registered my servlet in web.xml and my app.gwt.xml, like this:
App.gwt.xml:
<servlet path="/server" class="myapp.server.MyCometServlet"/>
web.xml
<servlet>
<servlet-name>myCometServlet</servlet-name>
<servlet-class>myapp.server.MyCometServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>myCometServlet</servlet-name>
<url-pattern>/myapp/server</url-pattern>
</servlet-mapping>
Jetty apparently doesn't find the CometServerServlet from which my MyCometServlet extends:
WARNING: Error starting handlers
java.lang.NoClassDefFoundError:
rocket/remoting/server/comet/CometServerServlet
Caused by:
java.lang.ClassNotFoundException:
rocket.remoting.server.comet.CometServerServlet
That is strange, because I've added the Rocket jar to my project.
Could someone explain why this error happened?
I could understand if I "registered" the servlet in a wrong way or something, but at what point Jetty (or who?) looks for the CometServerServlet and fails at finding it?
Adding the jar to your project (e.g. in some IDE) will remove compilation errors, because the jar can be found at compile time.
But Jetty needs the library at runtime, and the usual way to provide it, is by putting it in the WEB-INF/lib folder of your project.
Of course, make sure to redeploy the app afterwards (e.g. if you're using Dev Mode, click refresh in the "Development Mode" view).