Jars { public static String getJarPathForClass(Class<? extends Object> classObj) throws ClassNotFoundException, IOException { ClassLoader loader = classObj.getClassLoader(); String classFile = classObj.getName().replaceAll("\\.", "/") + ".class"; for (Enumeration<URL> itr = loader.getResources(classFile); itr.hasMoreElements();) { URL url = itr.nextElement(); if ("jar".equals(url.getProtocol())) { String toReturn = url.getPath(); if (toReturn.startsWith("file:")) { toReturn = toReturn.substring("file:".length()); } toReturn = toReturn.replaceAll("\\+", "%2B"); toReturn = URLDecoder.decode(toReturn, "UTF-8"); return toReturn.replaceAll("!.*$", ""); } } throw new ClassNotFoundException( "Unable to find containing jar for class " + classObj.getName()); } private  Jars(); static String getJarPathForClass(Class<? extends Object> classObj);  }
@Test public void testGetJarPathForClassNotFound() throws ClassNotFoundException, IOException { try { Jars.getJarPathForClass(TestJars.class); fail("ClassNotFoundException"); } catch (ClassNotFoundException ce) { assertEquals("Unable to find containing jar for class org.kiji.mapreduce.util.TestJars", ce.getMessage()); } }