J punto Marcos
J punto Marcos

Reputation: 437

Trying to show arabic characters in Java

I am trying to show arabic characters in a Java applet but I always get Questions marks '?????'.

I tried many solutions with no success:

I am using Windows 7 in a spanish language environment.

Some solutions work when running Netbeans, but they do not work outside this environment. Here it is Netbeans project with sources and .jar.

This is simple code I am using:

package javaapplication4;

import java.io.ByteArrayOutputStream;
import java.nio.charset.Charset;
import javax.swing.JApplet;
import javax.swing.JOptionPane;

public class JavaApplication4 extends JApplet{

@Override
public void init(){
    try {

        String str1 = new String("تعطي يونيكود رقما فريدا لكل حرف".getBytes(), "UTF-8");
        JOptionPane.showMessageDialog(rootPane, str1);

        String str2 = new String("تعطي يونيكود رقما فر");  
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        os.write(str2.getBytes());
        JOptionPane.showMessageDialog(rootPane, os.toString("UTF-8"));

    } catch (Exception ex) {
        JOptionPane.showMessageDialog(rootPane, ex.toString());
    }
}
}

Any idea of what is happening?

Upvotes: 7

Views: 38711

Answers (7)

Allen BMC
Allen BMC

Reputation: 27

I faced this issue in Windows Server 2016, my Arabic characters where passed as "???" in Netbeans.

Solution:

  1. Go to Control Panel -> Region.
  2. Open Administrative tab.
  3. Click “Change System Local” to Arabic.
  4. Restart the server.

Upvotes: 0

treeteq
treeteq

Reputation: 1

i wanna show you a very usefull code, about the arabic in jOptionPane message, i use jlabel to resolve this problem, try this:

JLabel jlbl1 = new JLabel("الملف غير موجود");
JOptionPane.showMessageDialog(null,jlbl1,"تنبيه", JOptionPane.ERROR_MESSAGE);

Upvotes: 0

user7037815
user7037815

Reputation: 11

change the set encoding to UTF-8 from the Edit menus on Eclipse

Upvotes: 1

Yishagerew
Yishagerew

Reputation: 94

The easiest solution would be using strings normally and changing the default encoding in your workspace for example eclipse.

Windows-->Preferences-->General-->workspace-->Text file encoding

Change the encoding to UTF-8.

There is no magic here.

Upvotes: 4

John Smith
John Smith

Reputation: 2332

My original Answer is wrong: getBytes() produces a bytearray using the system's default encoding, which netbeans sets to UTF-8.

Correct answer: Do not use ByteArrayOutputStream and new String(byte[], Charset) at all. Only use Strings. Should work fine.

EDIT: See comments for the actual problem and explanation why solution is not completely possible.

Upvotes: 2

Joni
Joni

Reputation: 111239

If your source code is encoded in UTF-8, you must set the -encoding parameter when compiling. Otherwise the compiler will use the system's default encoding, which is probably cp1252 in your case (Windows 7, Spanish), and doesn't support Arabic.

You should remove all the conversions to bytes, they can only make the matters worse. This is how it should work:

String str1 = "تعطي يونيكود رقما فريدا لكل حرف";
JOptionPane.showMessageDialog(rootPane, str1);

If you can't set compiler options you can use escape codes to encode the characters in ASCII. The native2ascii command line tool can do this conversion for you. For example, the code generated for the above two lines would be:

String str1 = "\u062a\u0639\u0637\u064a \u064a\u0648\u0646\u064a\u0643\u0648\u062f \u0631\u0642\u0645\u0627 \u0641\u0631\u064a\u062f\u0627 \u0644\u0643\u0644 \u062d\u0631\u0641";
JOptionPane.showMessageDialog(rootPane, str1);

Upvotes: 1

John Smith
John Smith

Reputation: 2332

os.toString(...) is the wrong method. It assumes that the characters inside the ByteArrayOutputStream are utf-8, which is not correct since java uses utf-16. The output of the method on the other hand is a valid java String which is again: utf-16.

So you use an array that contains utf-16 characters interpret it as utf-8 and convert it to utf-16. There you have your problem ^^

EDIT: same problem with the line:

new String("تعطي يونيكود رقما فريدا لكل حرف".getBytes(), "UTF-8");

getBytes() produces UTF-16 [THIS IS WRONG, SEE MY OTHER ANSWER], and you use it to create a String that interpretes the array as UTF-8

Upvotes: 1

Related Questions