drools规则出现中文验证 与jvm 默认编码(The default charset is determined during virtual-machin

栏目: Java · 发布时间: 7年前

内容简介:drools规则出现中文验证 与jvm 默认编码(The default charset is determined during virtual-machin

drools规则出现中文验证 与jvm 默认编码(The default charset is determined during virtual-machine)的关系很重要

java.nio.charset.Charset类中有一个静态变量:

private static volatile Charset defaultCharset;

参考文档说明: http://docs.oracle.com/javase/1.5.0/docs/api/java/nio/charset/Charset.html#defaultCharset()

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.

 JVM默认的编码要么我们显示设置: -Dfile.encoding=UTF-8 ( https://stackoverflow.com/questions/6031877/jvm-property-dfile-encoding-utf8-or-utf-8

,如果不设置,会依赖当前操作系统的时区和编码。

drools中的规则如果出现中文校验,则我们必须设置编码,以防止校验规则失灵,出现此问题时,经debug到

Charset
类,看到jvm默认的编码对规则校验的失灵发生。

请自己手动验证一下吧。

drools规则出现中文验证 与jvm 默认编码(The default charset is determined during virtual-machin drools规则出现中文验证 与jvm 默认编码(The default charset is determined during virtual-machin drools规则出现中文验证 与jvm 默认编码(The default charset is determined during virtual-machin drools规则出现中文验证 与jvm 默认编码(The default charset is determined during virtual-machin drools规则出现中文验证 与jvm 默认编码(The default charset is determined during virtual-machin drools规则出现中文验证 与jvm 默认编码(The default charset is determined during virtual-machin

注意:以 linux 服务方式启动,比如
service mysql start
方式启动自己的 java  应用也会出现编码问题。

具体解决:

Java uses a native call to list the contents of a directory. The underlying C runtime relies on the locale concept to build Java  String s from the byte blob stored by the filesystem as the filename.

When you execute a Java program from a shell (either as a privileged user or an unprivileged one) it carries an environment made of variables. The variable  LANG is read to transcode the stream of bytes to a Java String, and by default on Ubuntu it's associated to the UTF-8 encoding.

Note that a process need not to be run from any shell, but looking at the code it seems that Upstart is smart enough to understand when the command in the configuration file is intended to be executed from a shell. So, assuming that the JVM is invoked through a shell, the problem is that the variable LANG is not set, so the C runtime assumes a default charset, which happens to  not be UTF-8. The solution is in the Upstart stanza:

description "List UTF-8 encoded filenames"
author "Raffaele Sgarro"
env LANG=en_US.UTF-8
script
  cd /workspace
  java -jar list.jar test > log.txt
end script

I used en_US.UTF-8 as the locale, but any UTF-8 backed one will do just as well. The sources of the test  list.jar

public static void main(String[] args) {
    for (File file : new File(args[0]).listFiles()) {
        System.out.println(file.getName());
    }
}

The directory /workspace/test contains filenames like  àààèèè and so on. Now you can move to the database part ;)


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

部落:一呼百应的力量

部落:一呼百应的力量

高汀 (Godin.S.) / 刘晖 / 中信出版社 / 2009-7 / 26.00元

部落指的是任何一群人,规模可大可小,他们因追随领导、志同道合而相互联系在一起。人类其实数百万年前就有部落的出现,随之还形成了宗教、种族、政治或甚至音乐。 互联网消除了地理隔离,降低了沟通成本并缩短了时间。博客和社交网站都有益于现有的部落扩张,并促进了网络部落的诞生——这些部落的人数从10个到1000万个不等,他们所关注的也许是iPhone,或一场政治运动,或阻止全球变暖的新方法。 那么......一起来看看 《部落:一呼百应的力量》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具