简体   繁体   中英

Printing Hindi characters as string in java results in error

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?

您必须指定适当的编码:

javac -encoding utf8 MainClass.java

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.

尝试一下

java -Dfile.encoding=UTF8 MainClass.java

您需要以UTF-8格式保存源文件(使用Notepad ++或Eclipse IDE轻松完成),然后使用-encoding开关编译为javac

javac -encoding utf8 MainClass.java

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM