3

I print hindi characters as string in java and I'm getting an error. I save the java file in notepad uing UTF-8.

public class MainClass {
    public static void main(String args[]) throws Exception {
        String s = "साहिलसाहिल";

        System.out.print(s);
    }
}

After compilation, I get 2 errors of illegal character. Why so?

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
SAHIL SINGLA
  • 745
  • 2
  • 9
  • 26
  • It generally should compile (and it does, for me). Can you check the encoding once? – Nivas Sep 06 '11 at 10:34
  • What is the exact error that you get? Are you compiling on Windows or Linux or something else? – Jon Bright Sep 06 '11 at 10:34
  • This might be related: http://stackoverflow.com/questions/1726174/how-to-compile-a-java-source-file-which-is-encoded-as-utf-8 – wkl Sep 06 '11 at 10:34

4 Answers4

6

You have to specify the appropriate encoding :

javac -encoding utf8 MainClass.java
Tristan
  • 8,733
  • 7
  • 48
  • 96
2

Try compiling like this:

javac -encoding UTF-8 MainClass.java

Make sure that you use the correct name for the encoding too.

If this fails, it is likely that the problem that the Notepad is adding a BOM at the front, and that is confusing the java compiler. If that is your problem, you should stop using Notepad for editing UTF-8 files. Use a decent text editor that doesn't add a BOM to UTF-8 files.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
1

try it with

java -Dfile.encoding=UTF8 MainClass.java

Neifen
  • 2,546
  • 3
  • 19
  • 31
0

You need to save the source file in UTF-8 format (easy to do it using Notepad++ or Eclipse IDE) and then compile with the -encoding switch to javac.

javac -encoding utf8 MainClass.java
Nagendra U M
  • 601
  • 2
  • 6
  • 16