Get/Set/Print the CharacterSet/ChracterEncoding used by the Java

  • Default Character encoding in Java or charset is the character encoding used by JVM to convert bytes into Strings or characters when you don’t define java system property “file.encoding”.
  • if Java doesn’t get any file.encoding attribute it uses “UTF-8” character encoding for all practical purpose e.g. on String.getBytes() or Charset.defaultCharSet(). 

Get the character encoding used by java in following ways :

  • “file.encoding” system property
  •  java.nio.Charset
  • by using Code InputStreamReader.getEncoding()

Print Character Encoding using by java or file

public class PrintCharacterEncodingUsed {
	public static void main(String[] args) throws FileNotFoundException {
		System.out.println(new InputStreamReader(new FileInputStream("/home/tysongill/Desktop/temp/encoding/h1")).getEncoding());

Set the character encoding used by java in following ways :

  • Using the System property “file.encoding”
    • by providing the file.encoding system property when JVM starts e.g. java -Dfile.encoding=”UTF-8″  HelloWorld.
  • Using the Environment variable “JAVA_TOOLS_OPTIONS”
    • If by anyway you don’t have control how JVM starts up may be JVM is starting through some scripts which doesn’t provide any way to accept system properties. you can set environment variable JAVA_TOOL_OPTIONS to -Dfile.encoding=”UTF-16″ or any other character encoding and it will picked up any JVM starts in your windows machine. 
    • JVM will also print “Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF16” on console to indicate that it has picked JAVA_TOOS_OPTIONS. here is example of setting default character encoding using JAVA_TOOLS_OPTIONS
test@system:~/java java HelloWorld
þÿExecuting HelloWorld
Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF16

Reference :

Leave a Comment