英文:
GraalVM native-image Java i18n Locale problem
问题
我尝试使用Locale创建一个程序。我想使用GraalVM本地编译此程序,但是在本地编译后,Locale的行为不同。
我通过以下程序成功地分离出了问题:
import java.text.NumberFormat;
import java.util.Locale;
import java.util.Currency;
public class HelloWorld {
public static void main(String[] args) {
NumberFormat gbNumberFormat = NumberFormat.getCurrencyInstance(new Locale("en", "GB"));
gbNumberFormat.setCurrency(Currency.getInstance("USD"));
System.out.println(gbNumberFormat.format(1337));
NumberFormat usNumberFormat = NumberFormat.getCurrencyInstance(new Locale("en", "US"));
usNumberFormat.setCurrency(Currency.getInstance("USD"));
System.out.println(usNumberFormat.format(1337));
}
}
我可以在Java中编译此程序:
javac -d out/ HelloWorld.java
然后执行它(从out目录):
java HelloWorld
结果是:
US$1,337.00
$1,337.00
我现在可以制作本地镜像并运行它:
native-image -cp out/ HelloWorld
./helloworld
结果不同:
$1,337.00
$1,337.00
在本地模式下,我似乎遇到了国际化问题。我不太理解为什么会出现这种情况。我使用的是graalvm-ce-java11-20.0.0,但我测试了之前的版本,行为相同。
如果有需要,我随时为您提供帮助。
英文:
I try to make a program using Locale. I want compile natively this program with GraalVM but Locale don't have the same behavior after native compiling.
I success to isolate the problem with the following program :
import java.text.NumberFormat;
import java.util.Locale;
import java.util.Currency;
public class HelloWorld {
public static void main(String[] args) {
NumberFormat gbNumberFormat = NumberFormat.getCurrencyInstance(new Locale("en", "GB"));
gbNumberFormat.setCurrency(Currency.getInstance("USD"));
System.out.println(gbNumberFormat.format(1337));
NumberFormat usNumberFormat = NumberFormat.getCurrencyInstance(new Locale("en", "US"));
usNumberFormat.setCurrency(Currency.getInstance("USD"));
System.out.println(usNumberFormat.format(1337));
}
}
I can compile this program in Java :
javac -d out/ HelloWorld.java
And execute it (from out directory) :
java HelloWorld
The result is :
US$1,337.00
$1,337.00
I can now make the native image and launch it :
native-image -cp out/ HelloWorld
./helloworld
The result is not the same :
$1,337.00
$1,337.00
It is look likes I have a problem with i18n in native mode. I don't really understand why.
I am using graalvm-ce-java11-20.0.0 but I tested with previous versions with same behavior.
I am at your disposal if necessary.
专注分享java语言的经验与见解,让所有开发者获益!
评论