GreatWebGuy Self-proclaimed greatness is a hard thing to prove.

2May/0823

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.

Comments (23) Trackbacks (4)
  1. Any thoughts on why the jars in the project’s build path aren’t being copied into the lib directory?

  2. Are you in the Java perspective? Try switching to the Resource perspective and see if you see the jars in the lib directory.

  3. 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)?

  4. 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.

  5. 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.

  6. That really help me out here!

    Thanks!

  7. 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/CBPService
    at 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.

  8. Worked like a charm. Verry good article. It solved my problem.

  9. 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

  10. This saved my day from being a total waste

  11. 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

  12. Thank your for the tips. Its very useful one.

  13. 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

  14. 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

    • 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.

  15. 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.

  16. Thanks for sharing

  17. Very nice article , thank you very much, Im happy to see that you also had the same problem and resolved it :)

  18. 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.)

  19. 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


Leave a comment