8

I'm new to Spring and started using the Sprint Tool Suite (eclipse) and see this error in a java file in my maven project: "Content is not allowed in prolog". This prevents the class from compiling (which is just a minimal SpringBootApplication class) in eclipse.

package com.test;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

I've scoured the web and saw that in virtually every reference, this is an XML-parsing error. I had been updating three XML files but didn't see any invisible characters before the <?xml in a hex editor. They all specify to use UTF-8 encoding and I'm not seeing alternating 0 bytes that would indicate an accidental UTF-16 encoding. The line endings seem consistent (just a line feed).

Interestingly, it builds without compiler errors with "maven clean install". I don't see any special characters in the java file either, but eclipse tells me that file is the problem and that's it a spring beans problem. The error message shows on the first line with the package statement.

Any ideas what the root problem is or how I can solve this?

Update As requested, here's my pom.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.springframework</groupId>
    <artifactId>gs-rest-service</artifactId>
    <version>0.1.0</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.2.5.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.2.8</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
    </dependencies>

    <properties>
        <java.version>1.8</java.version>
    </properties>


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
       <resources>
         <resource>
           <targetPath>com/test/mybatis/mappers</targetPath>
           <directory>src/main/java/com/test/mybatis/mappers</directory>
           <includes>
             <include>*.xml</include>
           </includes>
         </resource>
         <resource>
           <directory>src/main/resources</directory>
         </resource>
       </resources>
    </build>

    <repositories>
        <repository>
            <id>spring-releases</id>
            <url>https://repo.spring.io/libs-release</url>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-releases</id>
            <url>https://repo.spring.io/libs-release</url>
        </pluginRepository>
    </pluginRepositories>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.36</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

Update 2 I searched my project folder for all .xml files and have removed all invisible characters programmatically using perl -i -pe 's/[^\x20-\x7f\x0A\x09]//g' fileName, but there were no invisible characters. This confirms what I saw in the hex editor.

Two questions come to mind: Why is the error showing in a .java file instead of a. xml file? Why is this only a problem for eclipse and not maven?

The eclipse log shows this message (which indicates a .xml file):

org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog.
    at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
    at org.springframework.ide.eclipse.core.io.xml.XercesDocumentLoader.loadDocument(XercesDocumentLoader.java:48)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadDocument(XmlBeanDefinitionReader.java:429)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:391)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:336)
    at org.springframework.ide.eclipse.beans.core.internal.model.BeansConfig$2.loadBeanDefinitions(BeansConfig.java:397)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:304)
    at org.springframework.ide.eclipse.beans.core.internal.model.BeansConfig$3.call(BeansConfig.java:456)
    at org.springframework.ide.eclipse.beans.core.internal.model.BeansConfig$3.call(BeansConfig.java:1)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

Update 3 I did find some eclipse-generated xml files in my workspace with characters before the <?xml using grep -ERl '^[^=]+<\?xml' . which ignores some of the eclipse user prefs which have xml embedded in the lines of the file. After removing the extra characters, the problem remained though.

Think I may restart the project and copy over files one at a time.

rimsky
  • 1,163
  • 3
  • 16
  • 27
  • 2
    Kinda far-fetched, but maybe it's related to line-breaks from Windows to Unix? This can happen if someone saved the file on Windows and then opened it on unix. You could fix it by running dos2unix. – Fabio Kenji Sep 24 '15 at 23:14
  • Did you check out [Eclipse Content is not allowed in prolog](http://stackoverflow.com/questions/5445143/eclipse-content-is-not-allowed-in-prolog)? – Mick Mnemonic Sep 24 '15 at 23:25
  • @Elliott Frisch I posted my pom.xml – rimsky Sep 24 '15 at 23:59
  • @Mick Mnemonic Yes, the .springBeans file looked promising since that trick was for a java file and the .springBeans listed the problem java file in the config section (and nothing else there) instead of in the autoconfig section. Yet, when I moved that one config line to the autoconfig section, still the same results. Also same results after deleting the .springBeans file. – rimsky Sep 25 '15 at 00:02
  • @Fabio Kenji Line endings are consistent in my pom.xml – rimsky Sep 25 '15 at 00:20
  • 2
    Perhaps you have a [byte-order-mark](https://en.wikipedia.org/wiki/Byte_order_mark) in the beginning of pom.xml (or some other XML file). The BOM might only show in a hex editor. – Mick Mnemonic Sep 25 '15 at 00:37
  • @Mick Mnemonic Unfortunately, the hex editor didn't show any bytes before the first < in any xml file. – rimsky Sep 25 '15 at 00:55

6 Answers6

7

It seems likely that the culprit was a corrupted eclipse workspace. I created a new workspace and imported the project and everything works fine. That would also explain why an external maven build didn't show any problems.

As noted in update 3, I had found some eclipse-generated xml files that had characters before the prolog. Although correcting those did not fix the problems, I didn't inspect every eclipse file with xml content (like the user prefs) to see if they had some invisible characters before the prolog.

Don't know what would have caused it to be corrupted except for perhaps force quitting eclipse during an unresponsive maven repo update but I can't be certain if that's when my troubles started. I'm just glad I can move forward.

rimsky
  • 1,163
  • 3
  • 16
  • 27
4

In my case this error occured while I was renamed main class (class annotated with @SpringBootApplication) of springboot application. I don't mean this will always happen but I accidently got this error after rename this class file in STS 3.8.4.

To resolve this issue, I closed the IDE, navigate to project folder and delete the .springBeans file. After reopened IDE, this error was gone and my IDE no longer complains about this.

Cataclysm
  • 7,592
  • 21
  • 74
  • 123
  • 2
    +1 This is what helped me out. Deleting the spring beans file, cleaning the project, closing it and re opening it. – Calebj Nov 26 '17 at 09:02
  • 1
    @cataclysm Thanks for the solution. I had the same scenario and after following above steps everything worked fine for me. – Beast Oct 13 '19 at 14:38
  • 1
    Thanks for solution. I had the same stupid problem. Seems have been caused by renaming the Application.java class. Deleting .springbeans was not enough; had to delete the Application.java file and make a new one. Spring Tool Suite 3, Version: 3.9.10.RELEASE – J Slick Mar 21 '20 at 00:33
3

Most likely the error was generated by the UTF-8 BOM at the start of the XML file - applications like Notepad puts that at the start of the file and the Java streams cannot handle that automatically. Your XMLis correct, but the file is not UTF-8Encoded. I've seen this before.. try copying and pasting the XML from this forum into a text editor and saving it. Also make sure that you're text editor is really saving in UTF-8.

Also, Springwill work just fine without any <?xml ... > declaration so go ahead and remove the comment between the DTDdeclaration and the starting element. Remove the UTF-8declaration. Problems with UTF-8encoded XMLin Java are pretty common, and not specific to Spring.

TheLuminor
  • 1,411
  • 3
  • 19
  • 34
1

For me it was because I had renamed some classes and the .springBeans file needed some manual editing.

0

Just put @Primary after @SpringBootApplication, delete @Primary and save again

0

For me the issue was with the charset encoding. Using echo command redirection to create the bean resource xml file set it to an encoding other than the expected UTF-8 on IntelliJ. This applies to Java classes too. I fixed it by deleting the xml file, creating a new one using the GUI/Vim this time and with the xml file selected, going to Build->Recompile <file_name>.xml.

douglas
  • 116
  • 3