“illegal character: \65279” during Maven compile d

2019-04-20 09:59发布

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>

标签: java maven utf-8
4条回答
贼婆χ
2楼-- · 2019-04-20 10:02

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
查看更多
三岁会撩人
3楼-- · 2019-04-20 10:10

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.

查看更多
地球回转人心会变
4楼-- · 2019-04-20 10:13

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

查看更多
疯言疯语
5楼-- · 2019-04-20 10:25

1.Close your project.

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

3.Reopen your project again.

查看更多
登录 后发表回答