Home  >  Article  >  Operation and Maintenance  >  java garbled code under linux

java garbled code under linux

angryTom
angryTomOriginal
2019-11-07 09:18:263023browse

java garbled code under linux

Causes and solutions to garbled characters in Java language

a) Graphical interface program based on awt (swing) displays garbled characters in Chinese

In graphical interface programs based on awt (swing), the Chinese display in the menu will usually be garbled. The reason is generally that the JVM cannot find the font used to display Chinese. The JVM does not have a Chinese font under the original installation. , and the distribution versions of Linux are different, and the paths and names of their fonts are different, so the JVM generally cannot find the fonts included in the Linux operating system. The solution is to allow the JVM to find the fonts included in the Linux operating system. The font library that comes with it, such as under Redflag 6.0, can be solved by the following command:

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 system client browser based on B/S structure JSP (Servlet) displays garbled Chinese characters

The Chinese characters in the page display garbled characters

For static content like HTML file, the character set of the file only needs to be the same as the one set in the 0f04c5bdbb60794ef339b96987c9e293 location in the file.

For dynamic files like JSP and Servlet, they need to be compiled. During operation, the JVM interprets the class file to generate the HTML file required by the client browser. If Chinese garbled characters are generated, it is usually compiled and run. generated during the process. If you use commercial Linux distributions and servers, you generally only need to save the JSP or Servlet file as the UTF-8 character set, and set 21c4de232fd95c180f8fa04993c532de Just set it to UTF-8. If garbled characters still appear, you can solve it by the following methods:

① If the server is independently developed, you can modify the source code to specify the JVM to read files, write files, and generate HTML files required by the client browser. , the encoding method when converting strings and binary sequence streams, thereby fundamentally solving the problem and enhancing the adaptability of the server. In MyWebServer 2.0, I adopted this method, and 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, you can also modify the "locale" setting of the JVM in the server's entry file (that is, the file containing the main subfunction). The code is as follows:

Locale.setDefault(new Locale(“zh”,”CN”));

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

④ Of course, you can also modify the "locale" settings of the operating system, but since modifying the "locale" settings of the operating system will affect other applications, this method is generally used.

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

Conclusion

The garbled characters generated by the Java language in Chinese display under Linux are mainly related to the JVM's font library, the locale settings of the operating system, the JVM's default locale settings, and the application The locale settings in and the binary sequence stream directly specified in the application are related to the character set for string conversion. It is due to the different character encoding methods used in the mutual conversion process of binary sequence streams and strings or the JVM cannot find the required font. Different methods can be used to solve it. The specific method to be used depends on the specific processing. Application Environment.

Recommended: [Linux Video Tutorial]

The above is the detailed content of java garbled code under linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn