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

What if there are Chinese garbled codes in the java linux file?

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article is to share with you what to do about Chinese garbled codes in java linux files. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Solutions to Chinese garbled code in java linux files: 1, download the sun source code of jdk1.8; 2, change the creation of Font from physical font to logical font; 3, restart the service.

This article operating environment: linux5.9.8 system, jdk1.8,Dell G3 computer.

The solution of Chinese garbled Code in Java under Linux Environment

I believe that many friends have encountered the problem of Java garbled, and recently I am also solving a problem of "using text to generate pictures in Chinese as well as special characters garbled"; it took me a lot of time to Debug the various source codes under sun.font and sun.awt, and finally understood its mechanism and solved the current problem; now write down the problem solving process and make a record so as not to encounter it again in the future.

Problems encountered

Here is the code I want to execute (extremely simplified, but the meaning remains the same):

Public static void main (String [] args) throws IOException {File file = new File ("test.png"); Font font = new Font ("Arial", Font.PLAIN, 10); BufferedImage bi = new BufferedImage (400,200, BufferedImage.TYPE_INT_ARGB); Graphics2D G2 = (Graphics2D) bi.getGraphics (); g2.setBackground (Color.WHITE); g2.clearRect (0,0,400,200); g2.setFont (font); g2.setColor (Color.BLACK) G2.setRenderingHint (RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); g2.drawString ("Why isn't the name special? @ Why don't you have the name "," g2.dispose (); ImageIO.write (bi, PNG, file);}

Of course, the goal is to see the following scenarios when you open test.png:

After there is no problem with local debugging, it is put on the test machine (Linux) to be executed, and the execution results are almost on the street:

Download the sun source code of jdk1.8

Follow the programmer's style: if there is a problem, then Debug!

The trick is that the current source package no longer contains the code of the sun package!

Fortunately, java officially confirmed that the OpenJDK code is basically the same as the JVM source code, which can be downloaded directly from OpenJDK8u: jdk8u

As for how to use the source code debug, don't write this, it won't be basic, so don't read this article.

Positioning problem

Download the source code directly, remote breakpoint, and the server executes. The first code that causes inconsistency between the local and the test server is first found in debug:

It turns out that JVM will use FontManagerFactory to get FontManager when creating Font, but different systems use different FontManager! Mac uses CFontManager, while Linux uses X11FontManager!

So what is the difference between the two FontManager?

CFontManager creates CFont as Font2D. This CFont is a class created by JVM specifically for mac. From the comments on classes and methods, you can see that physical fonts are sometimes wrapped by CFont in mac environment, and this is done in native code:

The Font2D created by X11FontManager is a collection of logical and physical fonts. X11FontManager inherits FcFontManager,FcFontManager inherits SunFontManager;. Let's take a look at X11FontManager's loadFonts () method, which directly uses SunFontManager's loadFonts (), SunFontManager's loadFonts () method to load physical fonts, SunFontManager implements FontManager's preferLocaleFonts () method, and loads logical fonts:

Logical fonts and physical fonts

The code debug has basically confirmed that there are font loading problems in different environments, so what are the logical and physical fonts found in the debug linux environment?

Physical font

Physical fonts are actual font libraries that contain glyph data and tables that map character sequences to glyph sequences using font techniques such as TrueType or PostScript Type 1. All implementations of Java Platform support TrueType fonts; support for other font technologies is implementation-related. Physical fonts can use font names, such as Helvetica, Palatino, HonMincho, or any number of other font names. In general, each physical font supports only a limited set of writing systems, for example, only Latin characters, or only Japanese and basic Latin. The collection of physical fonts available varies from configuration to configuration. Applications that require specific fonts can use the createFont method to bundle these fonts and instantiate them.

Logical font

Logical fonts are five font families defined by Java platforms that must be supported by all Java runtime environments: Serif, SansSerif, Monospaced, Dialog, and DialogInput. These logical fonts are not actual font libraries. In addition, logical font names are mapped to physical fonts by the Java runtime environment. Mapping relationships are related to the implementation and the usual locale, so they provide different appearances and specifications. Typically, in order to cover a large range of characters, each logical font name is mapped to several physical fonts.

Problem solving

Debug has a lot of source code, but this is the crux of the problem, and other debug content will not be posted.

Now that we have confirmed that the local (mac environment) is the code of native, we have encapsulated the physical font and converted it to CFont for rendering, while the X11FontManager of the Linux environment only loads the physical font and logical font for us, but we need to make our own choices, so the first step to solve the problem is obvious: change the creation of Font from physical font to logical font.

1 / / Serif, SansSerif, Monospaced, Dialog and DialogInput randomly choose 2 Font font = new Font ("Serif", Font.PLAIN, 10)

After the change, the execution of the code is still garbled! Continue Debug, found that the logical font Serif mapping on the Linux physical font does not have a Chinese font and the corresponding special symbol font, this is very simple, directly install the Chinese font (simsun.ttf) on the Linux, and then install the special symbol "font" can display the font (mysi.ttf), put these two fonts into the jdk fonts directory (JAVA_HOME/jre/lib/fonts). There is a method of installing Linux fonts at the end of the article.

After completing the above changes, restart the service and perform the successful display again! Celebrate warmly.

JVM logical font mapping configuration

The above changes can solve the problem of garbled Chinese and special characters, but I found in the Debug process that during the logical font loading process, JVM will refer to a configuration file, the code in sun.awt.FontConfiguration, this configuration class completes the mapping of logical fonts and physical fonts, and instructs SunFontManager to create logical fonts. The configuration file read by FontConfiguration is fontconfig.properties, and the configuration file directory is JAVA_HOME/jre/lib.

After looking up the data, the loading order of the JVM font configuration file is as follows:

JAVA_HOME/jre/lib/fontconfig.OS.Version.properties

JAVA_HOME/jre/lib/fontconfig.OS.Version.bfc

JAVA_HOME/jre/lib/fontconfig.OS.properties

JAVA_HOME/jre/lib/fontconfig.OS.bfc

JAVA_HOME/jre/lib/fontconfig.Version.properties

JAVA_HOME/jre/lib/fontconfig.Version.bfc

JAVA_HOME/jre/lib/fontconfig.properties

JAVA_HOME/jre/lib/fontconfig.bfc

OS is the system, such as Linux, CentOs, RedHat, etc.; Version is the version number

In this configuration file, you can modify the correspondence between logical fonts and physical fonts, that is, you can manually modify the real physical fonts used by the five logical fonts Serif, SansSerif, Monospaced, Dialog and DialogInput in different scenarios.

Take Chestnut, for example, the following configuration uses simsun.ttf for Chinese serif.plain logical fonts and java native fonts for Latin:

# @ (#) linux.fontconfig.SuSE.properties 1.203 Sun Microsystems 10 Copyright 2003 Sun Microsystems, Inc. All rights reserved.## Versionversion=1# Component Font Mappingsserif.plain.chinese=-misc-simsun-medium-r-normal--*-%d-*-*-c-*-iso10646-1serif.plain.latin-1=-b&h-lucidabright-medium-r-normal--*-%d-*-*-p-*-iso8859-scene Search Sequencessequence.allfonts=latin-1 Chinese# Exclusion Ranges# Font File Namesfilename.-misc-simsun-medium-r-normal--*-%d-*-*-c-*-iso10646-1=/usr/share/fonts/myfonts/simsun.ttfLinux installation fonts

Linux font directory: / usr/share/fonts

Create a new directory under fonts, for example: mkdir myfonts

Put the fonts that need to be installed under the new directory, for example: cp ~ / test/simsun.ttf / usr/share/fonts/myfonts

Go to the myfonts directory: cd / usr/share/fonts/myfonts

Execute the following command:

Mkfontscale

Mkfontdir

Fc-cache-fv

Check to see if the corresponding font has been installed: fc-list

The fc-cache-fv command flushes linux's font cache to take effect immediately.

PS: all of the above operations basically require root permission

Thank you for reading! This is the end of the article on "what to do with Chinese garbled codes in java linux documents". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out 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