RednaxelaFX 发表于 2013-1-24 21:02:25

让javac在中文系统上输出英文的信息

刚才有同事说在中文Windows上用maven-compiler-plugin出错了,因为用javac编译时输出的警告信息里有中文,而maven-compiler-plugin插件只能识别英文结果就错了。所以想找办法让javac输出英文信息。

我虽然没在用maven-compiler-plugin遇到这个问题,不过之前也一直想让javac输出英文信息。这次顺便把找方法的过程记下。

javac本身是用Java写的,选取字符串资源时会受到底下的JVM的语言状态影响。那么问题就转变为如何让JVM用英文环境来执行(同时还得继续使用GBK编码作为默认编码,不然有些文件的读取就不对了)。
其实问题不在系统是不是中文的,而在“国家”是不是中国。
通过设定Java的系统属性 user.country ,就可以任意指定JVM“认为”的当前所在国家;用于指定文件的默认编码的系统属性则是 file.encoding ,跟国家是分开的。
这里所说的“系统属性”是指在Java程序中通过 java.lang.System.getProperty() 能获取到的那些属性。

http://dl.iteye.com/upload/attachment/267145/5e1da1f4-637e-3ca4-b5d1-50017d7f42e7.jpg

如果用java/java.exe来启动JVM,那么在命令行上使用 -Duser.country=US 就可以把国家指定为美国。用javac/javac.exe来启动javac编译器则需要再多加个-J在前面,也就是 -J-Duser.country=US 。

例子如下:

Microsoft Windows XP [版本 5.1.2600](C) 版权所有 1985-2001 Microsoft Corp.D:\>javac用法:javac <选项> <源文件>其中,可能的选项包括:-g                         生成所有调试信息-g:none                  不生成任何调试信息-g:{lines,vars,source}   只生成某些调试信息-nowarn                  不生成任何警告-verbose                   输出有关编译器正在执行的操作的消息-deprecation               输出使用已过时的 API 的源位置-classpath <路径>            指定查找用户类文件和注释处理程序的位置-cp <路径>                   指定查找用户类文件和注释处理程序的位置-sourcepath <路径>         指定查找输入源文件的位置-bootclasspath <路径>      覆盖引导类文件的位置-extdirs <目录>            覆盖安装的扩展目录的位置-endorseddirs <目录>         覆盖签名的标准路径的位置-proc:{none,only}          控制是否执行注释处理和/或编译。-processor <class1>[,<class2>,<class3>...]要运行的注释处理程序的名称;绕过默认的搜索进程-processorpath <路径>      指定查找注释处理程序的位置-d <目录>                  指定存放生成的类文件的位置-s <目录>                  指定存放生成的源文件的位置-implicit:{none,class}   指定是否为隐式引用文件生成类文件-encoding <编码>             指定源文件使用的字符编码-source <版本>               提供与指定版本的源兼容性-target <版本>               生成特定 VM 版本的类文件-version                   版本信息-help                      输出标准选项的提要-Akey[=value]            传递给注释处理程序的选项-X                         输出非标准选项的提要-J<标志>                     直接将 <标志> 传递给运行时系统D:\>javac -J-Duser.country=USUsage: javac <options> <source files>where possible options include:-g                         Generate all debugging info-g:none                  Generate no debugging info-g:{lines,vars,source}   Generate only some debugging info-nowarn                  Generate no warnings-verbose                   Output messages about what the compiler is doing-deprecation               Output source locations where deprecated APIs are used-classpath <path>          Specify where to find user class files and annotation processors-cp <path>               Specify where to find user class files and annotation processors-sourcepath <path>         Specify where to find input source files-bootclasspath <path>      Override location of bootstrap class files-extdirs <dirs>            Override location of installed extensions-endorseddirs <dirs>       Override location of endorsed standards path-proc:{none,only}          Control whether annotation processing and/or compilation is done.-processor <class1>[,<class2>,<class3>...]Names of the annotation processors to run; bypasses default discovery process-processorpath <path>      Specify where to find annotation processors-d <directory>             Specify where to place generated class files-s <directory>             Specify where to place generated source files-implicit:{none,class}   Specify whether or not to generate class files forimplicitly referenced files-encoding <encoding>       Specify character encoding used by source files-source <release>          Provide source compatibility with specified release-target <release>          Generate class files for specific VM version-version                   Version information-help                      Print a synopsis of standard options-Akey[=value]            Options to pass to annotation processors-X                         Print a synopsis of nonstandard options-J<flag>                   Pass <flag> directly to the runtime system

可以参考一下Mindprod上的一篇介绍
<div class="quote_title">引用
页: [1]
查看完整版本: 让javac在中文系统上输出英文的信息