0

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:

 طباعة نص باللغة العربية 
waroa kawa
  • 29
  • 8
  • 1
    What is the character encoding setting in your Terminal app? What is your Terminal app? Is that part of Microsoft Visual Studio? What is the name and version of your host operating system? – Basil Bourque Sep 11 '22 at 17:29
  • I have tried many encoding utf-8 and windows1256 ... but none of them worked. I am using Visual Studio code version 1.68.1 with JDK 17.0.4 – waroa kawa Sep 11 '22 at 17:48
  • 1
    Those details should be posted as an edit to your Question rather than as Comments. – Basil Bourque Sep 11 '22 at 17:55
  • And your host OS name and version? – Basil Bourque Sep 11 '22 at 18:05
  • Explain *exactly* what you mean by “I have tried many encoding utf-8 and windows1256“. I suspect you have not specified an encoding for the Terminal app. In other words: This is not a Java problem. (most likely) – Basil Bourque Sep 11 '22 at 18:07
  • 1
    Perhaps the problem is that the encoding used by your application's `PrintStream` (i.e. `System.out`) is incorrect for writing to the VS Terminal? Does the code provided in [this answer](https://stackoverflow.com/a/73594488/2985643) help? Of course, to set that encoding correctly means that you need to also know the encoding used by your Terminal. – skomisa Sep 13 '22 at 08:25
  • The answer you mentioned @skomisa did not work in my case. – waroa kawa Sep 14 '22 at 11:49

3 Answers3

1

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 = طباعة نص باللغة العربية

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • Just to add that an additional (and arguably simpler) debugging approach is to have your app write to a file. But as you have shown, Swing also gets the job done. – skomisa Sep 13 '22 at 08:12
  • This is the solution I'm currently using, debugging using `swing app` and this works for me correctly, but I want to debug in the console, and fix this. – waroa kawa Sep 13 '22 at 14:17
1

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

JialeDu
  • 6,021
  • 2
  • 5
  • 24
0

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.

DonYeet46
  • 13
  • 6