sharakan sharakan - 1 year ago 112
Java Question

How do I add a new Currency to java.util.Currency for an existing country code in Java 7?

For example, the Chinese currency has the ISO 4217 code CNY. Since free global trading in that currency is restricted though, there's a second 'offshore' currency equivalent, called CNH. Wikipedia has a bit of summary of this all.

In Java 7, there's a method for updating the set of three letter ISO 4217 codes that the JVM ships with. However, it can't be used to add a separate currency code to an existing country code: it would replace CNY with CNH, which is no good for my purposes.

How do I add CNH (which is not in the ISO 4217 list) to the set of available currencies in Java 7, without overwriting CNY? Put another way, how can I get multiple currency codes for a single country?

Note that this question was asked and answered for Java 6. But the strategy of replacing java.util.CurrencyData doesn't work because that file no longer exists.

Answer Source

The key here is in a change that's part of Java 7 to allow updating of the list of currencies without rebuilding rt.jar by replacing a file called Using this approach, rather than the override approach, allows you to add new Currency codes without affecting other ones from the same country.

What's left unsaid there is how to go about actually building a new This file is generated from a file called, which can be found in the OpenJDK source code in java/util.

What I did was copy the found in the OpenJDK source (openjdk\jdk\src\share\classes\java\util), and changed the line:




Then I grabbed the file in the source distribution at openjdk\jdk\make\tools\src\build\tools\generatecurrencydata. This utility takes input from System.In in the same format as, and turns it in to a file. I made a slight change so that it used a FileInputStream instead of System.In:



currencyData.load(new FileInputStream(fileName));

Run that on your edited file and, after putting the original .data file somewhere safe, place the resulting file in to your JRE\lib directory, and you can now run code that uses Currency.getInstance("CNH").

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download