I'm using Moneta JavaMoney 1.3 to obtain an exchange rate:
CurrencyUnit base = Monetary.getCurrency(...);
CurrencyUnit term = Monetary.getCurrency(...);
ExchangeRateProvider xrates = MonetaryConversions.getExchangeRateProvider();
ExchangeRate xrate = xrates.getExchangeRate(base, term);
If I run the code within Eclipse it works fine. But after building a jar with Maven and running the jar from the command line, ExchangeRateProvider.getExchangeRate(base, term)
returns null.
I also tried specifying the ExchangeRateProvider: ExchangeRateProvider xrates = MonetaryConversions.getExchangeRateProvider("ECB");
Within Eclipse all ok, from the command line it throws an MonetoryException saying "Invalid ExchangeRateProvider (not found): ECB"
This question mentions the same issue: "Because if i'm using dependency on money-api without bp I get null as an result." I tried the accepted answer:
ConversionQuery cq = ConversionQueryBuilder.of().setBaseCurrency(Base).setTermCurrency(Term).build();
MonetaryAmount zero = FastMoney.zero(Base);
ExchangeRateProvider xrates = MonetaryConversions.getExchangeRateProvider("ECB");
CurrencyConversion cc = xrates.getCurrencyConversion(cq);
ExchangeRate xrate = cc.getExchangeRate(zero);
Again no luck: works within Eclipse, but returns null from the command line. Does this have something to do with dependencies when building the jar? I'm using the Maven shade plugin with minimizing turned off.
Or should I use moneta-bp too? Would be strange, since moneta-bp is built as backport for Java 7, and I'm using Java 8. Any other ideas?
Thanks in advance!
EDIT:
Running from Eclipse the ExchangeRateProvider is an instance of org.javamoney.moneta.spi.CompoundRateProvider
. From the command line it's an instance of org.javamoney.moneta.convert.internal.IdentityRateProvider
. Looking at IdentityRateProvider's code, it makes sense that it returns null. But how to get the CompoundRateProvider?
Instead of using the Maven shade plugin, I tried this solution and it seems to work now.