AppRootFinder { public static Path find(Class<?> clazz) { requireNonNull(clazz, "clazz"); final ProtectionDomain pd = clazz.getProtectionDomain(); if (pd == null) { throw new IllegalArgumentException(clazz + " does not have a protection domain."); } final CodeSource cs = pd.getCodeSource(); if (cs == null) { throw new IllegalArgumentException(clazz + " does not have a code source."); } final URL url = cs.getLocation(); if (url == null) { throw new IllegalArgumentException(clazz + " does not have a location."); } if (!"file".equalsIgnoreCase(url.getProtocol())) { throw new IllegalArgumentException(clazz + " is not on a file system: " + url); } File f; try { f = new File(url.toURI()); } catch (URISyntaxException ignored) { f = new File(url.getPath()); } final Path path = f.toPath(); if (!Files.isDirectory(path) && !isZip(path)) { throw new IllegalArgumentException(f + " is not a JAR, WAR or directory."); } return path; } private  AppRootFinder(); static Path findCurrent(); static Path findCurrent(int callDepth); static Path find(Class<?> clazz);  }
@Test void findWithMyClass() { final Path path = Paths.get(AppRootFinder.find(AppRootFinderTest.class).toString(), AppRootFinderTest.class.getName().replace('.', File.separatorChar) + ".class"); assertThat(path).isRegularFile(); }
@Test void findWithOthersClass() { final Path path = AppRootFinder.find(Test.class); assertThat(path.toUri().toString()).matches(".*/[^/]*junit[^/]*\\.jar$"); }