Search code examples
javaascii

(Intellij) "unclosed character literal" and "illegal character '\u00a7'" when compiling


Im trying to compile the '§' character into a char (char c = '§') but when i try to build, it says in the build output "Unclosed character literal" and "Illegal character: '\u00a7'" followed by "Unclosed character literal" again. If i put the character into a String (String s = "§") it works fine. But when i print it to console, it prints the 'Â' character (which it shouldn't)

In another java project, i can use the '§' character fine and compile normally, and it works as intended; printing it to console shows nothing (which is normal, because it's used as an escape character for colouring the text). That project (and the current one) don't use "BOM" in intellij, and they both use UTF8 encoding

Does anyone know how to fix this? thanks:)


Solution

  • How I could reproduce that error:

    • created a new Test.java with UTF-8 encoding (default)
    • added main with char c = '§'; and print it
    • run Test.main()

    no errors, as expected.


    So I tried:

    • created second file Test1.java and changed to ISO-8859-1 encoding
    • added main method with just a print command
    • run Test1.java

    this time I got the reported error but for Test.java (still encoded as UTF-8)

    Looks like IDEA uses the encoding of first file for the whole source code.


    Solutions:

    • make sure all files are encoded with UTF-8; and/or
    • use javac -encoding UTF-8 ... as commented by saka1029