Keywords: Java compilation | illegal character | BOM
Abstract: This article delves into illegal character errors encountered during Java compilation, particularly those caused by the Byte Order Mark (BOM). By analyzing error symptoms, explaining the generation mechanism of BOM and its impact on the Java compiler, it provides multiple solutions, including avoiding BOM generation, specifying encoding parameters, and using text editors for encoding conversion. With code examples and practical scenarios, the article helps developers effectively resolve such compilation errors and understand the importance of character encoding in cross-platform development.
Problem Symptoms and Error Analysis
In Java development, when users input code via a rich text box and attempt compilation, they may encounter error messages similar to the following:
C:\Users\Travis Michael>"\Program Files\Java\jdk1.6.0_17\bin\javac" Test.java
Test.java:1: illegal character: \187
public class Test
^
Test.java:1: illegal character: \191
public class Test
^
2 errors
These errors indicate that the compiler detects illegal characters at the beginning of the file, often caused by the Byte Order Mark (BOM). BOM is a special marker at the start of Unicode-encoded files to indicate byte order, but the Java compiler (e.g., javac) does not support BOM by default, leading to compilation failures.
Generation Mechanism of BOM
BOM is typically generated automatically when saving files with UTF-8 encoding, especially in certain programming environments or APIs. For example, in the .NET framework, methods like File.WriteAllText() or StreamWriter generate BOM by default when encoding parameters are not specified. Below is a sample code demonstrating how to avoid generating BOM:
// C# code example to avoid BOM generation
using System;
using System.IO;
using System.Text;
class Program
{
static void Main()
{
string code = "public class Test { public static void main(String[] args) { System.out.println(\"Hello World\"); } }";
// Specify Encoding.Default to avoid BOM
File.WriteAllText("Test.java", code, Encoding.Default);
}
}
By specifying Encoding.Default, the file is saved with the operating system's default code page, excluding BOM, thus preventing compilation errors. However, note that this approach may cause cross-platform compatibility issues, such as mojibake when compiling on machines in other regions.
Solutions and Best Practices
To address illegal character errors caused by BOM, the following solutions can be applied:
- Avoid Generating BOM: Explicitly specify encoding when saving files, such as using the
File.WriteAllText(String, String, Encoding)overload orStreamWriter(String, Boolean, Encoding)constructor, and choose encodings that do not generate BOM (e.g., ANSI or UTF-8 without BOM). - Use Compiler Encoding Parameters: Although javac does not support BOM by default, file encoding can be specified via the
-encodingcommand-line option. For example:javac -encoding UTF8 Test.java. However, this may not always work, as javac might still fail to parse BOM, so removing BOM beforehand is recommended. - Convert File Encoding: Use text editors (e.g., Notepad++) to convert file encoding to "UTF without BOM" or "ANSI". Notepad++ offers batch conversion features suitable for handling multiple files.
- IDE Settings: In integrated development environments (e.g., Eclipse or IntelliJ IDEA), specify Java file encoding through project properties or settings panels to ensure files are saved without BOM.
Below is a Java code example demonstrating how to read and process files that may contain BOM:
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
public class BOMHandler {
public static String readFileWithoutBOM(String filePath) throws IOException {
byte[] bytes = Files.readAllBytes(Paths.get(filePath));
// Check and skip BOM if present
if (bytes.length >= 3 && bytes[0] == (byte) 0xEF && bytes[1] == (byte) 0xBB && bytes[2] == (byte) 0xBF) {
return new String(bytes, 3, bytes.length - 3, StandardCharsets.UTF_8);
}
return new String(bytes, StandardCharsets.UTF_8);
}
public static void main(String[] args) {
try {
String content = readFileWithoutBOM("Test.java");
System.out.println("Processed content: " + content);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Additional References and Considerations
Other answers provide additional insights: for instance, in Android Studio, BOM can be removed by cutting and pasting into Notepad and back, but this is a temporary fix. Lower-scored answers emphasize the importance of using advanced text editors or IDE settings. Developers should note that character encoding issues not only affect compilation but may also cause display errors during cross-platform deployment. Therefore, it is advisable to standardize encoding early in projects and use version control tools (e.g., Git) to track file encoding changes.
In summary, resolving illegal character errors in Java compilation requires understanding the generation and impact of BOM, along with preventive or corrective measures. By combining encoding specifications, tool conversions, and code handling, cross-platform compatibility and compilability of code can be ensured.