Illegal Character when trying to compile java code

后端 未结 9 769
囚心锁ツ
囚心锁ツ 2020-12-29 03:09

I have a program that allows a user to type java code into a rich text box and then compile it using the java compiler. Whenever I try to compile the code that I have writte

9条回答
  •  别那么骄傲
    2020-12-29 03:52

    The BOM is generated by, say, File.WriteAllText() or StreamWriter when you don't specify an Encoding. The default is to use the UTF8 encoding and generate a BOM. You can tell the java compiler about this with its -encoding command line option.

    The path of least resistance is to avoid generating the BOM. Do so by specifying System.Text.Encoding.Default, that will write the file with the characters in the default code page of your operating system and doesn't write a BOM. Use the File.WriteAllText(String, String, Encoding) overload or the StreamWriter(String, Boolean, Encoding) constructor.

    Just make sure that the file you create doesn't get compiled by a machine in another corner of the world. It will produce mojibake.

提交回复
热议问题