Wednesday, February 13, 2013

The method X is undefined for the type Y

Classloader issues, yes, we all know about them. They are behind 99% of the "misterious" behaviors Java developers face when suddenly a well known class is apparently lacking a method that is well known to exist there.

When you face this issues try including once again the class you think is in your classpath in a place where you do know the classpath loader is looking for. Also check for duplicates. If you are in doubt which of your jars have the class (or related package) then you need to inspect inside your jars.

Just for the sake of my memory this was the cause for the below error which was happening in one of the developer's machines:
 The method getJSONArray(String) is undefined for the type JSONObject
It was happening in Talend Open Source BTW where the tLibraryLoad sometimes is forgotten by the developer as in many cases the classes "seem" to be already loaded and available when in reality it depends on the Eclipse classloader algorithms. For instance include the missing class from the component and delete the component after, it will "magically" find it. Or play with the tFileInputJSON component for a while and try to use the JSONObject class from a tJavaFlex, it will be available. Most likely you restart Talend with no loading of the class at all and the "mistery" returns :)

Using tLibraryLoad and including the json_simple-1.1.jar which comes as part of the tFileJSONInput did the trick.

No comments: