Make (Java) JVM support Chinese output on Unix/Linux

Source: Internet
Author: User

Make (Java) JVM support Chinese output on Unix/Linux

1. Enable JVM to support Chinese output on Unix/Linux

For example
If you are using a remote Unix server, you will encounter the problem of Chinese font output in the image, especially because many administrators do not like to set the locale of the host as ZH (because it means that
The output is garbled or the Micrographic terminal must be installed like zhcon, but in many cases such conditions are not met ). The Java experience of most programmers is limited to JSP script programs.
Sent middleware, Servlet, applet, or GUI program running on Windows. If the developed jfreechart runs on Windows as the host
You can skip this section. However, if you are using a Unix server, you will often encounter unexpected difficulties in displaying Chinese characters. If you have not even reached the stage of outputting Chinese fonts, the program will report
Display Exception error. The reason is that Java AWT was originally compiled for (x) Windows GUI, and it often needs to use the display
Set the display mode in. In server mode (such as JSP or servlet), no xwindowns is running. In this case, can
Not got display setting
0: 0 errors, including jfreechart. The solution is to add the-djava. AWT. Headless = true setting during JVM startup. However, there is another question.
Headless occurs in the code using the method like frame. getimage ().
Exception causes the program to stop, but the program using imagebuffer will not be affected.

Java-based charts like jfreechart
AWT, Java swing, Java 2D
When APIs and programs are applied to Linux/Unix, the output of Chinese fonts is a problem that must be solved. Otherwise, jfreereport cannot be used. Servlet will also encounter classes
But the solution is relatively simple. The servlet package has a built-in solution. In general, set two sentences in the servlet header:

Response. setcontenttype ("text/html; charset = UTF-8 ");
Request. setcharacterencoding ("UTF-8 ");

Medium
Text garbled characters cannot exist. Compared with the simple JSP/servlet Character Set conversion, this problem is much more complex, even more complex than the general Linux culture. Under normal circumstances,
JRE only contains a few fonts (font), but can
The system, such as Windows, receives support for favorite fonts. Therefore, if developers and users are developing on a Chinese Windows system, they will probably not find the problem. However, once the program is released
On Unix/Linux systems, Chinese characters in the image are displayed as question marks or small boxes. At this time, the display of programs such as JSP/servlet is complete on the client.
All are normal. In general, Java is the use of en_OTF-8 by default, or iso_8859_1 to read the string, therefore, like JSP usually use a forced transformation from 8859_1
For gb3312/GBK, Chinese characters can be displayed normally, but in the above circumstances, this forced transformation is completely ineffective. Why? If the programmer's system concept is clear, he will understand,
JSP/servlet string output, only the output font, and then the client (usually a browser) in the client desktop restructuring the font, the use isClient font,On the contrary, graphics programs such as jfreechart output an image usingThe JRE Font on the server,And xwindows fonts. When the system itself does not contain fonts (font), which is common to servers, JRE can only be used to Add fonts that support Chinese characters (font.

JRE
The font setting principle is similar to that of xwindows, and the same tool is used. In fact, binary font files are extended to the same standard. The font sets between companies, such as Lenovo, founder, Microsoft, and
Linux
In xwindows, they are the same and can be copied to each other. The process of reading fonts is slightly different from the setting method. The contents refer to articles about Linux localization, mainly including adding Chinese fonts.
Many of them are useless words. I don't know what it means, so it cannot be repeated. So here we will review it first, and then compare it with the JRE settings. The principle will be clear.
. Currently, two Linux fonts are used:
The font of applications such as xwin, including pseudo console programs such as zhcon. Each app font program can have its own font setting directory;
However, with the enhancement of Linux integration, the XFS font service is always called through the default port unixsocket7000. Therefore, you only need to set the XFS font.
Complete. Some articles claim that it is unnecessary to stop XFS. The call of XFS is only used as a fontpath option in xfconfig and as a method to Add fonts,
Directly add the directory containing the font to fontpath, and then manually execute ttmkfdir. This is exactly what XFS was designed instead of yours. What the user actually needs to do is either direct
Add the font file to fonts :\\ in the graphic tool, or manually add the font file to the corresponding locale directory under the XFS directory.
/Usr/share/lib/fonts/zh_cn/TrueType. Restart XFS. As a directory manually added to xfconfig
In short, the font bitmap file is generic, including the JRE in a directory. What the user needs to do is to notify xwin where these directories are located, the configured location is
/Etc/X11/xconfig fontpath. Run the ttmkfdir command to generate the fonts. dir file, which is actually a comparison table of font calls. In addition, you can
To edit a file such as fonts. Alias, the aim is to give the font a name that is easy to remember. Therefore, the key to font installation is the font bitmap file (copied to a directory ).
Ttmkfdir command generation), and font alias settings, the difference is that in xwin, these are automatically completed by XFS. in JRE, the developer must manually complete them.

JRE
The font bitmap directory is fixed in the $ jre_home/lib/fonts directory. The directory comparison table files of fonts. dir * are the same.
The ttmkdir program is generated, which is equivalent to an alias and other settings file, which is defined in the * font. properties * "file under the $ jre_home/lib directory. If
JVM can directly support Chinese output. Therefore, the font type indicated in the * font. properties * attribute file must support Chinese characters (in other words, the built-in font file of jsdk is not
Supported in Chinese ). By hour:

Font. properties. <language >_< region >_< encoding>. <osversion>
Font. properties. <language >_< region >_< encoding>
Font. properties. <language >_< region>. <osversion>
Font. properties. <language >_< region>
Font. properties. <language >_< encoding>. <osversion>
Font. properties. <language >_< encoding>
Font. properties. <language >_< osversion>
Font. properties. <language>
Font. properties. <encoding>. <osversion>
Font. properties. <encoding>
Font. properties. <osversion>
Font. Properties

However
In most cases, only one font. properties file is required. Recompiling a font. properties file is a tough project. Fortunately
In Linux, there is a font. properties. Zh. Turbo, which is intended for turbolinux users, but can be modified based on it in most cases. Put this
Rename the file to font. properties to overwrite the original file, but the system still does not support Chinese characters. Check the file and you will find
In the font. properties. Zh. Turbo file, "-TLC-song-Medium-r-normal -- *-% d-*-C-*-GBK
-0 "fonts are not available in the fonts. dir table. These fonts are included in the system fonts library of turbolinux. There are two methods below: one is to install the font, the other is to change
A font library and re-specifies it. The turbolinux font installation file is named TTF-ZH-song * rpm, which can be found on the Internet.
Copy the TTF file under/usr/lib/X11/fonts/TT to the $ jre_home/lib/fonts directory and regenerate the fonts. dir file. Second
One way is to re-find the font library, the TTF file under the fonts directory on Microsoft Windows is generally available, but more fully download its character set file from the http://www.microsoft.com/china/windows2000/downloads/18030.asp, after installation, copy TTF to the fonts directory of JRE. In addition, if xwin supports Chinese, you can find one or two Chinese font files under/usr/lib/X11/fonts/TrueType.

Set
Copying all these files to the JRE fonts directory does not immediately enable JVM to support Chinese characters. In retrospect, the text type specified in font. properties must have one
Table fonts. dir indicates how JVM matches the font type called by the user to the corresponding font file. Therefore, run ttmkfdir>
Fonts. dir to generate a new table. Open this file with vi. The top number is the number of fonts that can be called by the system. The following attribute values are the physical font names on the left and the number on the right,
This is the use of font. Properties
The number specified in the file (including the settings, and the character alias on the left, that is, the virtual font ), the difference is to change some items in the 0-0c-0 setting to wildcards and % d to accept the call parameters.
That's all. You can do it without changing it. The output is ugly (I am not an artist, but I am not very concerned about it ). After the invalid font. properties settings are replaced by the available font numbers
It seems that JVM already supports Chinese characters, but in practice, question marks and spaces are still frequently seen. The reason is that Java's support for Chinese characters is not only related to the runtime environment, it is also related to compilation parameters. If
Class file is not compiled with gb2312/encoding, equivalent to read is OTF-8/8859_1, then conversion is useless, so if it is drawstring
-Encoding gb2312 must be used. Of course, if the operating system itself is already Chinese, this is automatically adopted by the compiler.


Author's blog:Http://blog.csdn.net/zwwwxy/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.