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.
May 5th, 2008 at 2:16 pm
Converting a Java project to a Dynamic Web project in Eclipse…
[...]Converting a java project to a dynamic web project in Eclipse, so that you can deploy your web application to a local server.[...]…
May 26th, 2008 at 8:08 am
[...] Artikel [...]
Jul 15th, 2008 at 5:52 pm
Any thoughts on why the jars in the project’s build path aren’t being copied into the lib directory?
Jul 15th, 2008 at 9:10 pm
Are you in the Java perspective? Try switching to the Resource perspective and see if you see the jars in the lib directory.
Oct 10th, 2008 at 1:59 pm
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)?
Oct 10th, 2008 at 3:16 pm
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.
Dec 5th, 2008 at 8:05 pm
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.
Dec 29th, 2008 at 10:23 am
That really help me out here!
Thanks!
Feb 4th, 2009 at 12:08 pm
[...] http://greatwebguy.com/programming/eclipse/converting-a-java-project-to-a-dynamic-web-project-in-ecl... [...]
Mar 11th, 2009 at 9:21 am
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 at 3:35 am
Worked like a charm. Verry good article. It solved my problem.