Problem with unicode Emoji with java.jar

I am developing a chat application in J2SE that can also send emoticons to another user.

The application uses https://github.com/vdurmont/emoji-java (Vdurmont Emoji-java-2.1 jar),

I followed everything that was described in this link: everything works fine during the development environment, but when I do jar for the same thing , when I send emoticons to another user on the Internet, it shows the code (ðŸ ~ ¡and?) .

Firstly, I think that the problem with downloading files from a folder was used by ClassLoader in such a way to get the correct image, but is it displayed during jar creation? (question mark). So I deleted this code to understand you better.

The code is written below:

public ChatUI() { initComponents(); this.setLayout(new WrapLayout(FlowLayout.LEFT, 5, 5)); for (int i = 0; i < imageHexaCode.length; i++) { final javax.swing.JLabel imogis = new javax.swing.JLabel("&#x" + imageHexaCode[i] + ";"); imogis.setCursor(new Cursor(Cursor.HAND_CURSOR)); imogis.setIcon(new javax.swing.ImageIcon(getClass().getResource("emoji_" + imageHexaCode[i] + ".png"))); imogis.setHorizontalTextPosition(JLabel.CENTER); imogis.setVerticalTextPosition(JLabel.BOTTOM); imogis.setFont(new Font(null, Font.PLAIN, 1)); imogis.setForeground(Color.white); final int aa = i; imogis.addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent e) { JLabel jl = new JLabel("&#x" + imageHexaCode[aa] + ";"); jl.setName("&#x" + imageHexaCode[aa] + ";"); jl.setFont(new Font(null, Font.PLAIN, 1)); jl.setHorizontalTextPosition(JLabel.CENTER); jl.setVerticalTextPosition(JLabel.BOTTOM); jl.setIcon(new javax.swing.ImageIcon(getClass().getResource("emoji_" + imageHexaCode[aa] + ".png"))); jl.setForeground(Color.white); ChatPaneWrite.jtp.insertComponent(jl); System.out.println("" + imogis.getText()); // you can open a new frame here as // i have assumed you have declared "frame" as instance variable } }); this.add(imogis); } this.revalidate(); this.repaint(); } 

Where imageHexaCode is an array of strings of icons.

  static String[] imageHexaCode = { "1f621", "1f608", "2764", "1f494" }; 

& jtp is a JTextPane, where we insert the component of emotions when clicking on the shortcut

 ChatPaneWrite.jtp.insertComponent(jl); 

Emoticons are stored in the same package where I write, why I did not use ClassLoader in the line

 jl.setIcon(new javax.swing.ImageIcon(getClass().getResource("emoji_" + imageHexaCode[aa] + ".png"))); 

or he can also write as for ChatUI package

 jl.setIcon(new javax.swing.ImageIcon(getClass().ClassLoader.getResource("ChatUI/emoji_" + imageHexaCode[aa] + ".png"))); 

here are the pictures:

The bank at the end of the reception shows that emoticons like this

enter image description here

Please help me sort it out.

Very grateful to everyone in advance

+7
java jar unicode emoji
source share
2 answers

The first step is to verify that the image resources are actually at runtime. For example, add some logging to getClass().getResource("emoji_" + imageHexaCode[aa] + ".png") values ​​to make sure it is not null . They may be available in your development environment, but due to the way you build your production version, they are somehow overlooked or placed in the wrong directory.

However, if you don't mind, I would also like to suggest an alternative approach: instead of using shortcuts and image icons, you thought that you create your emoticons in a font and then send the font file with your application? This would have several significant advantages:

  • You do not need special processing for emoticons: just put Unicode text in a component that uses its own font, and emoticons, as well as all other texts will work.

  • Font images can be vector images, which means they can scale well when users change the font size / zoom level, etc.

  • User can cut / copy / paste text containing emoticons; since it is text, it "just works" again.

If you decide to go this route, there is a great open source font editor that I used before calling FontForge: https://fontforge.imtqy.com/en-US/ . It's pretty simple: you just create the image in the appropriate slot for character code, and then export the font file.

+1
source share

emoji-java uses UTF-8 encoding for parsing, and by default Netbeans also uses the same, so your parsing stopped when you started Netbeans.

But when you run the program as a jar file, the encoding scheme will be based on your system environment.

As a quick fix, you can force the use of UTF-8 encoding to run the jar file.

 java -Dfile.encoding=UTF-8 -jar your-jar-file.jar 

Or if you want a software solution, you can use this code

 System.setProperty("file.encoding", "UTF-8"); java.lang.reflect.Field charset = null; charset = java.nio.charset.Charset.class.getDeclaredField("defaultCharset"); charset.setAccessible(true); charset.set(null, null); 

This is a common issue in emoji-java from vdurmont. Both solutions work for me.

0
source share

All Articles