java d encoding_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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 这是一个 Java 运行时的命令行选项,表示将文件编码设置为 UTF-8。它可以在运行 Java 程序时作为参数传入,例如:"java -Dfile.encoding=utf-8 MyProgram"。 ### 回答2: "picked up java_tool_options: -dfile.encoding=utf-8" 这个提示信息通常出现在使用Java编译和执行程序的时候。这个提示就像一个告警,它意味着Java虚拟机在启动时发现了某个程序中设置了这个环境变量,这个环境变量指定了Java编译和执行程序时需要采用的字符集编码格式。 Java编译和执行时字符集编码是非常重要的,因为它直接影响着程序的运行效果和程序的输出。如此设置 -dfile.encoding=utf-8 ,是要指定编译和执行时使用的字符集编码格式为utf-8,这意味着这个程序是一个多语言的应用程序,他的输出包含多种语言的字符和符号。 这个警告信息不是一个错误信息,只是一个提示性的信息。虽然看上去有点烦人,但它并不会影响程序的运行效果。 而且,如果你不想看到这个提示信息,可以在启动命令加上 -Djava_tool_options="-Dfile.encoding=UTF-8",这样就可以屏蔽这个提示信息。 总之,"picked up java_tool_options: -dfile.encoding=utf-8" 这个提示信息提醒着我们在编写Java程序的时候一定要注意字符集编码格式的设置,以确保程序不会因字符集编码问题而出错。 ### 回答3: "picked up java_tool_options: -dfile.encoding=utf-8" 是一条提示信息,出现在使用 Java 命令行工具运行程序时的标准输出中。它告诉我们 Java 虚拟机(JVM)正在读取并使用环境变量中的 java_tool_options 参数。其中,-dfile.encoding=utf-8 部分是一个 JVM 系统属性,用于指定文件编码格式,这里指定为 UTF-8Java 的文件编码格式对于文本处理、字符集转换等操作非常重要。如果没有正确指定编码,可能会导致程序输出中文乱码、读取的文本文件内容出现错乱等问题。因此,当使用 Java 程序处理文本时,建议始终使用正确的编码方式进行操作,并在必要时使用 -dfile.encoding 参数进行指定。 值得注意的是,java_tool_options 环境变量是一个用于设置 JVM 系统属性的标准变量,可以用于调整 JVM 的运行参数,例如指定堆大小、开启调试模式等。具体使用方法可以参考相关文档或工具书。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值