tags:

views:

352

answers:

3

I saved my java source file specifying it's encoding type as UTF-8 (using notepad, by default notepad's encoding type is ANSI) then I tried to compile it using

javac -encoding "UTF-8" One.java

but it gave a error message

One.java:1: illegal character: \65279

?public class One {

^ 1 error

is there any other way, i can compile this java source

here is the source:

public class One {
public static void main(String[] args) {
 System.out.println("HI");
}}
A: 

Try javac -encoding UTF8 One.java

Without the quotes and it's UTF8, no dash.

See this forum thread for more links

StevenWilkins
Problem is the BOM which notepad adds and which javac doesn't need
asela38
A: 

Works fine here, even edited in Notepad. Moral of the story is, don't use Notepad. There's likely a unprintable character in there that Notepad is either inserting or happily hiding from you.

Nick Veys
+4  A: 

Your file is being read as UTF-8, otherwise a character with value "65279" could never appear. I believe javac always expects source code to be in UTF-8.

Edit: Actually, according to the javac documentation:

If -encoding is not specified, the platform default converter is used.

Decimal 65279 is hex FEFF, which is the Unicode Byte Order Mark (BOM). It's unnecessary in UTF-8, because UTF-8 is always encoded as an octet stream and doesn't have endianness issues.

Notepad likes to stick in BOMs even when they're not necessary, but some programs don't like finding them. As others have pointed out, Notepad is not a very good text editor. Switching to a different text editor will almost certainly solve your problem.

Daniel Pryden
I would remove the "good"
OscarRyz
@Oscar Reyes: Notepad is not a very text editor?
Daniel Pryden
+1: here's the alternative: use notepad++, editplus or consorts or, only if you've a good grasp on coding/building/running java at commandline, an IDE like Eclipse
BalusC
Notepad is decent for telling if a file *is* UTF-8 (or UTF-16) but this issue is a pretty serious one IMHO and a lot of people get tripped up by it.
Dan
Notepad sticks a BOM in there so that later on it (or anything else that understand BOMs) can figure out that the file is most likely UTF-8.
Pavel Minaev