Tag Archives: java

Find a class inside all jars

When having class path issues caused by an old version of a library in your class path, it might be convenient to find all the jars provided this class to figure out what component is loading the old version. So here is a command to do this:

for f in `find /home/jenkins -name '*.jar' -print -exec echo {} \;`; do echo "$f: "; unzip -l $f | grep 'javax.ws.rs.core.MultivaluedMap'; done

Test a method calling System.exit()

Today I was writing test for one of our command line tools, and I had this problem where the method dumping everything, which I really needed to be called since that’s the outcome┬áI was checking, also called System.exit(). I had to find a way to test this anyway. I thought about using PowerMock and mocking system but that would have been complicated because I would have to find the exact class calling the System.exit(). So here is another solution to avoid the System.exit to exit (yes that’s possible I didn’t know about that either).

The secrets lays in the SecurityManager mechanism of Java, this class not only allows you to check permissions, but also to check exit event. Therefore you can throw an exception if you want to stop the exit. Here is the code:
Continue reading