Compiling (javac) a UTF8 encoded Java source code with a BOM

后端 未结 3 1550
春和景丽
春和景丽 2021-02-02 13:37

Hello and thank you for reading my post.

My problem is the following: I want to compile a Java source file with \"javac\" with this file being UTF-8 encoded with a BOM (

相关标签:
3条回答
  • 2021-02-02 13:55

    This isn't a problem with your text editor, it's a problem with javac ! The Unicode spec says BOM is optionnal in UTF-8, it doesn't say it's forbidden ! If a BOM can be there, then javac HAS to handle it, but it doesn't. Actually, using the BOM in UTF-8 files IS useful to distinguish an ANSI-coded file from an Unicode-coded file.

    The proposed solution of removing the BOM is only a workaround and not the proper solution.

    This bug report indicates that this "problem" will never be fixed : http://bugs.java.com/view_bug.do?bug_id=4508058

    Since this thread is in the top 2 google results for the "javac BOM" search, I'm leaving this here for future readers.

    0 讨论(0)
  • 2021-02-02 14:01

    https://stackoverflow.com/a/28043356/7050261

    Actually, using the BOM in UTF-8 files IS useful to distinguish an ANSI-coded file from an Unicode-coded file.

    Actually

    • BOM is not about distinguishing ANSI and Unicode. Do not use a feature on purpose it is not designed for.

    • UTF-8 was designed to be backward-compatible with ANSI intentionally, so a lot of code written to process formatted text relied on 0..127 bytes only (XML, JSON, etc.) should work correctly with UTF-8 encoded text without any modifications.

    0 讨论(0)
  • 2021-02-02 14:04

    Trim the BOM and then use javac -encoding utf8 x.java

    0 讨论(0)
提交回复
热议问题