“illegal character: \\65279” during Maven compile

2019-04-20 09:54发布

问题:

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>

回答1:

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.



回答2:

1.Close your project.

2.Try to open your file by notepad++,and switch to 'UTF-8 without BOM'.

3.Reopen your project again.



回答3:

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:

  1. Open your class from Android Studio/Eclipse
  2. Ctrl + A and then Ctrl + X
  3. Delete your empty class now
  4. Create a new Class with the same name
  5. Ctrl + V the code
  6. Done


回答4:

==, 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.



标签: java maven utf-8