第一部分:问题描述
当WebSphere Application Server(以下简称为WAS)安装到中文Linux平台时,经常出现中文被显示为方块的情况,如下图所示:
图 1 应用程序组装工具乱码现象
图 2 FirstStep程序乱码现象
本文以在United Linux1.0下配置WebSphere Application Server 5.0 Base版本为例,描述了定位及解决中文显示乱码问题的过程。
第二部分:问题定位
Linux下Java程序界面中中文显示的问题,经常存在于XWindow配置、Java程序内部错误和Java环境配置等几个方面。为了辅助进行错误定位,我们可以写一个简单的Swing程序,如下所示:
//在JDK 1.3.1 下调试运行通过(Linux & Windows 平台)
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class HZSwingTest {
//Define two strings that containing DBCS and English character.
private static String DBCSValue = "This is a DBCS string[汉字]";
private static String DBCSWindowTitle = "DBCS Title[汉字]";
public Component createComponents() {
final JLabel label = new JLabel(DBCSValue);
JPanel pane = new JPanel();
pane.setBorder(BorderFactory.createEmptyBorder(30, 30, 10, 30));
pane.setLayout(new GridLayout(0, 1));
pane.add(label);
return pane;
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(
UIManager.getCrossPlatformLookAndFeelClassName()); }
catch (Exception e) { }
//Create the top-level container and add contents to it.
JFrame frame = new JFrame(DBCSWindowTitle);
HZSwingTest app = new HZSwingTest();
Component contents = app.createComponents();
frame.getContentPane().add(contents, BorderLayout.CENTER);
//Finish setting up the frame, and show it.
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.pack();
frame.setVisible(true);
}
}
该程序简单模拟了WAS出错时的界面,创建一个标题为中文字符串的窗口,其中还包含一个显示中文字符串的标签。在Windows 2000 中文版和United Linux 1.0平台上,其运行结果如下所示:
图 3 United Linux 1.0运行结果
图 4 Windows 2000中文版运行结果