Java文件:prolog中不允许内容 [英] Java file: Content not allowed in prolog

查看:1114
本文介绍了Java文件:prolog中不允许内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Spring的新手,并开始使用Sprint Tool Suite(eclipse),并在我的maven项目的java文件中看到这个错误:prolog中不允许内容。这样可以防止eclipse中的类编译(这只是一个最小的SpringBootApplication类)。

  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 );
}
}

我已经扫过网路,每个引用,这是一个XML解析错误。我已经更新了三个XML文件,但在十六进制编辑器中的<?xml 之前没有看到任何不可见的字符。他们都指定使用UTF-8编码,我没有看到交替的0字节,表示意外的UTF-16编码。有趣的是,它使用maven clean install创建没有编译器错误的线程结尾似乎是一致的(只是一个换行)。



我也没有在java文件中看到任何特殊字符,但eclipse告诉我该文件是问题,那就是一个spring bean问题。



任何想法根本问题是什么或我如何解决这个问题?



更新
根据要求,这是我的pom.xml文件:

 <?xml version =1.0encoding =UTF-8?> 
< project xmlns =http://maven.apache.org/POM/4.0.0xmlns: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>

<依赖关系>
<依赖关系>
< groupId> org.springframework.boot< / groupId>
< artifactId> spring-boot-starter-web< / artifactId>
< / dependency>
<依赖关系>
< groupId> org.mybatis< / groupId>
< artifactId> mybatis< / artifactId>
< version> 3.2.8< / version>
< / 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>
<依赖关系>
<依赖关系>
< groupId> mysql< / groupId>
< artifactId> mysql-connector-java< / artifactId>
< version> 5.1.36< / version>
< / dependency>
< / dependencies>
< / dependencyManagement>
< / project>

更新2
我搜索了所有的项目文件夹。使用 perl -i -pe's / [^ \x20-\x7f\x0A\x09] // g'fileName ,但没有看不见的人物。这确认了我在十六进制编辑器中看到的内容。



有两个问题出在:为什么错误显示在.java文件中而不是。 xml文件?为什么这只是eclipse而不是maven的问题?



eclipse日志显示此消息(表示一个.xml文件):

  org.xml.sax.SAXParseException; lineNumber:1; columnNumber:1;序言中不能有内容。 
在org.apache.xerces.parsers.DOMParser.parse(未知源)
在org.springframework.ide.eclipse.core.io.xml.XercesDocumentLoader.loadDocument(XercesDocumentLoader.java:48)
在org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadDocument(XmlBeanDefinitionReader.java:429)
在org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:391)
在org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:336)
在org.springframework.ide.eclipse.beans.core.internal.model.BeansConfig $ 2.loadBeanDefinitions( BeansConfig.java:397)
在org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:304)
在org.springframework.ide.eclipse.beans.core.internal。 model.BeansConfig $ 3.call(BeansConfig.java:456)
在org.springframework.ide.eclipse.beans.core.internal。 model.BeansConfig $ 3.call(BeansConfig.java:1)
在java.util.concurrent.FutureTask.run(FutureTask.java:266)
在java.util.concurrent.Executors $ RunnableAdapter.call (Executors.java:511)
在java.util.concurrent.FutureTask.run(FutureTask.java:266)
在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
在java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:617)
在java.lang.Thread.run(Thread.java:745)

更新3
我在工作区中找到一些eclipse生成的xml文件, code><?xml
使用 grep -ERl'^ [^ =] +< \?xml'。一些eclipse用户prefs将xml嵌入到文件的行中。删除额外的字符后,问题仍然存在。



想想我可以重新启动项目并一次复制一个文件。

解决方案

似乎可能是罪魁祸首是一个损坏的eclipse工作区。我创建了一个新的工作区并导入项目,一切都正常。这也将解释为什么一个外部的maven构建没有显示任何问题。



如更新3所示,我发现一些eclipse生成的xml文件,序幕虽然纠正这些问题没有解决问题,但我没有检查每个eclipse文件的xml内容(如用户prefs),看看他们是否在prolog之前有一些不可见的字符。



不知道什么会导致它被破坏,除了在无响应的maven repo更新期间强行退出日食,但是我不能确定如果这是我的烦恼开始。我很高兴我可以前进。


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.

解决方案

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.

这篇关于Java文件:prolog中不允许内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆