Jan 6, 2015

[Solved]HBase Java program Joda time problem - NoSuchMethodError: org.joda.time.DateTime.now()

I am writing a HBase program and use Joda time as my time type. I have a simple api call

System.out.println(DateTime.now())  which DateTime was imported as

In Eclipse the code is compiled successfully, but when I tried to execute it it poped up a strange error:  NoSuchMethodError: org.joda.time.DateTime.now()

What? DateTime.now() is a well-known API and I check the source code, it's available since Joda jar 2.0, in my maven dependency it clearly shows that my joda jar library is 2.0. Then I even manually added a latest version which is version 2.6 and clearly see that the 2.6 version is picked up.

But, the problem still exists. What's wrong with Joda?
Then I see that in my project, I also have storm-core api, which also has a joda-time dependency, so I exclude it. Now I see that
in Eclipse the DateTime also has library, and it points to jruby-complete-1.6.8.jar, then it tells me whether the problem is.


Root cause:
1. my HBase program imported external jars, one of them is jruby-complete-1.6.8.jar, which internally has a joda-time reference, but it's a very low version.

2. somehow this version got picked up and makes DateTime.now() has a problem as it's only available after 2.0

Solution
1. remove the external library jruby-complete-1.6.8.jar in Java Build Path (as the screenshot above, choose the jruby-complete-1.6.8.jar and click Remove)
2. Use maven to add jruby-complete-1.6.8 dependency


org.jruby
jruby-complete
1.6.8
        provided



3. Add Joda-time dependency and make sure it comes first as the screenshot below shows

joda-time
joda-time
2.6



DateTime.now() is a wonderful world!

If you find this blog is useful, please kindly click the ads on this page to help. Thank you very much.

No comments: