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:
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:
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
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.
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.
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:
Console output, in Terminal pane within IntelliJ:
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
Install the Code Runner extension
Use
Run Code
option to run the codeOutput the results in the OUTPUT panel
launch.json
Configure launch.json as follows
Start debugging in the Run and Debug window
Debug results are displayed in the DEBUG CONSOLE panel