eclipse Java 文件:prolog 中不允许的内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32772352/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java file: Content not allowed in prolog
提问by rimsky
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.
我是 Spring 的新手,开始使用 Sprint 工具套件 (eclipse),并在我的 Maven 项目的 java 文件中看到此错误:“序言中不允许使用内容”。这可以防止类在 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);
}
}
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).
我浏览了网络,发现几乎在每个参考文献中,这是一个 XML 解析错误。我一直在更新三个 XML 文件,但<?xml
在十六进制编辑器之前没有看到任何不可见的字符。他们都指定使用 UTF-8 编码,我没有看到交替的 0 字节表示意外的 UTF-16 编码。行尾似乎一致(只是换行)。
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.
有趣的是,它使用“maven 全新安装”构建时没有编译器错误。我也没有在 java 文件中看到任何特殊字符,但是 eclipse 告诉我该文件是问题所在,这是一个 spring bean 问题。错误消息显示在包语句的第一行。
Any ideas what the root problem is or how I can solve this?
任何想法根本问题是什么或我如何解决这个问题?
UpdateAs requested, here's my pom.xml file:
更新根据要求,这是我的 pom.xml 文件:
<?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 2I 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.
更新 2我在我的项目文件夹中搜索了所有 .xml 文件,并使用 以编程方式删除了所有不可见字符perl -i -pe 's/[^\x20-\x7f\x0A\x09]//g' fileName
,但没有不可见字符。这证实了我在十六进制编辑器中看到的内容。
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?
想到两个问题:为什么错误显示在 .java 文件中而不是 a.java 文件中。xml文件?为什么这只是 eclipse 而不是 maven 的问题?
The eclipse log shows this message (which indicates a .xml file):
eclipse 日志显示此消息(指示 .xml 文件):
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.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.call(BeansConfig.java:456)
at org.springframework.ide.eclipse.beans.core.internal.model.BeansConfig.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 3I 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.
更新 3在<?xml
使用之前,我确实在我的工作区中找到了一些 eclipse 生成的 xml 文件,grep -ERl '^[^=]+<\?xml' .
其中包含字符,这忽略了一些 eclipse 用户首选项,这些用户首选项在文件的行中嵌入了 xml。删除多余的字符后,问题仍然存在。
Think I may restart the project and copy over files one at a time.
想我可能会重新启动项目并一次复制一个文件。
采纳答案by rimsky
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.
罪魁祸首似乎是一个损坏的 eclipse 工作区。我创建了一个新工作区并导入了项目,一切正常。这也可以解释为什么外部 Maven 构建没有显示任何问题。
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.
如更新 3 中所述,我发现了一些 eclipse 生成的 xml 文件,这些文件在序言之前具有字符。虽然纠正这些并没有解决问题,但我没有检查每个带有 xml 内容的 eclipse 文件(如用户首选项),以查看它们在序言之前是否有一些不可见的字符。
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.
不知道什么会导致它被损坏,除了在没有响应的 maven repo 更新期间强制退出 eclipse 但我不能确定这是否是我的麻烦开始的时候。我很高兴我可以继续前进。
回答by Cataclysm
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.
在我的情况下,这个错误发生在我重命名springboot 应用程序的主类(用@SpringBootApplication 注释的类)时。我并不是说这总是会发生,但在 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.
为了解决这个问题,我关闭了 IDE,导航到项目文件夹并删除.springBeans
文件。重新打开 IDE 后,这个错误消失了,我的 IDE 不再抱怨这个。
回答by TheLuminor
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 XML
is correct, but the file is not UTF-8
Encoded. 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
.
该错误很可能是由文件UTF-8
BOM
开头生成的XML
- 应用程序(例如Notepad
将其放在文件开头)而 Java 流无法自动处理该错误。你XML
是对的,但文件没有UTF-8
编码。我以前见过这个.. 尝试将这个论坛中的 XML 复制并粘贴到文本编辑器中并保存它。还要确保你的文本编辑器真的保存在UTF-8
.
Also, Spring
will work just fine without any <?xml ... >
declaration so go ahead and remove the comment between the DTD
declaration and the starting element
. Remove the UTF-8
declaration. Problems with UTF-8
encoded XML
in Java
are pretty common, and not specific to Spring
.
此外,Spring
在没有任何<?xml ... >
声明的情况下也能正常工作,因此请继续删除DTD
声明和开头之间的注释element
。删除UTF-8
声明。有问题的UTF-8
编码XML
中Java
是很常见的,而不是具体到Spring
。
回答by h2hjastermereel
For me it was because I had renamed some classes and the .springBeans file needed some manual editing.
对我来说,这是因为我重命名了一些类,并且 .springBeans 文件需要一些手动编辑。