Avoid backslashes anywhere in Java code (Java error “illegal unicode escape”)

Did you know you can insert unicode-escaped characters, anywhere in a Java program?

Most of us are familiar with using unicode escapes like this:

String pound = "\u00A3";

but in fact constructs like \u00A3 can go anywhere, including in a comment.

This is all fine so long as they’re valid, but what if you’re generating Java code without due care and attention?

And what if you’re inserting file paths into your generated code?

And what if one of your directories has a name starting with a “u”?

Then you get code like this (edited example from our real project!):

// DO NOT EDIT!  This file was generated from:
// C:\usr\foo.xml

And, only on the machine with the dir called “usr”, we got a compile error like this:

MyClass.java:14: illegal unicode escape
// C:\usr\foo.xml
1 error

Which took me a while to track down.

Reference: JLS-3.3 Unicode Escapes.

Note this only applies to unicode escapes, not others like \n or \t – they are only processed within character or string literals (JLS-3.10.6).

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.