简体   繁体   中英

UTF-8 text in jar file

When I run maven project from IntelijIdea and use for example

System.out.println("Привет")

everything is ok and I see "Привет" in output.

But when I compile project with maven and run jar file I get????? in output. How can it be fixed?

In general, there are multiple possible configuration problems regarding character encodings.

I guess, the jar file is still correct. Only the problem lies in outputting the characters to the console (or whatever you are using for the execution command line).

Your JRE typically outputs characters to System.out using the "platform default encoding". You can check it with

System.out.println(System.getProperty("file.encoding"));

(of course, in a maven-compiled JAR file, to check the envorinment where the problem exists). That should give "UTF-8". If not, run with -Dfile.encoding=UTF-8 on the java command line and see if things changed.

Then it might be that the console where you're outputting doesn't support UTF-8. If you're on a Windows system, check which code page the Windows Console uses. You can change that with

> CHCP 65001

and then run the JAR file. 65001 is the windows code page number meaning UTF-8. Hopefully then you'll see the correct output.

Still another aspect can often go wrong, but I guess it does not apply to your case: It's possible that you wrote your Java source files in UTF-8, but maven thinks the sources are encoded in eg ISO-8859-1. You can check that by

String text = "Привет";
System.out.println(text.length());
System.out.println(text);

If the length printed is not 6, then there's confusion about the source file encoding.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM