zoukankan      html  css  js  c++  java
  • [读后感]Java 控制台执行 Jar 传參的编码问题

    下面这版面,太让我纠结了。谁能帮调整一下,。。。

    _________________

    java -jar test.jar "一二一“


    这样传进去的 "一二一“ 假设打印出来,会是乱码,能够加个 -Dfile.encoding=UTF-8 来指定參数作为什么字符编码传入。


    java -Dfile.encoding=UTF-8 -jar test.jar "一二一“


    这样。在 java 代码内打印出来的就不是乱码了。


    另外:

    String 表示一个 UTF-16 格式的字符串。当中的增补字符代理项对 表示(有关具体信息,请參阅 Character 类中的 Unicode 字符表示形式)。

    索引值是指 char 代码单元。因此增补字符在 String 中占用两个位置。

    String 类提供处理 Unicode 代码点(即字符)和 Unicode 代码单元(即 char 值)的方法。

    getBytes :这里的字符集,是将字符串编码成指定字符集的字节序列,而 java 中的字符串一定是UTF-8的,如上官方文档摘贴。不知道理解是否正确。

    public byte[] getBytes(Charset charset)

    使用给定的 charset 将此 String 编码到 byte 序列。并将结果存储到新的 byte 数组。

    此方法总是使用此字符集的默认替代 byte 数组替代错误输入和不可映射字符序列。假设须要对编码过程进行很多其它控制。则应该使用 CharsetEncoder 类。

    參数:

    charset - 用于编码 StringCharset

    返回:

    所得 byte 数组

    从下面版本号開始:

    1.6


    String:这里的字符集,是说明字节数组是按什么字符集编码出来的。通过这个字符集指定解析方法,以构造出 UTF-8 的 String 字符串。

    public String(byte[] bytes, Charset charset)

    通过使用指定的 charset 解码指定的 byte 数组。构造一个新的 String

    String 的长度是字符集的函数。因此可能不等于 byte 数组的长度。

    此方法总是使用此字符集的默认替代字符串替代错误输入和不可映射字符序列。

    假设须要对解码过程进行很多其它控制,则应该使用 CharsetDecoder 类。

    參数:

    bytes - 要解码为字符的 byte

    charset - 要用来解码 bytescharset

    从下面版本号開始:

    1.6



    Java -Dfile.encoding=UTF-8 干掉乱码

    FEB 10TH, 2013 | COMMENTS

    參考链接:

    java 乱码问题-Dfile.encoding=UTF-8

    Java’s file.encoding property on Windows platform

    How do you open a jar file on a mac?

    more

    遭遇乱码问题的来龙去脉

    这两天写了一个 Java 程序来玩,结果又遭遇了曾经遇到过非常多次的乱码问题,具体描写叙述一下:

    在 Mac 系统里面,经常使用的 Java 程序启动方式有例如以下几种:

    1.通过 eclipse 运行 class 入口文件启动。

    2.在 Terminal 里面用 java Test.class 或 jave -jar Test.jar 启动

    3.通过 ant 运行 class 入口文件启动;

    4.直接用 ant 运行 jar 文件;

    5.用 Mac OS CoreServices 中的 Jar Launcher.app 运行 jar 文件。

    6.用 Mac OS 自带的 Jar Bundler.app 将 jar 文件包装成 app,然后运行

    运行途径还是相当地丰富,但以不同的方式来运行,从控制台中得到的程序输出也不一致

    比方说。刚刚在 eclipse 中还能正常打印出来的汉字。在打成 jar 包以后。

    双击该 jar 文件以 Jar Launcher.app 的方式来启动,打印出来的文字就成了乱码了。

    毕竟写出来的 java 程序终于还是要打成 Jar 包来使用的。总不能每次都在 eclipse 中启动吧?

    前面说过,不是第一次碰到这样的问题了,于是便想着要把这个问题给解决下。

    灵机一动之下想到一个好办法,在这些启动方式下均把 System 中的属性遍历打印出来,

    然后用 git 来做各个版本号的差异比較,有可能会套出一些蛛丝马迹~

    抱着试一试的想法实践了一把,果然发现一些猫腻,集中体如今 file.encoding 这个属性上面。

    file.encoding 属性的值是 UTF-8 时,是不存在乱码问题的,eclipse 运行就属于这样的情况。

    Jar Launcher.app 运行时,该属性的值就变成 MacRoman 了,

    上面给出的资料中有对该属性的介绍,能够用 java -D<name>=<value> Test.jar 来更改它。

    另外,仅仅有在启动 java 程序前通过传递參数来更改才有效,程序一经启动就无法再更改了。

    这样的话,也就仅仅有通过传递 jvm 參数的方式来做默认编码的变更了:

    其一,写一个带 -Dfile.encoding=UTF-8 參数的脚本文件来启动。

    其二,用 Jar Bundler.app 打包成 app,效率应该不如第一种方案。

    原理事实上都差点儿相同。都仅仅是将更改 jvm 默认编码的操作封装了起来。运行时就不用再手动键入了。

    java 乱码问题 -Dfile.encoding=UTF-8

    -Dfile.encoding 解释:

    在命令行中输入 java,在给出的提示中会出现 -D 的说明:

    -D<name>=<value> # set a system property

    -D 后面须要跟一个键值对,作用是设置一项系统属性

    -Dfile.encoding=UTF-8 来说就是设置系统属性 file.encodingUTF-8

    那么 file.encoding 什么意思?字面意思为文件编码。

    搜索 java 源代码,仅仅能找到 4 个文件里包括 file.encoding 的文件,

    也就是说,仅仅有四个文件调用了 file.encoding 这个属性。

    java.nio.charset 包中的 Charset.java 中,这段话的意思说的非常明白了。

    简单说就是默认字符集是在 java 虚拟机启动时决定的。

    依赖于 java 虚拟机所在的操作系统的区域以及字符集。

    代码中能够看到,默认字符集就是从 file.encoding 这个属性中获取的。

    Java’s file.encoding property on Windows platform

    This property is used for the default encoding in Java, all readers and writers would default to use 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 not be changed dynamically even this property can be changed. So the conclusion is that the default encoding can’t be changed 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.

    How do you open a jar file on a mac?

    You can indeed launch a jar file from the command line, with the following command: 

    java -jar yourfile.jar

    As well as this you can assign “Jar Launcher” as the default app. To use when you double-click a jar file, as follows (I don’t believe you need the developer tools installed for this): 

    Click once on the .jar file in the Finder and then from the menubar in the Finder select File -> Get Info”. Click on “Open with” and from the popup menu select “Other”. A file browser window will open. In this window, go to the /System/Library/CoreServices folder and select ‘Jar Launcher’. Then make sure the “Always Open With” checkbox is checked and then click Add. Then click the “Change all” button so that any jar file will be opened automatically. Finally, close the Info window and now when you double-click any of your jar files they should run automatically.

    Posted by yang3wei Feb 10th, 2013 Java相关




  • 相关阅读:
    题解 CF1304E 【1-Trees and Queries】
    题解 P4302 【[SCOI2003]字符串折叠】
    题解 P2070 【刷墙】
    题解 P1278 【单词游戏】
    题解 P1985 【[USACO07OPEN]翻转棋】
    题解 P2642 【双子序列最大和】
    Python面向对象之反射
    Python面向对象之进阶
    Python面向对象之多态、封装
    Python面向对象之继承
  • 原文地址:https://www.cnblogs.com/brucemengbm/p/7286134.html
Copyright © 2011-2022 走看看