1

How can I export as jar with utf8 encoding? When I export my project to JAR file, the I run jar file on command line it shows quesion marks. How can I fix it?

package utf8JAVA;

import java.io.UnsupportedEncodingException;

public class HelloWorld {

    public static void main(String[] args) throws UnsupportedEncodingException {
        String s = "ıııııəəəə";
        String UTF8Str = new String(s.getBytes(), "UTF-8");
        System.out.println(UTF8Str);
    }

}
  • After creating the jar file, while running the jar file from command use this -Dfile.encoding="UTF-8", it will work. – Sambit Oct 01 '19 at 18:00
  • It doesn;t works. My command is: java -Dfile.encoding="UTF-8" -jar test.jar – Ayxan Əmiraslanlı Oct 01 '19 at 18:03
  • Can you try with java -Dfile.encoding=UTF8 -jar test.jar, just remove the quotes. – Sambit Oct 01 '19 at 18:06
  • 3
    `new String(s.getBytes(), "UTF-8")` is very wrong. All Strings have the same encoding (UTF-16). Where are you seeing question marks? Is it in a command window in Windows? – VGR Oct 01 '19 at 18:06
  • Yes questions shows on command line – Ayxan Əmiraslanlı Oct 01 '19 at 18:08
  • What do you use for command line? Windows `cmd.exe` does not support utf8 by default, you would have to use a special configuration – Joni Oct 01 '19 at 18:15
  • I also tested on powersheel same result – Ayxan Əmiraslanlı Oct 01 '19 at 18:19
  • you can check this.https://stackoverflow.com/questions/44503588/text-utf-8-in-console-works-in-eclipse-but-fails-to-work-with-an-exported-jar – Sambit Oct 01 '19 at 18:54
  • Right after creating `s`, add `System.out.println(s.codePointAt(0));`. If it prints `305`, your String is correct and your file is correct, and you should not be messing with bytes; the problem is that a Windows command prompt cannot display those characters. – VGR Oct 01 '19 at 21:47
  • Until you remove `new String(s.getBytes(), "UTF-8")` there is no need for further testing. Ask if you have questions about that. – Tom Blodget Oct 01 '19 at 22:54

0 Answers0