如何让Java输出英语错误而不是其他语言?
我的系统是中文,并且当我的java程序中发生错误时,它可能会输出不可读的非英语错误消息:
My system is Chinese, and when there is an error occurs in my java program, it may output non-English error messages which is unreadable:
Caused by: java.io.IOException: CreateProcess error=2, 系统脮也禄碌陆指露
或:
IOException occured : Cannot run program "cmd /C tsc hello.ts":
CreateProcess error=2, ϵͳÕҲ»µ½ָ¶
是否可以让Java始终输出英语错误消息?
Is it possible to let java always output English error messages ?
答案有两个部分.
-
Java根据默认语言环境显示错误消息.有三个定义默认语言环境的系统属性:
user.language
,user.country
和user.variant
.如果您有权访问Java VM命令行,则可以使用-D
命令行开关设置这些属性.例如.这足以将Java切换为通用英语:
Java displays error messages based on the default locale. There are three system properties which define the default locale:
user.language
,user.country
anduser.variant
. If you have access to your Java VM command line you can set these properties using the-D
command line switch. E.g. this is enough to switch Java to generic English:
java -Duser.language=en com.my.Class
您可以定义JAVA_TOOL_OPTIONS
环境变量,并在此处指定选项.在这种环境中启动的所有JVM都会选择这些选项,就像在命令行中一样.
You can define a JAVA_TOOL_OPTIONS
environment variable and specify your options there. Any JVM starting in such environment will pick these options up as if they were on the command line.