How to compile a java source file which is encoded as "UTF-8"?

74,681

Solution 1

Your file is being read as UTF-8, otherwise a character with value "65279" could never appear. javac expects your source code to be in the platform default encoding, according to the javac documentation:

If -encoding is not specified, the platform default converter is used.

Decimal 65279 is hex FEFF, which is the Unicode Byte Order Mark (BOM). It's unnecessary in UTF-8, because UTF-8 is always encoded as an octet stream and doesn't have endianness issues.

Notepad likes to stick in BOMs even when they're not necessary, but some programs don't like finding them. As others have pointed out, Notepad is not a very good text editor. Switching to a different text editor will almost certainly solve your problem.

Solution 2

Open the file in Notepad++ and select Encoding -> Convert to UTF-8 without BOM.

Solution 3

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 : https://web.archive.org/web/20160506002035/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.

Solution 4

Try javac -encoding UTF8 One.java

Without the quotes and it's UTF8, no dash.

See this forum thread for more links

Solution 5

See Below For example we can discuss with an Program (Telugu words)

Program (UnicodeEx.java)

class UnicodeEx {  
    public static void main(String[] args) {   
        double ఎత్తు = 10;  
        double వెడల్పు = 25;   
        double దీర్ఘ_చతురస్ర_వైశాల్యం;  
        System.out.println("The Value of Height = "+ఎత్తు+" and Width = "+వెడల్పు+"\n");  
        దీర్ఘ_చతురస్ర_వైశాల్యం = ఎత్తు * వెడల్పు;  
        System.out.println("Area of Rectangle = "+దీర్ఘ_చతురస్ర_వైశాల్యం);  
    }  
}

This is the Program while saving as "UnicodeEx.java" and change Encoding to "unicode"

**How to Compile**

javac -encoding "unicode" UnicodeEx.java

How to Execute

java UnicodeEx

The Value of Height = 10.0 and Width = 25.0

Area of Rectangle = 250.0

Share:
74,681
asela38
Author by

asela38

programmer (scjp,scwcd,scbcd) Works as a Senior Software Engineer at Rakuten

Updated on November 21, 2021

Comments

  • asela38
    asela38 over 2 years

    I saved my Java source file specifying it's encoding type as UTF-8 (using Notepad, by default Notepad's encoding type is ANSI) and then I tried to compile it using:

    javac -encoding "UTF-8" One.java
    

    but it gave an error message"

    One.java:1: illegal character: \65279
    
    ?public class One {
    
    ^
    1 error
    

    Is there any other way, I can compile this?

    Here is the source:

    public class One {
        public static void main( String[] args ){
            System.out.println("HI");
        }
    }