Trying to show Arabic characters in Java

I try to show Arabic characters in a Java applet, but always get < Signs . >.

I tried many solutions without success:

I am using Windows 7 in a Spanish language environment.

Some solutions work when starting Netbeans, but they do not work outside of this environment . Here's the Netbeans Project with sources and .jar .

This is the simple code I use:

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 what is going on?

+4
source share
7 answers

My original answer is incorrect: getBytes () creates a bytearray using the default system encoding that netbeans sets to UTF-8.

The correct answer: do not use ByteArrayOutputStream and a new line (byte [], Charset) at all. Use only strings. Should work fine.

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

+2
source

os.toString (...) is the wrong method. The characters inside ByteArrayOutputStream are supposed to be utf-8, which is not true since java uses utf-16. The method output, on the other hand, is a valid java string, which again is: utf-16.

So you are using an array containing utf-16 characters, interpret it as utf-8 and convert it to utf-16. There you have a problem ^^

EDIT: same issue with line:

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

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

+3
source

The simplest solution is to use strings normally and change the default encoding in your workspace, for example, eclipse.

Windows → Settings → General → Workspace → Text Encoding

Change the encoding to UTF-8.

There is no magic here.

+2
source

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

You must remove all conversions to bytes, they can only worsen the situation. Here's how it should work:

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

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

 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); 
+1
source

change the set encoding to UTF-8 in the Edit menu to Eclipse

0
source

I want to show you very useful code about Arabic in a jOptionPane post, I use jlabel to solve this problem, try the following:

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

I ran into this problem in Windows Server 2016, my Arabic characters were passed as "???" at Netbeans.

Decision:

  • Go to Control Panel → Region.
  • Click the Admin tab.
  • Click "Change System Local" to Arabic.
  • Reboot the server.
0
source

All Articles