简体   繁体   English

尝试编译Java代码时出现非法字符

[英]Illegal Character when trying to compile java code

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. 我有一个程序,允许用户在富文本框中键入Java代码,然后使用Java编译器对其进行编译。 Whenever I try to compile the code that I have written I get an error that says that I have an illegal character at the beginning of my code that is not there. 每当我尝试编译自己编写的代码时,都会收到一条错误消息,提示我在我的代码开头没有一个非法字符。 This is the error the compiler is giving me: 这是编译器给我的错误:

C:\Users\Travis Michael>"\Program Files\Java\jdk1.6.0_17\bin\javac" Test.java
Test.java:1: illegal character: \187
public class Test
 ^
Test.java:1: illegal character: \191
public class Test
  ^
2 errors

The BOM is generated by, say, File.WriteAllText() or StreamWriter when you don't specify an Encoding. 当您不指定编码时,BOM由File.WriteAllText()或StreamWriter生成。 The default is to use the UTF8 encoding and generate a BOM. 默认值为使用UTF8编码并生成BOM。 You can tell the java compiler about this with its -encoding command line option. 您可以使用-encoding命令行选项将此内容告知Java编译器。

The path of least resistance is to avoid generating the BOM. 阻力最小的路径是避免生成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. 通过指定System.Text.Encoding.Default来执行此操作,该操作将在操作系统的默认代码页中将包含字符的文件写入文件,并且不会编写BOM。 Use the File.WriteAllText(String, String, Encoding) overload or the StreamWriter(String, Boolean, Encoding) constructor. 使用File.WriteAllText(String,String,Encoding)重载或StreamWriter(String,Boolean,Encoding)构造函数。

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 . 它将产生mojibake

That's a byte order mark, as everyone says. 大家都说,这是一个字节顺序标记。

javac does not understand the BOM, not even when you try something like javac不了解BOM,即使您尝试类似

javac -encoding UTF8 Test.java

You need to strip the BOM or convert your source file to another encoding. 您需要剥离BOM表或将源文件转换为其他编码。 Notepad++ can convert a single files encoding, I'm not aware of a batch utility on the Windows platform for this. Notepad ++可以转换单个文件的编码,因此我不知道Windows平台上的批处理实用程序。

The java compiler will assume the file is in your platform default encoding, so if you use this, you don't have to specify the encoding. Java编译器将假定该文件采用您的平台默认编码,因此,如果使用此格式,则无需指定编码。

  1. If using an IDE, specify the java file encoding (via the properties panel) 如果使用IDE,请指定Java文件编码(通过属性面板)
  2. If NOT using an IDE, use an advanced text-editor (I can recommend Notepad++ ) and set the encoding to "UTF without BOM", or "ANSI", if that suits you. 如果不使用IDE,请使用高级文本编辑器(我建议使用Notepad ++ ),然后将编码设置为“不带BOM的UTF”或“ ANSI”(如果适合)。

http://en.wikipedia.org/wiki/Byte_order_mark http://en.wikipedia.org/wiki/Byte_order_mark

The byte order mark (BOM) is a Unicode character used to signal the endianness (byte order) of a text file or stream. 字节顺序标记(BOM)是Unicode字符,用于表示文本文件或流的字节序(字节顺序)。 Its code point is U+FEFF. 它的代码点是U + FEFF。 BOM use is optional, and, if used, should appear at the start of the text stream. BOM的使用是可选的,并且如果使用的话,应出现在文本流的开始。 Beyond its specific use as a byte-order indicator, the BOM character may also indicate which of the several Unicode representations the text is encoded in. 除了将其特定用作字节顺序指示符之外,BOM字符还可以指示文本在几种Unicode表示形式中的哪一种编码。

The BOM is a funky-looking character that you sometimes find at the start of unicode streams, giving a clue what the encoding is. BOM是一个看起来很时髦的字符,有时您会在unicode流的开头找到它们,以提供有关编码是什么的线索。 It's usually handles invisibly by the string-handling stuff in Java, so you must have confused it somehow, but without seeing your code, it's hard to see where. 它通常由Java中的字符串处理内容进行不可见的处理,因此您必须以某种方式对其进行了混淆,但是如果看不到代码,就很难知道在哪里。

You might be able to fix it trivially by manually stripping the BOM from the string before feeding it to javac . 您可以通过在将字符串提供给javac之前手动从字符串中剥离BOM来轻松修复它。 It probably qualifies as whitespace, so try calling trim() on the input String, and feeding the output of that to javac . 它可能符合空白条件,因此请尝试在输入String上调用trim() ,并将其输出提供给javac

That's a problem related to BOM (Byte Order Mark) character. 这是与BOM(字节顺序标记)字符有关的问题。 Byte Order Mark BOM is an Unicode character used for defining a text file byte order and comes in the start of the file. 字节顺序标记BOM是用于定义文本文件字节顺序的Unicode字符,位于文件的开头。 Eclipse doesn't allow this character at the start of your file, so you must delete it. Eclipse在文件的开头不允许使用此字符,因此必须将其删除。 for this purpose, use a rich text editor like Notepad++ and save the file with encoding "UTF-8 without BOM". 为此,请使用富文本编辑器(如Notepad ++)并使用编码为“ UTF-8 without BOM”的文件保存文件。 That should remove the problem. 那应该消除问题。

I have copy pasted the some content from a website to a Notepad++ editor,
it shows the "LS" with black background. Have deleted the "LS" content and 
have copy the same content from notepad++ to java file, it works fine.

通过右键单击textEdit程序文件并选择[替换]并取消选中智能引号,可以解决此问题。

In Android Studio 在Android Studio中

1. Menu -> Edit -> Select All
2. Menu -> Edit -> Copy
  1. Open new Notepad.exe 打开新的Notepad.exe

In Notepad 在记事本中

4. Menu -> Edit -> Paste
5. Menu -> Edit -> Select All
6. Menu -> Edit -> Copy 

Back In Android Studio 返回Android Studio

7. Menu -> Edit -> Paste

无需获取Notepad ++,您只需使用Wordpad打开文件,然后另存为-纯文本文档

Even I was facing this issue as am using notepad++ to code. 甚至我在使用notepad ++进行编码时都遇到了这个问题。 It is very convenient to type the code in notepad++. 在notepad ++中键入代码非常方便。 However after compiling I get an error " error: illegal character: '\»'". 但是,编译后出现错误“错误:非法字符:'\\ u00bb'”。 Solution : Start writing the code in older version of notepad(which will be there by default in your PC) and save it. 解决方案:开始在旧版本的记事本(默认情况下在PC上)中编写代码并保存。 Later the modifications can be done using notepad++. 以后可以使用记事本++进行修改。 It works!!! 有用!!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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