Converting a Java project to a Dynamic Web project in Eclipse
Once a project is created in Eclipse Europa of a certain type, it's not the most straight forward process of changing the project to a different type. For example you have a project that Eclipse considers to be a Java project, but it's actually a Web Project, you don't get to take advantage of all of the features available for Web Projects, such as deploying to a local tomcat server, unless Eclipse is aware that it's a web project. This has to do with the project settings and what eclipse considers the "Nature" of the project.
For example to convert a Java Project to a Web Project switch to or open the Resource Perspective of the project, in the root of the project. Open the .project file and make sure the builders and natures are present that are needed for a web project. See the example below, the name should be the name of your project, the most important nodes are the nature children in the natures node:
testProject org.eclipse.jdt.core.javabuilder org.eclipse.wst.common.project.facet.core.builder org.eclipse.wst.validation.validationbuilder org.eclipse.wst.common.project.facet.core.nature org.eclipse.jdt.core.javanature org.eclipse.wst.common.modulecore.ModuleCoreNature org.eclipse.jem.workbench.JavaEMFNature
Once you've updated the .project file you can close the file and right click and choose properties on the project. When the properties window opens click on Project Facets. The Facets grid is probably empty, click the Modify Project button. Check the Dynamic Web Module and Java Facets, choose the Java and Servlet version that applies to your project. Click Next and specify the existing or new location of your src and web content directories. Click Finish.
As a final step I would recommend modifying the build path to compile your source directly into your /WEB-INF/classes directory by selecting Java Build Path and modifying the Default output directory.
Now you should be able to create a local tomcat server, or if you've already created one you should be able to add the project to the server by right clicking the server and choosing Add and Remove Projects.
July 15th, 2008 - 17:52
Any thoughts on why the jars in the project’s build path aren’t being copied into the lib directory?
July 15th, 2008 - 21:10
Are you in the Java perspective? Try switching to the Resource perspective and see if you see the jars in the lib directory.
October 10th, 2008 - 13:59
I am using Eclipse Ganymede and there is no Modify Project button on Project Facets. The Eclipse Help says: “You can add facets only to Java EE projects and othe types of projects that are based on J2EE projects, such as enterprise application projects, dynamic Web projects, and EJB projects. You cannot add facets to a Java project or plug-in project for example”.
Is there an alternative (or am I doing something wrong)?
October 10th, 2008 - 15:16
Lincoln,
Basically when I’m trying to add/modify a facet because of a mistake when creating the project or I want to change the direction of the project, I typically do it at the file level and then restart Eclipse to see the change.
To this I’ll create an empty project of the type I want, look at the file system to see what files were created and where the Facets I need are specified and then manually copy and past them into the appropriate file in the existing project. Eclipse will recognize the project when it loads and treat it accordingly.
December 5th, 2008 - 20:05
A faceted project allows the addition of natures to a project. A simple faceted project .project config file looks like this:
faceted-project
org.eclipse.wst.common.project.facet.core.builder
org.eclipse.wst.common.project.facet.core.nature
I found that copying the buildcommand and nature tags above into an existing project’s .project file and refreshing allows me to bring up the project’s properties dialog and then add the desired Java and Web Project natures.
December 29th, 2008 - 10:23
That really help me out here!
Thanks!
March 11th, 2009 - 09:21
Hi wg,
This seems to work as far as it goes. I’m able to add a modified project to an embedded Server. Unfortunately, when I attempt to run the application, a class that is clearly found in in project/WebContent/WEB-INF/classes/com/… can’t be located by the server’s class loader.
In this case, the code in the project implements a web service interface. When I use the Web Services Explorer to invoke the service, the fault returned is as follows:
java.lang.NoClassDefFoundError: com/matrixone/cbp/ws/CBPServiceat java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
at org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:1847)
at org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:873)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1326)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1205)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
etc.
Note: I have two projects in play here. Both are added to the Server and one lists the other as a dependency. The latter has the class that is not found by the class loader.
Any suggestions appreciated.
May 6th, 2009 - 03:35
Worked like a charm. Verry good article. It solved my problem.
August 1st, 2009 - 03:03
nice one…
August 11th, 2009 - 21:39
I noticed in my installation (Eclipse 3.4) that the instructions above didn’t work.
I tracked down the problem, and in the file:
/.settings/org.eclipse.wst.common.project.facet.core.xml
I inserted the line shown below by ‘+’
. . .
+
. . .
That seemed to do it for me.
Cheers,
– Luke
October 28th, 2009 - 10:06
This saved my day from being a total waste
January 20th, 2010 - 13:23
Greate tip!!
April 2nd, 2010 - 06:25
hi ,
i installed eclipse3.5.2…but getting the problem to create dynamic web project ?….so any one tell me and also tell its procedure to work…….
regards
param
April 30th, 2010 - 11:18
Also see http://wiki.eclipse.org/IRC_FAQ#How_do_I_manually_assign_a_project_Nature_or_BuildCommand.3F for a general hint on adding natures to projects.
February 22nd, 2011 - 03:57
Thank your for the tips. Its very useful one.
May 21st, 2011 - 05:03
Hi can you tell me how do i convert java project into dynamic web project in WSAD(Websphere Studio Application developer) as there is no Project Facets link in the properties of the project. WSAD is based on Eclipse.
Thanks in Advance
September 22nd, 2011 - 09:28
I did all those stuffs but I still do not have my projects added to Tomcat, although I restarted Eclipse afterwards but I still get the same message popup when I try to add projects to the server:
“There are no resources that can be added or removed from the server”
By the way, my Eclipse is Helios 3.6.1 and Tomcat v6.0.
BR /// Hamin
October 5th, 2011 - 13:30
This was with a much older version of Eclipse, I’m not sure the natures are even the same. The best thing to do is to create a Java Project and then create a Dynamic Web Project, compare the meta files and you’ll see the differences in how the project meta is defined and then try modifying the Java Project to mirror the Web Project and see if you can get eclipse to pick it up.
November 21st, 2011 - 04:59
Thank you for the article! Just what I was looking for.
I was afraid I was going to have to check out the whole thing again or go rooting around in some config… I’m glad it was as comparatively simple as checking the right facets, once I read what you had to do.
December 13th, 2011 - 05:35
Thanks for sharing
February 20th, 2012 - 08:22
Very nice article , thank you very much, Im happy to see that you also had the same problem and resolved it
March 26th, 2012 - 14:12
At least with Indigo (3.7), I have found it much more convenient (and quicker) to simply open the .settings folder in the Package Explorer, and delete all the files in that folder. If you are nervous about taking such a drastic action, make copies of those files in some other folder before deleting.
Then open the project’s properties; click on the Project Facets node. All facets will be available for selection. You’ll have to do additional configuration for some facets, but that’s just par for the course.
(Editing the .classpath and .project files is much too fussy and error prone. I used to do it before discovering the above trick, but was never satisfied with that approach. Of course, the “right” way would be to have the eclipse crew formalize such a conversion in their menus, etc., as they do for JPA, for example.)
May 7th, 2012 - 10:45
This information has really helped me when i had issue with web project in RAD. I read this article and resolved the issues. Many thanks for such great input.
appreciate it