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

Viewed 76171

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");
    }
} 
11 Answers

In the Intellij Idea(Settings>Editor>File Encodings), the project encoding was "windows-1256". So I used the following code to convert static strings to utf8

protected String persianString(String persianStirng) throws UnsupportedEncodingException {
    return new String(persianStirng.getBytes("windows-1256"), "UTF-8");
}

Now It is OK! Depending on the file encoding you should change "windows-1256" to a proper one

Related