记一次ubuntu docker 镜像 java 中文乱码问题查找

原创
2016/09/09 10:25
阅读数 2.4K

java 测试代码

public class Test {

	public static void main(String[] args) {
		System.out.println(java.nio.charset.Charset.defaultCharset());
		System.out.println(System.getProperty("file.encoding"));
        System.out.println("这是一段中文");
	}
}

Dockerfile

FROM ubuntu:14.04

# 时区
RUN ln -sf /usr/share/zoneinfo/Asia/Chongqing /etc/localtime && \
	echo "Asia/Chongqing" > /etc/timezone && \
	mkdir -p /etc/sysconfig/ && \
	echo -e "ZONE="Asia/Chongqing"\nUTC=false\nRTC=false" > /etc/sysconfig/clock

# jdk/jre/server-jre
ENV JAVA_TYPE=server-jre
ENV JAVA_VERSION_MAJOR=8
ENV JAVA_VERSION_DETAIL=101

COPY "${JAVA_TYPE}-${JAVA_VERSION_MAJOR}u${JAVA_VERSION_DETAIL}-linux-x64.tar.gz" /tmp/
RUN tar -zxf /tmp/${JAVA_TYPE}-${JAVA_VERSION_MAJOR}u${JAVA_VERSION_DETAIL}-linux-x64.tar.gz -C /opt/
RUN rm /tmp/${JAVA_TYPE}-${JAVA_VERSION_MAJOR}u${JAVA_VERSION_DETAIL}-linux-x64.tar.gz

RUN ln -s /opt/jdk1.${JAVA_VERSION_MAJOR}.0_${JAVA_VERSION_DETAIL} /opt/${JAVA_TYPE}${JAVA_VERSION_MAJOR}
RUN ls /opt/${JAVA_TYPE}${JAVA_VERSION_MAJOR}

RUN update-alternatives --install /usr/bin/java java /opt/${JAVA_TYPE}${JAVA_VERSION_MAJOR}/bin/java 100
RUN update-alternatives --install /usr/bin/javac javac /opt/${JAVA_TYPE}${JAVA_VERSION_MAJOR}/bin/javac 100

ENV JAVA_HOME=/opt/${JAVA_TYPE}${JAVA_VERSION_MAJOR}

docker 容器内测试

  1. 测试c.utf-8


root@e23dde2b15f3:/tmp# locale
LANG=
LANGUAGE=
LC_CTYPE="POSIX"
LC_NUMERIC="POSIX"
LC_TIME="POSIX"
LC_COLLATE="POSIX"
LC_MONETARY="POSIX"
LC_MESSAGES="POSIX"
LC_PAPER="POSIX"
LC_NAME="POSIX"
LC_ADDRESS="POSIX"
LC_TELEPHONE="POSIX"
LC_MEASUREMENT="POSIX"
LC_IDENTIFICATION="POSIX"
LC_ALL=

root@e23dde2b15f3:/tmp# vi Test.java
root@e23dde2b15f3:/tmp# javac Test.java
root@e23dde2b15f3:/tmp# java Test
US-ASCII
ANSI_X3.4-1968

root@e23dde2b15f3:/tmp# export LANG=C.UTF-8
root@e23dde2b15f3:/tmp# export LANG=C.UTF-8
root@e23dde2b15f3:/tmp# locale
LANG=C.UTF-8
LANGUAGE=
LC_CTYPE="C.UTF-8"
LC_NUMERIC="C.UTF-8"
LC_TIME="C.UTF-8"
LC_COLLATE="C.UTF-8"
LC_MONETARY="C.UTF-8"
LC_MESSAGES="C.UTF-8"
LC_PAPER="C.UTF-8"
LC_NAME="C.UTF-8"
LC_ADDRESS="C.UTF-8"
LC_TELEPHONE="C.UTF-8"
LC_MEASUREMENT="C.UTF-8"
LC_IDENTIFICATION="C.UTF-8"
LC_ALL=

root@bed9245d00ee:/tmp# java Test
UTF-8
UTF-8

  1. 测试 en_US.UTF-8
root@50563c7d7aea:/# locale
LANG=
LANGUAGE=
LC_CTYPE="POSIX"
LC_NUMERIC="POSIX"
LC_TIME="POSIX"
LC_COLLATE="POSIX"
LC_MONETARY="POSIX"
LC_MESSAGES="POSIX"
LC_PAPER="POSIX"
LC_NAME="POSIX"
LC_ADDRESS="POSIX"
LC_TELEPHONE="POSIX"
LC_MEASUREMENT="POSIX"
LC_IDENTIFICATION="POSIX"
LC_ALL=

root@50563c7d7aea:/# vi Test.java
root@50563c7d7aea:/# javac Test.java
root@50563c7d7aea:/# java Test
US-ASCII
ANSI_X3.4-1968

root@50563c7d7aea:/# export LANG=en_US.UTF-8
root@50563c7d7aea:/# locale
locale: Cannot set LC_CTYPE to default locale: No such file or directory
locale: Cannot set LC_MESSAGES to default locale: No such file or directory
locale: Cannot set LC_ALL to default locale: No such file or directory
LANG=en_US.UTF-8
LANGUAGE=
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=

root@50563c7d7aea:/# java Test
US-ASCII
ANSI_X3.4-1968

root@50563c7d7aea:/# locale-gen en_US.UTF-8
Generating locales...
  en_US.UTF-8... done
Generation complete.
root@50563c7d7aea:/# locale
LANG=en_US.UTF-8
LANGUAGE=
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=

root@50563c7d7aea:/# java Test
UTF-8
UTF-8
  1. locale
root@c44b590a4d16:/# locale -a
C
C.UTF-8
POSIX

root@c44b590a4d16:/# vi Test.java
# 直接编译java时 报错
root@c44b590a4d16:/# export LANG=C.UTF-8
root@c44b590a4d16:/# javac Test.java
root@c44b590a4d16:/# java Test
UTF-8
UTF-8
这是一段中文

root@c44b590a4d16:/# export LANG=en_US.UTF-8
root@c44b590a4d16:/# javac Test.java
# 这时候编译java会报错
root@c44b590a4d16:/# java Test
US-ASCII
ANSI_X3.4-1968
??????

root@c44b590a4d16:/# locale-gen en_US.UTF-8
Generating locales...
  en_US.UTF-8... done
Generation complete.

root@c44b590a4d16:/# locale -a
C
C.UTF-8
en_US.utf8
POSIX

root@c44b590a4d16:/# locale
LANG=en_US.UTF-8
LANGUAGE=
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=

root@c44b590a4d16:/# java Test
UTF-8
UTF-8
这是一段中文

问题分析: 在默认情况下, locale 只有C, C.UTF-8, POSIX, 所以 当export LANG=C.UTF-8 打印为 UTF-8, 中文显示正常 当export LANG=en_US.UTF-8 打印为 US-ASCII, ANSI_X3.4-1968, 中文显示不正常 执行 locale-get en_US.UTF-8 之后 打印为 UTF-8, 中文显示正常

展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
0 评论
0 收藏
0
分享
返回顶部
顶部