Some compilers failed on non-ASCII characters in JavaDoc and source code comments. What is the current (Java 7) and future (Java 8 and beyond) practices with respect to Unicode in Java source files? Are there differences between IcedTea, OpenJDK, and other Java environments, and what is dictated the the language specification? Should all non-ASCII characters be escaped in JavaDoc with HTML &escape;-like codes? But what would be the Java // comment equivalent?
Update: comments indicate that one can use any character set, and that upon compiling one needs to indicate what char set is used in the source file. I will look into this, and will be looking for details on how to configure this via Ant, Eclipse, and Maven.
This is likely because the compiler assumes that the input is UTF-8, and there are invalid UTF-8 sequences in the source file. That these appear to be in comments in your source code editor is irrelevant because the lexer (which distinguishes comments from other tokens) never gets to run. The failure occurs while the tool is trying to convert bytes into chars before the lexer runs.
The
manpage forjavacandjavadocsayso running
javadocwith the encoding flagafter replacing
<encoding-name>with the encoding you’ve used for your source files should cause it to use the right encoding.If you’ve got more than one encoding used within a group of source files that you need to compile together, you need to fix that first and settle on a single uniform encoding for all source files. You should really just use UTF-8 or stick to ASCII.
The algorithm for dealing with a source file in Java is
'\\''u'followed by four hex digits with the code-unit corresponding to those hex-digits. Error out if there is a"\u"not followed by four hex digits.The current and former practice is that step 2, converting bytes to UTF-16 code units, is up to the tool that is loading the compilation unit (source file) but the de facto standard for command line interfaces is to use the
-encodingflag.After that conversion happens, the language mandates that
\uABCDstyle sequences are converted to UTF-16 code units (step 3) before lexing and parsing.For example:
is a valid pair of Java statements.
Any java source code tool must, after converting bytes to chars but before parsing, look for \uABCD sequences and convert them so this code is converted to
before parsing. This happens regardless of where the \uABCD sequence occurs.
This process looks something like
[105, 110, 116, 32, 97, 59, 10, 92, 117, 48, 48, 54, 49, 32, 61, 32, 52, 50, 59]['i', 'n', 't', ' ', 'a', ';', '\n', '\\', 'u', '0', '0', '6', '1', ' ', '=', ' ', '4', '2', ';']['i', 'n', 't', ' ', 'a', ';', '\n', a, ' ', '=', ' ', '4', '2', ';']["int", "a", ";", "a", "=", "42", ";"](Block (Variable (Type int) (Identifier "a")) (Assign (Reference "a") (Int 42)))No need except for HTML special characters like
'<'that you want to appear literally in the documentation. You can use\uABCDsequences inside javadoc comments.Java process
\u....before parsing the source file so they can appear inside strings, comments, anywhere really. That’s whyis a valid Java statement.
is equivalent to
as far as javadoc is concerned.
You can use
//comments in java but Javadoc only looks inside/**...*/comments for documentation.//comments are not metadata carrying.One ramification of Java’s handling of
\uABCDsequences is that althoughlooks like a single line comment, and many IDEs will highlight it as such, it is not.