Search code examples
javaunicodeutf-8java-8thai

Thai language is not showing in Java output


Unable to print Thai string value in Java console

public static void main(String [] args){
   String engParam = "Beautiful";
   String thaiParam = "สวย";
   System.out.println("Output :" + engParam + ":::" + thaiParam);}

Output is showing like:

Output :Beautiful:::à?ªà??à?¢

I think System.out.println will not be able to print the UTF-8 characters with default console settings. Is there any other way available to resolve this issue? help needed.


Solution

  • You don't specify your environment, but this approach worked for me on Windows 10 from within my IDE, and also from the Command window:

    • First, use a font that supports Thai characters. But also make sure that the font you choose can be set in the Command window, and not just within your IDE. Some can (e.g. Courier Mono Thai), and some can't (e.g. Angsana New). You can mess with the Registry to add font selections, but Courier Mono Thai was available by default, so I used that one.
    • Once you have identified a font that you can set in the Command window, you can probably use that in your IDE as well if its default font(s) can't handle Thai characters.

    Here are the steps to get things working:

    • Download font Courier Mono Thai. You can download it from several web sites but I got it from here.
    • Install the downloaded font. On Windows 10 all you have to do is select it (Courier_MonoThai.ttf) in File Explorer, right click, and select Install from the context menu.
    • Once the font is installed, make it the default font in the Command window. Open a Command window, click the icon in the top right corner, select Properties and then select Courier Mono Thai as your font:

      CmdFont

    • Run the application in your IDE. If the source code or the output don't render the Thai characters correctly, change the font. I used Courier Mono Thai in NetBeans, and everything looked good: NetBeansWindow
    • Finally run in the Command window. The Thai characters probably won't render correctly. To fix that just change the code page to the one that supports Thai (chcp 874) before running your application: cmdRun

    These instructions are specific to Windows 10. If you are running in a different environment update your question with full details of your platform and your IDE.


    Updated 12/15/19 to provide an alternative approach:

    Instead of using Code page 874 (Thai) from the Command window, you could do this instead:

    • Create a PrintStream that uses the UTF-8 charset, and write the output using that PrintStream.
    • In the Command window, use code page 65001 (UTF-8).

    Here's the code:

    package thaicharacters;
    
    import java.io.PrintStream;
    import java.io.UnsupportedEncodingException;
    import java.nio.charset.StandardCharsets;
    
    public class ThaiCharacters {
    
    public static void main(String[] args) throws UnsupportedEncodingException {
    
        String engParam = "Beautiful";
        String thaiParam = "สวย";
    
        // Write the output to a UTF-8 PrintStream:
        PrintStream ps = new PrintStream(System.out, true, StandardCharsets.UTF_8.name());
        ps.println("UTF-8: " + engParam + ":::" + thaiParam);
    }
    }
    

    And here's the output in the Command window, showing that:

    • The Thai characters are not rendered correctly when using the default code page (437), or the Thai code page (874).
    • The Thai characters render correctly using the UTF-8 code page (65001):

    chcp65001