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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1992933/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 02:13:02  来源:igfitidea点击:

Illegal Character when trying to compile java code

javajavac

提问by muckdog12

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 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:

我有一个程序,它允许用户在富文本框中键入 java 代码,然后使用 java 编译器编译它。每当我尝试编译我编写的代码时,都会收到一个错误,提示我的代码开头有一个不存在的非法字符。这是编译器给我的错误:

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

采纳答案by Hans Passant

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.

当您未指定编码时,BOM 是由 File.WriteAllText() 或 StreamWriter 生成的。默认使用 UTF8 编码并生成 BOM。您可以使用它的 -encoding 命令行选项告诉 java 编译器这个。

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.

阻力最小的路径是避免生成 BOM。通过指定 System.Text.Encoding.Default 执行此操作,这将使用操作系统默认代码页中的字符写入文件,而不写入 BOM。使用 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

回答by skaffman

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. Its code point is U+FEFF. BOM use is optional, and, if used, should appear at the start of the text stream. 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 字符,用于表示文本文件或流的字节顺序(字节顺序)。它的代码点是 U+FEFF。BOM 的使用是可选的,如果使用,应出现在文本流的开头。除了作为字节顺序指示符的特定用途之外,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. 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.

BOM 是一个看起来很时髦的字符,您有时会在 unicode 流的开头找到它,它提供了编码是什么的线索。它通常被 Java 中的字符串处理东西隐形处理,所以你一定以某种方式混淆了它,但是如果没有看到你的代码,就很难看出在哪里。

You might be able to fix it trivially by manually stripping the BOM from the string before feeding it to javac. It probably qualifies as whitespace, so try calling trim()on the input String, and feeding the output of that to javac.

您可以通过在将 BOM 送入javac. 它可能符合空格的条件,因此请尝试调用trim()输入字符串,并将其输出提供给javac.

回答by Bozho

  1. If using an IDE, specify the java file encoding (via the properties panel)
  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.
  1. 如果使用 IDE,请指定 java 文件编码(通过属性面板)
  2. 如果不使用 IDE,请使用高级文本编辑器(我可以推荐Notepad++)并将编码设置为“无 BOM 的 UTF”或“ANSI”(如果适合您)。

回答by zneo

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. Notepad++ can convert a single files encoding, I'm not aware of a batch utility on the Windows platform for this.

您需要剥离 BOM 或将源文件转换为另一种编码。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 编译器将假定文件采用您的平台默认编码,因此如果您使用它,则不必指定编码。

回答by anand krish

That's a problem related to BOM (Byte Order Mark) character. Byte Order Mark BOM is an Unicode character used for defining a text file byte order and comes in the start of the file. Eclipse doesn't allow this character at the start of your file, so you must delete it. for this purpose, use a rich text editor like Notepad++ and save the file with encoding "UTF-8 without BOM". That should remove the problem.

这是与 BOM(字节顺序标记)字符相关的问题。字节顺序标记 BOM 是用于定义文本文件字节顺序的 Unicode 字符,位于文件的开头。Eclipse 不允许在文件开头使用此字符,因此您必须将其删除。为此,请使用像 Notepad++ 这样的富文本编辑器,并使用“UTF-8 without BOM”编码保存文件。那应该可以消除问题。

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.

回答by Akhil Arkyath

Even I was facing this issue as am using notepad++ to code. It is very convenient to type the code in 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. Later the modifications can be done using notepad++. It works!!!

甚至我在使用记事本++编码时也面临这个问题。在notepad++中输入代码非常方便。但是在编译后我得到一个错误“错误:非法字符:'\u00bb'”。解决方案:开始在旧版本的记事本中编写代码(默认情况下会在您的 PC 中)并保存它。稍后可以使用记事本++进行修改。有用!!!

回答by HelpNeeded

instead of getting Notepad++, You can simply Open the file with Wordpad and then Save As - Plain Text document

而不是得到 Notepad++,你可以简单地用写字板打开文件,然后另存为 - 纯文本文档

回答by Bobby Iveson

I solved this by right clicking in my textEdit program file and selecting [substitutions] and un-checking smart quotes.

我通过右键单击我的 textEdit 程序文件并选择 [substitutions] 并取消选中智能引号解决了这个问题。

回答by Ingo

In Android Studio

在 Android Studio 中

1. Menu -> Edit -> Select All
2. Menu -> Edit -> Copy
  1. Open new Notepad.exe
  1. 打开新的记事本.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