`
RednaxelaFX
  • 浏览: 3019462 次
  • 性别: Icon_minigender_1
  • 来自: 海外
社区版块
存档分类
最新评论

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

    博客分类:
  • Java
阅读更多
刚才有同事说在中文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() 能获取到的那些属性。



如果用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=US
Usage: 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 u
sed
  -classpath <path>          Specify where to find user class files and annotati
on processors
  -cp <path>                 Specify where to find user class files and annotati
on 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 compil
ation is done.
  -processor <class1>[,<class2>,<class3>...]Names of the annotation processors t
o 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 for
implicitly 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上的一篇介绍
引用
The System properties are generated by a magic native method System.initProperties. There is no corresponding jar element or *.properties file on disk. file. You can temporarily add a System property with the -D option on the java.exe line. You can also use the technique to pass the value of a SET variable in as a system property.
  • 大小: 54.4 KB
分享到:
评论
3 楼 RednaxelaFX 2012-12-12  
logicmd 写道
Windows也没有Alias,还是我只是改一个编译的脚本,直接改即可。
另外楼主的图片窗口是怎么打开的啊?

图片?是说JConsole的截图么?如果JAVA_HOME在您的PATH上那直接jconsole即可;如果没在的话那 %JAVA_HOME%\bin\jconsole (%JAVA_HOME%没设的话替换为您的JDK安装路径)
2 楼 logicmd 2012-12-12  
Windows也没有Alias,还是我只是改一个编译的脚本,直接改即可。
另外楼主的图片窗口是怎么打开的啊?
1 楼 umeit 2011-08-18  
要是能在执行javac时自动加上这些参数就好了,不知道能不能实现?

相关推荐

Global site tag (gtag.js) - Google Analytics