How do I get the character encoding of the currently running JVM? - java

I have an issue where I think my local JVM is running with a different character encoding to one that runs on a server. Is there a command I can pass to the vm to display what character encoding is has? Something like
java show 'charset'
I've been searching for ages, and all of the solutions require writing a test in java code, but I don't really want to have to write code, deploy it to the server, etc.

Related

Issues with windows command line using utf-8 code page

In my application, I'm reading the properties file(UTF-8 encoded) containing Chinese characters and printing them on the Windows command line. But,for some reason the the messages are not getting displayed correctly(some extra characters are coming). However the same mesages are getting displayed correctly on Eclipse console and Cygwin. I've set the command line code page to utf-8(65001) and used "Lucida" font as well.
If you see the above image, on Windows it printed one extra 0 on the second line, but that is not expected;on cygwin the message was printed correctly.
Please let me know if I'm missing something. From this post , I can see that there are some issues with Windows UTF-8 code page implementation. If so, is there any other way to get over this problem ?
I can see that there are some issues with Windows UTF-8 code page implementation
oh most surely yes
is there any other way to get over this problem ?
The typical solution is to accept that the Windows command prompt is a broken disaster and move on. But if you really have to, you can use JNA to call the Win32 function WriteConsole directly, avoiding the broken byte encoding layer, when you can determine you are outputting to the console (rather than eg a pipe).

JavaFx application in Windows is not displaying text correctly

So I have an application written in JavaFx 2.2 that has been packaged for linux, mac, and windows. I am getting a strange issue with some of the text fields though. The application will read a file and populate some labels based on whats found in the file. When run on ubuntu or mac we get a result like as you can see we have that special accent character over the c and it looks just fine. However in Windows it shows up like this . Any idea as to why this is happening? I was a bit confused as it is the same exact application on all three. Thanks.
Make sure to specify character encoding when reading the file, in order to avoid using the platform's default encoding, which varies between operating systems. Just by coincidence, the default on Linux and Mac happens to match the file encoding and produces correct output, but you should not rely on it.

Defining accent characters in different platforms under java

I have a problem with the accent character in different platforms.
When I log this in my machine under fedora (where default charset is UTF-8) it is printing correvtly as Sacré Coeur.
But when i update to another server that is running on RedHat (where default charset is ISO-8859-1), it is printing as
Sacré Coeur. I want to log it in RedHat server as same as in my my Fedora machine. How can I do this?
My Workout :
I tried to changes the System.setProperty("file.encoding",
"ISO-8859-1"); in local with the purpouse of doing the reverse
version System.setProperty("file.encoding", "UTF-8"); in the RedHat
Server, if it change the way of logging in the local. But nothing
changed.
I noticed there are couple of threads regarding the accent character
but nithing answers me. That's why I asked a new question.
I tried this one as well but not working.
System.setProperty("file.encoding","ISO-8859-1");
Field charset =Charset.class.getDeclaredField("defaultCharset");
charset.setAccessible(true);
charset.set(null,null);
But I didn't try to set the charset at the JVM start. If it will works please explain me how can I do it?
To get similar out put from all the environments, with out depending on the server OS
default character encoding, when you start your program or the server environment (Jboss tomcat or jetty) pass -Dfile.encoding to the start-up script
(lets say run.sh in jboss, add -Dfile.encoding=UTF-8 to JAVA_OPTS)
-Dfile.encoding=UTF-8

Character encoding between Java (Linux) and Windows system

I have a simple program that makes a request to a remote server running a service which I believe is written in Delphi, but definately running on Windows.
I'm told the service will be using whatever the default encoding is for Windows.
When I get a response and use println to output it I'm getting some strange symbols in the output, which make me think it is a character encoding issue.
How can I tell Java the the input from the remote system is in the windows encoding?
I have tried the following:
_receive = new BufferedReader(new InputStreamReader(_socket.getInputStream(),"ISO-8859-1"));
_System.out.println(_receive.readLine());
The extra characters appear as squares in the output with 4 numbers in the square.
Unless you KNOW what the "default encoding" is, you can't tell what it is. The "default encoding" is generally the system-global codepage, which can be different on different systems.
You should really try to make people use an encoding that both sides agree on; nowadays, this should almost always be UTF-16 or UTF-8.
Btw, if you are sending one character on the Windows box, and you receive multiple "strange symbols" on the Java box, there's a good chance that the Windows box is already sending UTF-8.
Use cp1252 instead of ISO-8859-1, as it is default on windows.

Using Rxtx library to send messages to USB connected phone causes computer lock up/crash — why?

Ultimately I would like to use a Java program to send and receive messages from a phone that I have plugged in via USB. I can do this using a C# program, however the program I want to send and receive messages is written in Java. To do this I am using the Rxtx library (but using the Windows x64 compiled version from Cloudhopper). But whenever I try and send any commands to the phone via USB my computer completely locks up and I have to hard-restart it.
The code I am running is here: Two way communication with the serial port. I think that it successfully establishes a link since it gets to the stage where it accepts input from the console, though when I press enter, and the input is sent, the computer locks up.
I am running Windows 7 x64, using Eclipse. Thank you for any help.
A little hard to tell from the code, but here are some debugging tips:
Try stepping through the code with the debugger line by line, and step in to the library itself to see if you can find the problem.
Instead of reading/writing from the console, try sending character codes programmatically. The console operates very differently from direct access. i.e. instead of System.in.read()) just try passing in a known good String.
Keep in mind that Java works with UCS-16 internally, but that consoles typically work with different character encodings (e.g. cp1252 for Windows). So, your "enter" may be a completely different character from what the system is expecting. If your device is expecting ASCII 13 and your keyboard is generating ASCII 12, that could be enough to confuse things.
The crash makes it seem very likely that there is something going on with the native library. If you find that the Java debugging keeps dropping you into the JNI boundary, you may need to debug with a C/C++ toolset.
Good luck!

Categories