Pages

Java and the JDK

Java was originally developed by James Gosling at Sun Microsystems, which was subsequently acquired by Oracle Corporation.

The Java Development Kit (JDK) is an implementation of either one of the Java Platform, Standard Edition, Java Platform, Enterprise Edition, or Java Platform, Micro Edition platforms released by Oracle Corporation in the form of a binary product aimed at Java developers on Solaris, Linux, macOS or Windows.

JDK packages can be downloaded from Oracle.

Snap!

Sometimes I use the Windows PowerShell ISE editor to create a file (e.g. X.java) containing code, such as:

class X {}
Then I attempt to compile the code using a command like javac X.java. This command invokes the compiler program called javac, which comes bundled with various editions of the Java Development Kit and is located, on the computer I'm using now, in the directory C:\Program Files\Java\jdk1.8.0_101\bin.

Sometimes the code fails to compile, and the compiler outputs an error message that includes information like:

■  c l a s s   X   { }
              ^
X.java:1: error: illegal character: '\u0000'

I've experienced this problem often enough that now when I see this sort of information in an error message, I immediately suspect the javac program tried to interpret the contents of the source code file using an encoding that is incompatible with the one used to create the file. I'm often able to successfully compile the file using the longer, more explicit command

javac -encoding unicode X.java
which instructs the javac program to interpret the contents of the file using an encoding that happens to be compatible with the one my Windows PowerShell ISE editor is configured to use by default. Alternatively, I could resolve the problem by changing the way the file is encoded so that the file’s encoding is compatible with whatever encoding my javac program is trying to use.

Some editors let you to configure the encoding the editor uses by default. Some let you specify the encoding when creating a file. Note, for example, the Encoding selector widget on Microsoft Notepad’s Save As window.

In a Nutshell

Digital computers use encoding schemes to produce and consume information represented by sequences of bits. Even within the context of a single computer, information is often encoded in different ways. Programs that consume information must use an encoding that is compatible with the encoding used to produce the information.

If you’re trying to compile a file containing Java code by invoking the javac compiler from a command-line interface and you encounter an error message that refers to an illegal character, then consider the possibility that the javac compiler is trying to interpret the contents of the file using an encoding that is incompatible with the encoding that was used to create the file.

If that is the problem: