Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"illegal character: \65279" during Maven compile despite being set to UTF-8

Tags:

java

maven

utf-8

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>
like image 633
Sled Avatar asked Jul 09 '13 18:07

Sled


3 Answers

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.

like image 86
Hut8 Avatar answered Oct 13 '22 01:10

Hut8


1.Close your project.

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

3.Reopen your project again.

like image 38
JasonChiu Avatar answered Oct 13 '22 01:10

JasonChiu


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
like image 38
Theodhor Pandeli Avatar answered Oct 13 '22 01:10

Theodhor Pandeli