2

I have an encoding problem in Eclipse (Indigo Service Release 1). Here's what happens:

I have a property file (language bundle) in German language and it is not displayed correctly when I open it in Eclipse. For instance: gelöscht is displayed as gel�scht. I have tried changing the File Association to Text Editor, Properties File Editor, JBoss Tools Properties Editor but it is shown like this in all cases.

The interesting part is that when I open the file in Notepad++ it is displayed correctly. In Eclipse, the text file encoding is set to UTF-8, and the encoding of the file (according to Notepad++) is ANSI.

Is there another setting I do not know about? How can I fix this problem to see the correct encoding in Eclipse?

Atticus
  • 1,622
  • 7
  • 32
  • 55
  • Note that `.properties` files in Java **must** be encoded in ISO-8859-1 and **not** UTF-8. That's one of the more strange/inconsistent decisions in Java. – Joachim Sauer Mar 12 '12 at 11:44
  • @JoachimSauer: Well, it depends on what's going to read them. Spring has supported other encodings for a long time, and you can pass any `Reader` into `Properties.load` now. – Jon Skeet Mar 12 '12 at 11:48
  • @JonSkeet: of course, but there's a whole lot of infrastructure that doesn't actually **use** that feature. And the *save* bet is to use the default encoding (and/or use unicode escapes for all non-ASCII characters). – Joachim Sauer Mar 12 '12 at 11:49
  • @JoachimSauer: The default encoding is a terrible bet, IMO - who's to say that the default encoding of your machine is the same as my machine? You should *definitely* use a fixed, well-specified encoding - which should be ASCII-with-Unicode-escapes or UTF-8 IMO. – Jon Skeet Mar 12 '12 at 11:53
  • @JonSkeet: I've been unclear: I meant the "default encoding for .properties files as defined by `Properties.load(InputStream)` which happens to be ISO-8859-1". Yes, using the platform default encoding is a terrible idea. – Joachim Sauer Mar 12 '12 at 11:55
  • @JoachimSauer: Right, that's fair enough. Will edit my post to mention ISO-8859-1. – Jon Skeet Mar 12 '12 at 11:56
  • https://stackoverflow.com/questions/9180981/how-to-support-utf-8-encoding-in-eclipse – Sathya ChandraKala Mar 29 '21 at 14:22

2 Answers2

5

How are you reading the properties files? Storing them in any non-ASCII encoding other than ISO-8859-1 (the default encoding expected by various bits of infrastructure) or UTF-8 sounds like a bad idea to me. I would strongly recommend that you stick to either ASCII, ISO-8859-1 or UTF-8.

These days you can load Properties with any Reader, but UTF-8 is usually the most appropriate encoding to use for files which need to cope with non-ASCII characters and work on any machine.

You can probably force Eclipse to think of it as using a difference encoding, by right-clicking on the file, selecting Properties, and then selecting a "Text file encoding" at the bottom of the Resources page - but I would suggest that you don't do that.

An alternative is to only use ASCII with appropriate \u.... escape sequences, as generated by native2ascii.

(While you can include non-ASCII ISO-8859-1 characters, I personally wouldn't - it's obvious when characters aren't ASCII, but the distinction between ISO-8859-1 and other encodings gets trickier to spot with the naked eye.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • In the application the values are shown correctly. I only have problems when seeing the files in Eclipse. I also have problems when I change a value in that property file because then all the special characters will be shown incorrectly. I am using Turbine as an application framework and as far as I know that uses the ResourceBundle java class to read propery file values. My property files are encoded in ANSI (according to Notepad++) but I have specified UTF-8 in Eclipse, so I don't know why it changed. – Atticus Mar 12 '12 at 12:10
  • @Atticus: How are the files being *read* in the application? Also, you shouldn't just believe Notepad++ - it will be guessing to some extent at least. You need to understand that a text file is just a bunch of bytes, being *interpreted* in a particular encoding. – Jon Skeet Mar 12 '12 at 13:07
  • In the application, the he files are being read by the java.util.ResourceBundle.getString(String) method. – Atticus Mar 12 '12 at 13:31
  • @Atticus: Okay, so in that case you *must* use ISO-8859-1 or Unicode escaping. (I'd urge Unicode escaping, to avoid this being an issue.) You may want to consider having "master" properties files in whatever encoding you want, and then running native2ascii as part of your build. – Jon Skeet Mar 12 '12 at 13:34
  • But the strange problem is that I do not have problems with displaying the characters in the application (unless I change something in the property files). I think my problem is that my Eclipse tries to open the property files as if they were in UTF-8 encoding, but in fact they are in ANSII, therefore the special characters are displayed incorrectly in Eclipse. I think this is the case because I have tried opening the files in simple Notepad where you can specify the encoding and it works with ANSII and it does the same error as in eclispe when opening as UTF-8. What do you think? – Atticus Mar 12 '12 at 13:49
  • @Atticus: They're not ANSI (there's no such encoding as ANSII, and even the term ANSI is ambiguous), they're ISO-8859-1 - but chances are the differences aren't biting you *at this point*. However, as soon as you start trying to use a character which differs in ISO-8859-1 from whatever exact encoding Notepad++ is calling "ANSI" you *will* have problems. – Jon Skeet Mar 12 '12 at 13:51
0

Text files donot carry encoding information with them, so what you are seeing is a best guess. Notepad++ just guesses the file is ANSI.

The file in question is not UTF8 (or atleast wasn't saved in that way the last time someone edited it). You will have to resave the file as UTF8 or change the encoding in Eclipse to something like CP1252.

Recognizing the correct format automatically is not really possible.

john16384
  • 7,800
  • 2
  • 30
  • 44