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:
طباعة نص باللغة العربية
CodePudding user response:
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.
CodePudding user response:
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:
Console:
java.vendor : Eclipse Adoptium
Runtime.version : 18.0.2.1 1
ARABIC_TEXT = طباعة نص باللغة العربية