java 乱码问题-Dfile.encoding=UTF-8
http://blog.csdn.net/telnetor/article/details/5555361
问题描述:
程序涉及到国际化问题,httpclient抓回来的数据乱七八糟的乱码,在转了几次编码之后在Myeclipse下可以获取正常编码的源码(准确的说是能显示一大部分,少部分内容依然乱码),但是将程序移植到eclipse下先前的程序就出现了乱码(移植工作曾经尝试过以下几种形式:1,程序从myeclipse中导出,然后再从Eclipse中导入;2,将Eclipse工作空间切换到myeclipse工作空间。然后将工程编码设置为utf-8)。如果将myeclipse工程使用fatjar打包为可执行jar包,然后在windows下运行的话还是乱码。而在Linux环境下是正常的。
在Linux上执行定时任务的时候出现乱码(最早的时候系统为默认的环境,安装好的系统没有对环境设置进行修改,这时候程序运行良好,当安装了一些涉及到语言之类的包以后,程序异常)。
问题分析:
Myeclipse、Eclipse中的工程编码都是utf-8
windows环境编码GBK、Linux环境下没有出现异常时默认编码是utf8、异常时虽然 系统环境显示utf-8,但是因为安装了一些涉及系统环境的包之后,对编码为utf-8有些 许怀疑
程序中转换的最终编码utf-8
怀疑对象是程序运行环境的默认编码问题
在使用java自带工具Java virtualVM分析程序内存、线程使用情况时,意外发现JVM arguments一项中的参数对在Eclipse和myeclipse下运行程序有所不同:Eclipse里面得参数只有-Xmx1024m而myeclipse下的程序则多出一项:-Dfile.encoding=UTF-8
在启动程序时多加上-Dfile.encoding=UTF-8参数,程序Eclipse中乱码消失,Linux下定时任务执行的程序也没有乱码了
-Dfile.encoding解释:
在命令行中输入java,在给出的提示中会出现-D的说明:
-D=
set a system property
-D后面需要跟一个键值对,作用是设置一项系统属性
对-Dfile.encoding=UTF-8来说就是设置系统属性file.encoding为UTF-8
那么file.encoding什么意思?字面意思为文件编码。
搜索java源码,只能找到4个文件中包含file.encoding的文件,也就是说只有四个文件调用了file.encoding这个属性。
在java.nio.charset包中的Charset.java中。这段话的意思说的很明确了,简单说就是默认字符集是在java虚拟机启动时决定的,依赖于java虚拟机所在的操作系统的区域以及字符集。
代码中可以看到,默认字符集就是从file.encoding这个属性中获取的。个人感觉这个是最重要的一个因素。下面的三个可以看看。
/**
* Returns the default charset of this Java virtual machine.
*
*
The default charset is determined during virtual-machine startup and
* typically depends upon the locale and charset of the underlying
* operating system.
*
* @return A charset object for the default charset
*
* @since 1.5
*/
public static Charset defaultCharset() {
if (defaultCharset == null) {
synchronized (Charset.class) {
java.security.PrivilegedAction pa =
new GetPropertyAction("file.encoding");
String csn = (String)AccessController.doPrivileged(pa);
Charset cs = lookup(csn);
if (cs != null)
defaultCharset = cs;
else
defaultCharset = forName("UTF-8");
}
}
return defaultCharset;
}
在java.net包中的URLEncoder.java中的static块里面:
dfltEncName = (String)AccessController.doPrivileged (
new GetPropertyAction("file.encoding")
);
在javax.print包中的DocFlavor.java
static {
hostEncoding =
(String)java.security.AccessController.doPrivileged(
new sun.security.action.GetPropertyAction("file.encoding"));
}
在com.sun.org.apache.xml.internal.serializer包中的Encodings
// Get the default system character encoding. This may be
// incorrect if they passed in a writer, but right now there
// seems to be no way to get the encoding from a writer.
encoding = System.getProperty("file.encoding", "UTF8");
另外另一个网友的博客上面看到的:
http://yaojingguo.blogspot.com/2009/02/javas-fileencoding-property-on-windows.html
Java's file.encoding property on Windows platfor
This property is used for the default encoding in Java, all readers and writers would default to using this property. file.encoding is set to the default locale of Windows operationg system since Java 1.4.2. System.getProperty("file.encoding") can be used to access this property. Code such as System.setProperty("file.encoding", "UTF-8") can be used to change this property. However, the default encoding can be not changed dynamically even this property can be changed. So the conclusion is that the default encoding can't change after JVM starts. java -dfile.encoding=UTF-8 can be used to set the default encoding when starting a JVM. I have searched for this option Java official documentation. But I can't find it.
java 乱码问题-Dfile.encoding=UTF-8的更多相关文章
- Java -Dfile.encoding=UTF-8 出现乱码问题原因分析
这两天写了一个 Java 程序来玩,结果又遭遇了以前遇到过很多次的乱码问题,具体描述一下: 在 Mac 系统里面,常用的 Java 程序启动方式有如下几种: 1.通过 eclipse 执行 class ...
- Java -Dfile.encoding=UTF-8 干掉乱码
遭遇乱码问题的来龙去脉 这两天写了一个 Java 程序来玩,结果又遭遇了以前遇到过很多次的乱码问题,具体描述一下:在 Mac 系统里面,常用的 Java 程序启动方式有如下几种:1.通过 eclips ...
- 正确设置-Dfile.encoding参数
正确设置-Dfile.encoding参数 摘自:https://blog.csdn.net/youge/article/details/6178265 2011年02月11日 10:18:00 阅读 ...
- windows上java中文乱码-指定字符集 -Dfile.encoding=UTF-8
jvm启动中增加参数: -Dfile.encoding=UTF-8 重启即可.
- java 乱码详解_jsp中pageEncoding、charset=UTF -8"、request.setCharacterEncoding("UTF-8")
http://blog.csdn.net/qinysong/article/details/1179480 java 乱码详解__jsp中pageEncoding.charset=UTF -8&quo ...
- 配置tomcat启动参数-Dfile.encoding=UTF-8后,IDEA控制台乱码
配置tomcat启动参数-Dfile.encoding=UTF-8后,IDEA控制台出现乱码 解决方法: 在idea的bin目录(如:D:\JetBrains\IntelliJ IDEA 2018.1 ...
- 深入解析java乱码
1.什么是编码 ,为什么要编码 先前从没有思考这么深入的问题,觉得一切理所当然,直到有一天java的乱码让我跪了,他不在听我的话,到处是乱码,这次我不打算放过它,我要收拾了它. 大家都知道,文本文件, ...
- 解决 Mac 的 Terminal 中,Java 乱码的问题
在 .bash_profile 文件中,增加如下行: export JAVA_TOOL_OPTIONS=-Dfile.encoding=UTF-8 然后,重新加载该配置 source .bash_pr ...
- java乱码问题处理
java乱码问题处理 java乱码出现的问题有很多,这里主要解释tomcat,jsp,html,http(get,post请求乱码处理).常见的问题可能是tomcat,http请求乱码问题,对于jsp ...
随机推荐
- js打印的两种方法
第一种: <!--startprint1-->……打印的内容放在这里…… <!--endprint1--> //打印 function preview(DivID) { ) { ...
- 独立线程中实现QT GUI
在网上搜集的资料: http://www.qtcentre.org/threads/16552-Starting-QT-GUI-in-a-seperate-Threadhttp://stackover ...
- 如何从Windows Phone 生成PDF文档
我需要从我的Windows Phone应用程序生成PDF. 遗憾的是没有标准的免费的PDF生成库在Windows Phone上运行. 我不得不自己生成PDF,通过直接写入到文件格式. 这竟然是真的很容 ...
- HW4.37
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...
- A Tour of Go Variables
The var statement declares a list of variables; as in function argument lists, the type is last. pac ...
- 导入excel数据
前提条件:先要安装好EXCEL软件. 程序中经常要用到导入excel数据的功能.其实通过ole操作excel就简单的几行代码,但记性不好,经常要用经常要找, 还是作篇笔记吧. var ExcelApp ...
- jQuery中get与eq的区别
get与eq的区别 .eq() 减少匹配元素的集合,根据index索引值,精确指定索引对象. .get() 通过检索匹配jQuery对象得到对应的DOM元素. 同样是返回元素,那么eq与get有什么区 ...
- Fast特征检测
一.Fast算法 1.基本原理 Fast特征点检测feature2D原理是在圆周上按顺时针方向从1到16的顺序对圆周像素点进行编号.如果在圆周上有N个连续的像素的亮度都比圆心像素的亮度Ip加上阈值t还 ...
- JavaWeb文件的上传与下载(1)
经常用到的上传: 头像上传,资料分享等 文件上传的步骤 1.指定表单类型为文件上传表单 enctype="multipart/form-data" 2.表单提交方式必须为:post ...
- 使用dispatch_once:创建单列
无论是爱还是恨,你都需要单例.实际上每个iOS或Mac OS应用都至少会有UIApplication或NSApplication. 什么是单例呢?Wikipedia是如此定义的: 在软件工程中,单例是 ...