Intellij compilation error: "illegal character' for É sign

1.1k views Asked by At

I have an Enum class in Java that contains the letter É:

public enum Types {
   RÉSUMÉ
}

When trying to build the project, IntelliJ complains on the É sign:

error: illegal character: '\u2030'
RÉSUMÉ
  ^

I'm using Windows 10.

In the past, the same project was compiled and run with no problems on my computer. So it seems like something in the settings was changed that caused this.

There is no option to replace É with E

Any idea how to fix this?

EDIT

  • The code used to run with JDK 11 and was upgraded to Java 17. Maybe it has something to do with it. Trying to downgrade the JDK of the project (Settings -> Build -> Gradle -> back to JDK 11) didn't help
1

There are 1 answers

0
Basil Bourque On

Character set & encoding of Java source code files

As commented, likely your source code files were written with a character encoding other than UTF-8. Now your compiler is expecting UTF-8, and reading your source code as such.

This problem could occur for either of two reasons:

  • Your settings for your compiler or IDE changed
  • You changed your JDK from an earlier version of Java to Java 18 or later.
    • Earlier versions of Java by default expect source code to be written in the default character set of the host OS platform.
    • Java 18+ defaults to UTF-8 across OS platforms for most purposes.

The main clue for this character encoding misreading hypothesis is that code point U+2030 is not the LATIN CAPITAL LETTER E WITH ACUTE character, nor is it the composition of an uppercase E followed by the accent. No, the code point 2030 in hex (8,240 in decimal) is PER MILLE SIGN: .

Java 18+ defaults to UTF-8

See JEP 400: UTF-8 by Default. To quote:

If source files were saved with a non-UTF-8 encoding and compiled with an earlier JDK, then recompiling on JDK 18 or later may cause problems. For example, if a non-UTF-8 source file has string literals that contain non-ASCII characters, then those literals may be misinterpreted by javac in JDK 18 or later unless -encoding is used.

You can easily verify if this new default is the source of your problem. Go back to your old project, and specify UTF-8 for compiling. Again, quoting the JEP:

Prior to compiling on a JDK where UTF-8 is the default charset, developers are strongly encouraged to check for charset issues by compiling with javac -encoding UTF-8 ... on their current JDK (8-17).