error: illegal character: '\ufeff' in java - java

Got this error when compiled java code in ubuntu.
![Got this error][1]
error: illegal character: '\ufeff'
import java.net.*;
^
error: class, interface, or enum expected
import java.net.*;
^

As Jim Garrison pointed out, you probably have a Byte Order Marker (BOM) at the start of the file. Use an editor that can view all non-printable characters and remove it.
Alternatively, you can use sed to remove it:
sed '1s/^.//' infile >> outfile

If you are using IntelliJ, right click on the class file and select 'Remove BOM'.
That should remove the BOM at the start.

In case your are working on Windows O.S. and use Eclipse (which don't have the functionality to remove the BOM from the file), just open the file in Notepad++ and in the encoding menu, select "UTF-8", then save the file.

Use another editor because it seems to be an eclipse UTF-8-BOM problem.
Convert the UTF type UTF-8.
And also I note that ; First it doesn't work for me and I convert the type ANSI and after convert type to UTF-8 format. It can be a another alternative solution for you

downloading models from https://codebeautify.org/json-to-java-converter is causing such problems.
create new file and copy/paste all data from the downloaded file.

Related

Java encoding issue with ├ └

If I print
System.out.println("│ ├── └──");
I see only question marks (???). Seams that this is some king of encoding problem. Any ideas how to fix this?
Use the UTF-8 codes instead of the actual characters. For example ├ is \u251c.
Here is a link that will help you convert characters to corresponding codes: http://www.cylog.org/online_tools/utf8_converter.jsp
Hope it helps!
Any ideas how to fix this?
There are two possible causes of your problem:
1) It could be happening when you edit compile source code. The compiler could be reading the source code using a different file encoding to the one that you are using when you edit it. If you don't specify a source file encoding, the compiler will use a platform-specific default, and that might not be the right one.
The fix for this is to adjust your compiler settings to specify the correct source file encoding. How you do that will depend on how you are compiling. If you are compiling from the command line using javac, use the -encoding option.
Alternatively, a workaround for this problem is to replace the offending in your source code with Unicode escapes. For example:
String s = "\u251c";
should give you a one character string consisting of a "├" character. I would recommend the workaround. Source code that includes non-ASCII characters is always going to be sensitive to how you edit and compile ... and that is not a good thing.
2) It could be happening because there is a mismatch between your Java runtime platform's default output encoding and the actual encoding of whatever is displaying the output.
The fix for this is one of:
change the encoding for the display,
override the default encoding for the JVM (e.g. using -Dfile.encoding=UTF-8), or
change your code to output using a specific encoding.
Which is best depends on the circumstances; e.g. why things are "wrong" in the first place.
It is worth running this test application from the command prompt to see if the problem exists their too. If it does, then redirect standard output to a file, and use a hex dump utility (e.g. od on Linux) to see how the characters are encoded. That will help you distinguish causes 1) and 2) above.
(It is also possible that you have both problems ...)
The encoding of the java file (editor( and the encoding that the javac compiler better both use UTF-8. This generally is a IDE or project setting.
One might check that both encodings are equal, by the u-escaping of those chars: \u251C etcetera,
System.out must use the operating system encoding. If that encoding cannot convert those characters, one might see a ?. If the console is a console emulation of the IDE, you might search the setting of that encoding. Also check that the console font contains those graphic chars. Running the IDE with java -Dfile.encoding UTF-8 might help.
In your case: Strange. Check the source encoding with gedit, dump System.getProperty("file,encoding").

Javadoc error: unmappable character for encoding ASCII

Im trying to create a Javadoc but i can't.
I have written my comments in swedish så they content charachters as å,ä,ö.
This is giving me over 248 erros.
Is there a way to change the encoding for the whole Project?
I have tried:
Right-clicked on the project
Choosed Resource
Change to UTF-8
Restarted Eclipse
Create new Javadoc
This results in the following error:
error: unmappable character for encoding ASCII
Is there something else i can do to solve this problem?
Specifying UTF-8 as your resource encoding is a good thing to do, but you may also perform the following:
If you generate your Javadoc using javadoc binary, you may check its -encoding parameter:
javadoc:
usage: javadoc [options] [packagenames] [sourcefiles] [#files]
...
-encoding <name> Source file encoding name
Using Eclipse, you may specify this option in field "Extra Javadoc options (...):" in last Wizard step (example: -encoding UTF-8).
I know it's old question, but maybe it would be helpful for someone.
I want to add something to Xav's answer (I cannot add comments so I write answer):
Javadoc gives you following description (javadoc -help):
-encoding <name> Source file encoding name
-charset <charset> Charset for cross-platform viewing of generated documentation
"-encoding" parameter indicates how to read characters. You can also use "-charset" option to making your HTML documentation more readable.

Invalid Header Field Name when adding manifest to JAR using Eclipse

I'm using eclipse to develop a java applet and need to add an external jar to Class-Path in the manifest. I created the manifest file using notepad, used UTF-8 encoding and added the line break at the end, but attempting to add even a simple example manifest during jar creation results in the following error from Eclipse.
invalid header field name: ï>¿Manifest-Version
invalid header field name: ï>¿Manifest-Version
I have a feeling that it's some simple configuration issue, but I can't figure out what.
To test it, my manifest consists of the single line
Manifest-Version: 1.0
Notepad save your file with a special mark named 'BOM' at the beginning : 2 bytes (U+FEFF) which identify UTF-8 format.
You have to edit the file with Notepad++ (or vi) and encode it as UTF-8 (without BOM).
Next steps to make JARs:
Using eclipse
Using Apache ANT
Using Apache Maven
In Notepad++ , from the encoding menu , choose encode in UTF-8 without BOM .
In eclipse, right click in file properties and choose text file encoding to ISO-8859-1
Back in editor delete the special chars.

Why do I need to escape unicode in java source files?

Please note that I'm not asking how but why. And I don't know if it's a RCP specific problem or if it's something inherent to java.
My java source files are encoded in UTF-8.
If I define my literal strings like this :
new Language("fr", "Français"),
new Language("zh", "中文")
It works as I expect when I use the string in the application by launching it from Eclipse as an Eclipse application :
But if fails when I launch the .exe built by the "Eclipse Product Export Wizard" :
The solution I use is to escape the chars like this :
new Language("fr", "Fran\u00e7ais"), // Français
new Language("zh", "\u4e2d\u6587") // 中文
There is no problem in doing this (all my other strings are in properties files, only the languages names are hardcoded) but I'd like to understand.
I thought the compiler had to convert the java literal strings when building the bytecode. So why is the unicode escaping necessary ? Is it wrong to use use high range unicode chars in java source files ? What happens exactly to those chars at compilation and in what it is different from the handling of escaped chars ? Is the problem just related to RCP cache ?
It appears that the Eclipse Product Export Wizard is not interpreting your files as UTF-8. Perhaps you need to run Eclipse's JVM with the encoding set to UTF-8 (-Dfile.encoding=UTF8 in eclipse.ini)?
(Copypasta'd at OPs request)
When exporting a plug-in, it gets compiled through a process separate from the normal build process within the IDE. There is a known bug that the build process (PDE.Build) disregards the text encoding used by the IDE.
The export can be made to work properly by specifying the text encoding in the build.properties file of your plugin
javacDefaultEncoding.. =UTF-8

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. 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. 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.
That's a byte order mark, as everyone says.
javac does not understand the BOM, not even when you try something like
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.
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.
If using an IDE, specify the java file encoding (via the properties panel)
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.
In this case do the following Steps 1-7
In Android Studio
1. Menu -> Edit -> Select All
2. Menu -> Edit -> Cut
Open new Notepad.exe
In Notepad
4. Menu -> Edit -> Paste
5. Menu -> Edit -> Select All
6. Menu -> Edit -> Copy
Back In Android Studio
7. Menu -> Edit -> Paste
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.
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.
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.
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.
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.
I solved this by right clicking in my textEdit program file and selecting [substitutions] and un-checking smart quotes.
instead of getting Notepad++,
You can simply
Open the file with Wordpad
and then
Save As - Plain Text document
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!!!
I had the same problem with a file i generated using the command echo echo "" > Main.java in Windows Powershell. I searched the problem and it seemed to have something to do with encoding. I checked the encoding of the file using file -i Main.java and the result was text/plain; charset=utf-16le.
Later i deleted the file and recreated it using git bash using touch Main.java and with this the file compiled successfully. I checked the file encoding using file -i command and this time the result was Main.java: text/x-c; charset=us-ascii.
Next i searched the internet and found that to create an empty file using Powershell we can use the Cmdlet New-Item. I create the file using New-Item Main.java and checked it's encoding and this time the result was Main.java: text/x-c; charset=us-ascii and this time it compiled successully.

Categories

Resources