I am trying to compile a Maven Java project with files saved as UTF-8 that have a BOM, but I am getting an illegal character error from the BOM character in the despite I having both the project.build.sourceEncoding
as well as the encoding
of the maven-compiler-plugin
set to UTF-8
.
Am I missing an additional setting? Can I even get this to compile without removing the BOM (not allowed to make any change to the source, but I can modify the POM)?
The error:
java: C:\code\main\src\test\java\net\initech\finance\FinanceTest.java:1: illegal character: \65279
The property:
<properties>
...
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
...
</properties>
The plugin:
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
This happened to me after I opened a Java class using notepad. This changed the Encoding of the file.
A simple trick I did is this:
If it's UTF-8 and not UTF-16, the BOM serves no purpose at all. Why is it put in there? Also, only Java is complaining here -- not Maven.
Check out JDK-4508058 : UTF-8 encoding does not recognize initial BOM which is related.
==, take a look at the difference of the equal signs. The right one is used for normal English type, but the left longer one is usually used for Chinese languages.
1.Close your project.
2.Try to open your file by notepad++,and switch to 'UTF-8 without BOM'.
3.Reopen your project again.