Friday, August 29, 2008

Java Classpath issues

The most difficult problem to solve from a distance... But if we look closely its a piece of cake.
Many developers spend so much time in fixing this issue. Lot of frustration and anger. What they dont spend time on is finding how the classpath and class loading is done in java. Once you know it, you are done.

I herewith try to log a list of java classpath issues:

1. Junit cannot load classpath resource:
The ideal solution as I know is to put the resource along with the junit class(unless it is not reused across). Then load the resource using MyClassTest.class.getResourceAsStream("articles.xml");

This is a simple and elegant solution rather than hardcoding or putting a relative path , which are bound to fail in different environments.

No comments: