skip to Main Content

I try to print Arabic text using Java, like these:

 System.out.println("طباعة نص باللغة العربية");

But the Output (in Terminal):

 ╪╚╟┌╔ غ╒ ╚╟طط█╔ ╟ط┌╤╚و╔

I think the problem on my terminal.
Because When I try to type the same text directly in the Terminal,
This is how the result looks:

enter image description here

I have tried these encoding: utf-8, utf-8 with Bom and windows1256 , but none of them worked. I am using Visual Studio code version 1.68.1 with JDK 17.0.4 .

I also edited settings.json file as follows:

enter image description here

The purpose is to print the text correctly. So, how can I print the string or content of text correctly in its original form? like:

 طباعة نص باللغة العربية 

3

Answers


  1. Have you set your LANG environment variable to the appropriate setting for Arabic output? The default for most systems is utf-8 which would not help much. There may be Java functions to change the language in use as well that you can use within your program before you try to output the data.

    Login or Signup to reply.
  2. Terminal tool’s character encoding

    Your problem is almost certainly that what ever terminal app you are using as your console is not configured for the correct character encoding.

    This has been covered many times already on Stack Overflow. Search to learn more.

    Use Swing as part of debugging

    We can eliminate your JVM and host operating system (supply of fonts, & font rendering) as sources of trouble by running this basic Swing app to display the Arabic text.

    package work.basil.example.swing;
    
    import javax.swing.*;
    
    // This example Swing code was adapted from this web page:
    // http://www.javapractices.com/topic/TopicAction.do?Id=231
    public class ShowArabicString
    {
        private static final String ARABIC_TEXT = "طباعة نص باللغة العربية";
    
        public static void main ( String... aArgs )
        {
            System.out.println( "java.vendor : " + System.getProperties().getProperty( "java.vendor" ) );
            System.out.println( "Runtime.version : " + Runtime.version() );
            System.out.println( "ARABIC_TEXT = " + ARABIC_TEXT );
    
            ShowArabicString app = new ShowArabicString();
            app.buildAndDisplayGui();
        }
    
        // PRIVATE
    
        private void buildAndDisplayGui ( )
        {
            JFrame frame = new JFrame( "Show Arabic text" );
            buildContent( frame );
            frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
            frame.pack();
            frame.setVisible( true );
        }
    
        private void buildContent ( JFrame aFrame )
        {
            JPanel panel = new JPanel();
            panel.add( new JLabel( ARABIC_TEXT ) );
            aFrame.getContentPane().add( panel );
        }
    }
    

    On a MacBook Pro 16" with Apple M1 Pro chip, running macOS Monterey 12.5.1, with Java 18.0.2, from IntelliJ IDEA 2022.2.2 RC (Ultimate Edition), I get the following GUI and the following console output in the IntelliJ Terminal pane:

    screenshot of Arabic text appearing correctly within a GUI window

    Console output, in Terminal pane within IntelliJ:

    java.vendor : Eclipse Adoptium

    Runtime.version : 18.0.2.1+1

    ARABIC_TEXT = طباعة نص باللغة العربية

    Login or Signup to reply.
  3. The answers and comments above have pointed out that this may be related to the terminal encoding and file encoding format, and have given a valid solution.

    Here are some of my suggestions that may be useful to you:

    Code Runner

    1. Install the Code Runner extension

    2. Use Run Code option to run the code

      enter image description here

    3. Output the results in the OUTPUT panel

      enter image description here

    launch.json

    1. Configure launch.json as follows

       {
           // Use IntelliSense to learn about possible attributes.
           // Hover to view descriptions of existing attributes.
           // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
           "version": "0.2.0",
           "configurations": [
               {
                   "type": "java",
                   "name": "Launch Current File",
                   "request": "launch",
                   "mainClass": "${file}",
                   "console": "internalConsole"
               },
      
           ]
       }
      
    2. Start debugging in the Run and Debug window

      enter image description here

    3. Debug results are displayed in the DEBUG CONSOLE panel

      enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search