Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The causes and Solutions of garbled codes in Java under linux

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail the reasons and solutions for Java garbled under linux. The content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

A) A graphical interface program based on awt (swing) displays garbled codes in Chinese.

In the graphical interface program based on awt (swing), the Chinese display garbled in the menu generally appears. The reason is that JVM cannot find a font to display Chinese, JVM does not have a Chinese font under the original installation, and the distribution version of linux is different, and the path and name of the font are different, so JVM generally can not find the font that comes with the linux operating system. The solution is to let JVM find the font in the linux operating system. For example, under Redflag 6.0, you can use the following command to solve the problem:

Mkdir / usr/java/jdk1.6.0/jre/lib/fonts/fallback ln-s / usr/share/fonts/chinese/TrueType/*.ttf / usr/java/jdk1.6.0/jre/lib/fonts/fallback

Generally, there is no need to modify the font configuration file (fontconfig.OS.Version.properties) in the JRE/lib/ directory.

B) the Chinese display garbled in the browser of the system user side of JSP (Servlet) based on the structure of Bash S

Garbled Chinese display on the page

For static files like HTML, the character set of the file is the same as that set in the file.

For dynamic files like JSP and Servlet that need to be compiled, JVM interprets class files and generates HTML files needed by client browsers when running. If Chinese garbled is generated, it is generally generated during compilation and running. If you use a commercial distribution version of Linux and server, you can usually just save the JSP or Servlet file as the UTF-8 character set and set it to UTF-8 in. If there are still garbled codes, you can solve them in the following ways:

① if the server is independently developed, you can modify the source code to specify the encoding method when JVM reads and writes files and generates HTML files needed by the client browser, so as to fundamentally solve the problem and enhance the adaptability of the server. The code is as follows:

New BufferedReader (new InputStreamReader (new FileInputStream (jspfile), "GBK"); new PrintWriter (serfile, "GBK"); new PrintWriter (new OutputStreamWriter (os, "GBK"))

Of course, ② can also modify the "locale" setting of JVM in the server's entry file (that is, the file with the mainsubfunction), as follows:

Locale.setDefault (new Locale ("zh", "CN"))

③ when using a commercial server, you can modify the shell file that starts the server and set the environment variable "export LC_ALL=zh_CN.UTF-8" before starting the server, thus changing the default "locale" value under this console.

Of course, ④ can also modify the "locale" setting of the operating system, but this method is generally adopted because changing the "locale" setting of the operating system will affect other applications.

The main reason for adopting the above method is that when JVM starts for the first time, the "locale" of the operating system will be set to the default "locale" of JVM. When the operating system does not set the "locale" value, JVM will set the default "locale" of JVM to the default "locale" of JVM. Of course, you can also modify the default "locale" setting of JVM when running the application, and when JVM converts strings to and from binary sequence streams. If the encoding is specified, it will be converted in the specified encoding, otherwise it will be converted according to the default "locale" of JVM.

Conclusion

The garbled code produced by the Chinese display of Java language under linux is mainly related to the font of JVM, the locale setting of the operating system, the default locale setting of JVM, the locale setting in the application program and the binary sequence stream directly specified in the application program and the character set of string conversion. It is due to the different character coding methods used in the conversion between binary sequence stream and string, or because JVM can not find the required font, we can take different methods to solve it, which method should be combined with the specific application environment.

So much for the reasons and solutions of Java garbled under linux. I hope the above content can be helpful to everyone and learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report