eclipsejava文件乱码 java中文乱码怎么解决

Eclipse java文件中文乱码 - 轻剑寂寥 - ITeye博客
博客分类:
/cnjsp/blog/item/e2954afb84b19c.html
问题描述:
程序涉及到国际化问题,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&name&=&value&
&&&&&&&&&&&&&& 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.
&&&& *
&&&& * &p& 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 =
&&&&&&&&&&&&&&& else
&&&&& defaultCharset = forName("UTF-8");
&&&&&&&&&&& }
}
return defaultC
}
在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");
另外另一个网友的博客上面看到的:
/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.
浏览: 715973 次
来自: 北京
朋友,你确定你的标题跟文章内容对应???
我只想说牛逼,就我接触过的那点云计算的东西,仔细想想还真是这么 ...
endpoint=&/Hello/messagebr ...
rsrsdgrfdh坎坎坷坷
谢谢。不过说得有点太罗嗦了。君,已阅读到文档的结尾了呢~~
扫扫二维码,随身浏览文档
手机或平板扫扫即可继续访问
eclipse导入项目java文件中文乱码,该如何解决
举报该文档为侵权文档。
举报该文档含有违规或不良信息。
反馈该文档无法正常浏览。
举报该文档为重复文档。
推荐理由:
将文档分享至:
分享完整地址
文档地址:
粘贴到BBS或博客
flash地址:
支持嵌入FLASH地址的网站使用
html代码:
&embed src='/DocinViewer-4.swf' width='100%' height='600' type=application/x-shockwave-flash ALLOWFULLSCREEN='true' ALLOWSCRIPTACCESS='always'&&/embed&
450px*300px480px*400px650px*490px
支持嵌入HTML代码的网站使用
您的内容已经提交成功
您所提交的内容需要审核后才能发布,请您等待!
3秒自动关闭窗口

我要回帖

更多关于 eclipse java文件乱码 的文章

 

随机推荐