简体   繁体   English

jar 文件中的 UTF-8 文本

[英]UTF-8 text in jar file

When I run maven project from IntelijIdea and use for example当我从 IntelijIdea 运行 maven 项目并使用例如

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

everything is ok and I see "Привет" in output.一切正常,我在 output 中看到“Привет”。

But when I compile project with maven and run jar file I get?????但是当我用 maven 编译项目并运行 jar 文件时,我得到了?????? in output.在 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.我猜,jar 文件还是正确的。 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".您的 JRE 通常使用“平台默认编码”将字符输出到System.out 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). (当然,在 maven 编译的 JAR 文件中,检查存在问题的环境)。 That should give "UTF-8".那应该给出“UTF-8”。 If not, run with -Dfile.encoding=UTF-8 on the java command line and see if things changed.如果没有,请在 java 命令行上使用-Dfile.encoding=UTF-8运行,看看情况是否发生了变化。

Then it might be that the console where you're outputting doesn't support UTF-8.那么可能是您输出的控制台不支持UTF-8。 If you're on a Windows system, check which code page the Windows Console uses.如果您使用的是 Windows 系统,请检查 Windows 控制台使用的代码页。 You can change that with你可以改变它

> CHCP 65001

and then run the JAR file.然后运行 JAR 文件。 65001 is the windows code page number meaning UTF-8. 65001 是 windows 代码页号,意思是 UTF-8。 Hopefully then you'll see the correct output.希望您能看到正确的 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. 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.如果打印的长度不是 6,则说明源文件编码存在混淆。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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