如何让Java输出英文错误信息而非其他语言?

14

我的系统是中文版的,当我的Java程序出现错误时,可能会输出一些非英语的错误信息,导致我无法阅读:

Caused by: java.io.IOException: CreateProcess error=2, 系统脮也禄碌陆指露
或者:
IOException occured : Cannot run program "cmd /C tsc hello.ts": 
CreateProcess error=2, ϵͳÕҲ»µ½ָ¶

是否有可能让Java始终输出英文错误信息?


你需要Java的其余部分来确认系统语言环境为中文,还是你愿意让所有内容都变成英文? - Jon Skeet
如果我能让错误信息变成英文,那就最好了。但如果不可能的话,把所有东西都变成英文也没关系。 - Freewind
其他异常也是用中文吗?还是只有在创建进程时才会出现?我想知道这个错误消息是不是来自操作系统。(顺便说一句,你的第二个错误看起来由于编码错误而被错误地显示了。) - Jon Skeet
它们是相同的消息,第一个是从“cmd”控制台复制的,第二个是从网页复制的。但两者都不正确,我不知道它们的含义。而且你是对的,它们可能来自操作系统,因为我正在尝试运行“cmd /C tsc hello.ts”。 - Freewind
@Freewind:难道它们不是恰当的中文吗?或者你不懂中文(或者你特定系统上的版本)? - Jon Skeet
1个回答

7
答案分为两部分。
  1. Java displays error messages based on the default locale. There are three system properties which define the default locale: user.language, user.country and user.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
    
  2. 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.


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接