Phyo Wai
Phyo Wai

Reputation: 111

cvc-elt.1: Cannot find the declaration of element 'web-app'

When I created new dynamic web project in Eclipse, automatically created web.xml showed the following error:

cvc-elt.1: Cannot find the declaration of element 'web-app'

and a red background on this line:

<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

I want to know why this error occur and how to get rid of this error. I used Eclipse version 3.1, Apache Tomcat 5.0 and JDK 1.4 in my project. I'm pretty new to Eclipse.

Upvotes: 11

Views: 54935

Answers (9)

You can find this problem in the XML catalog of the Eclipse IDE I solved it by adding the web-common_3_0.xsd file.

First you need to download this file from the link below:

http://java.sun.com/xml/ns/javaee/web-common_3_0.xsd

Then you will need to follow Eclipse-->Window-->Preferences-->XML (Wild Web Developer) --> Catalogs and add the file you downloaded from the link above. You can also drag and drop the file into the Catalog tab.

Upvotes: 0

Piyush Pranjal
Piyush Pranjal

Reputation: 584

Error: cvc-elt.1.a: Cannot find the declaration of element 'web-app'

Issue:

This error is related to the web.xml file of Eclipse IDE Dynamic Web Project
The main root cause of this error is different Java Enterprises Edition (JEE) support.
From August 13, 2019 Java EE Servlet API is maintained by Jakarta EE Servlet API
As these package names are changed from:

javax.servlet.*; ---> to ---> jakarta.servlet.*;
javax.servlet.http.*; ---> to ---> jakarta.servlet.http.*;

Your IDE might still populating the old doc type declaration for xml files to be compatible with javax package

Solution:

If you're using new version of Tomcat or other Java Server Application, check whether they're working with new jakarta package or old javax package.
Tomcat 10 is already migrated to Jakarta EE so if you're using Tomcat 10 you can use the jakarta doctype declaration mentioned below.

Correct Declaration for Old Version javax package

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0">

Correct Declaration for New Version jakarta package

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="https://jakarta.ee/xml/ns/jakartaee" xmlns:web="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd http://xmlns.jcp.org/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="5.0">

Things I am working with:

Eclipse IDE for Enterprise Java and Web Developers
Version: 2021-12 (4.22.0)
Build id: 20211202-1639*

Apache Tomcat
Version 10.0.7

Upvotes: 11

Mehdi Rahimi
Mehdi Rahimi

Reputation: 2566

This issue is related to Eclipse auto-generated web.xml file.

In new versions of Java EE, you must change Java EE to Jakarta EE.

As a sample change your <web-app> opening tag as this [5/7/2022]:

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="https://jakarta.ee/xml/ns/jakartaee"
    xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee 
    https://jakarta.ee/xml/ns/jakartaee/web-app_6_0.xsd"
    version="6.0">

Upvotes: 0

Amresh Singh
Amresh Singh

Reputation: 43

For me its working now Replace with this - <web-app 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-app_3_0.xsd">

Upvotes: 4

Roshan
Roshan

Reputation: 21

Deleting the workspace and creating a new workspace has helped me to resolve this error. I was getting this error for the ZK web application

Upvotes: 0

user4973642
user4973642

Reputation: 1

I received this error when I unknowingly lost the internet connection meaning the xsd was not available. Reconnecting solved it

Upvotes: 0

Adarsh Khalique
Adarsh Khalique

Reputation: 66

I also came across this problem and didn't found something reasonable. For quick solution. Delete your workspace entirely and take clone of project again. You won't see the problem again. It helped me.

Upvotes: 1

sanjay
sanjay

Reputation: 69

You need to add the DOCTYPE element to the start of the XML:

<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">

Upvotes: 6

Garret Wilson
Garret Wilson

Reputation: 21466

I'm seeing this on Eclipse 4.1, Apache Tomcat 7.x, and JDK 1.6. It apparently has something to do with caching the XSD files. Some people provide an alternate syntax of web.xml that uses DTDs instead of schemas. But I've had success with one solution which suggested merely turning off caching. The better option, also at that last link, is to download and install the XSDs manually.

Update: I decided to try to download one of the XSDs myself and try to install it in the Eclipse XML catalog manually. It became obvious that the Java XSD server was having problems---which is apparently what Eclipse saw when it tried to download and cache the XSD. Perhaps when Oracle/Sun gets its act together and the server starts working again, then Eclipse will stop giving this error. In the meantime, I'll have to turn off caching and see how far I get.

Update: Once the Sun server comes back up for a little while, download the XSD file and store it in some semi-permanent place locally. Then go into the Eclipse preferences, search for the XML Catalog, and give it a reference to the XSD file. I've verified this prevents Eclipse from trying to download and cache the file. This is the best solution if you can't trust the Sun servers---and apparently you can't.

Upvotes: 4

Related Questions