• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Issue with HashMap on Linux box

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All,

I see HashMap behaves differently on the JVM configured on linux box.

<Map>.put("”", "”") //Key is &followedby#followedby8221

On windows Map.get("”") returns correctly: i.e ”

On linux box it returns : ââ¬ï¿½

Is there any best alternative for Map to handle key-value pair with special character set?

Regards,
Vijay
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is nothing to do with HashMap. This is everything to do with how you are displaying the data you get from a HashMap.

You didn't say anything at all about how you're displaying the data, but you should start looking at that. In particular you should look at what encoding (a.k.a. charset) is being used to convert the data to whatever output technology you're using. (Java's default system coding is different on Linux than on Windows.) If you still have problems (and quite likely you will) then by all means post back here with more information about that.
 
Saloon Keeper
Posts: 27752
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem isn't with the HashMap, it's with the data and with the rendering of the data.

Windows web browsers are typically set to use the CP-1251 character encoding. Linux web browsers are more likely to be set for ISO-8559-1 Latin-1 encoding. Most of the time this doesn't matter, except when you get into the "On Beyond ASCII" characters - characters whose code values are 256 or higher. Which includes the left- and right-facing quote marks used for word processing instead of the neutral quotes that are part of basic ASCII.

Internally, ALL characters in Java are considered to be in Unicode, so the proper thing to do when storing such data is to make sure that regardless of what OS your client and server machines are on, that the character values you are storing into the hashmap have been converted to their Unicode values. It then becomes the responsibility of the output display mechanism to ensure that the Unicode values are rendered properly for the recipient's needs.
 
vdammala vkumar
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was developing a service for Web application , this problem exists in normal java code which is part of service layer.

I even tried adding below two jvm params , but no luck.

System.setProperty("file.encoding","Cp1252");
System.setProperty("sun.jnu.encoding","Cp1252");

I was suspecting the problem with HashMap since i am using set of special chars as key and value , it looks like it is internally ( JVM on linux box) executing default HASHING algorithm and returning some junk data.


I need make sure both Windows and Linux JVM props same w.r.t encoding type?
 
Paul Clapham
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And as I asked before, how are you observing those characters. In a web page? At a command line via System.out.println? In an e-mail?
 
vdammala vkumar
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Front end app sends those character set to the service request , I am trying to decode and populate in database.

Ideally front end app should restrict them , due to some other limitation i am trying to handle it in service layer.

 
Tim Holloway
Saloon Keeper
Posts: 27752
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a suspicion that the actual code in question looks like this:


But because you don't understand how HTML entities work, it doesn't display on our web browsers like that and so it makes no sense.

But that also is a risky way of coding, since the text editor on your development system could be generating the wrong character code, depending on not only what OS, but what editor you are using. So the safer version of that would be:


That sort of construct would be safely usable to translate Unicode right-quote characters into their HTML/XML entity equivalents, regardless of the OS.
 
Paul Clapham
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

vdammala vkumar wrote:Ideally front end app should restrict them , due to some other limitation i am trying to handle it in service layer.



No, the front end should handle them properly. And so should the other parts of the system. Ideally UTF-8 should be used as the charset throughout, so you don't have to write your own hacks to work around the problems which arise when that isn't the case. Websites which restrict themselves to only ASCII characters are so 20th-century.
 
vdammala vkumar
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Tim Holloway

I do agree with you on that , but how come it worked as expected on Windows JVM?

Please forget about Webapp , this particular question and piece of work doesn't involve any browser/html etc.
 
Tim Holloway
Saloon Keeper
Posts: 27752
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

vdammala vkumar wrote:@Tim Holloway

I do agree with you on that , but how come it worked as expected on Windows JVM?



Because it was written - and debugged - under Windows. Which is to say, incorrectly. It was made to "work" and not to work properly (OS-independent).

Java is "write once/run anywhere". But only if you pay strict attention to the platform's idiosyncrasies and avoid them. Repeating myself:

Tim Holloway wrote:
the text editor on your development system could be generating the wrong character code, depending on not only what OS, but what editor you are using



It's a certainty that Windows Notepad doesn't generate the same text characters as Windows Wordpad does, for example. That's even before you switch to Linux, which has different editors entirely.
 
vdammala vkumar
Ranch Hand
Posts: 64
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all for valuable time and inputs , I tried to fix the problem in JAVA which actually created by DataPower(IBM) ,as per your suggestions i am going ahead and working with IBM people for finding fix in DataPower itself.
 
Paul Clapham
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Excellent! Hopefully they can get the encoding issues sorted out.
 
So there I was, trapped in the jungle. And at the last minute, I was saved by this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic