RunningFromJar { public static String getJarFilePathOrNull(Class<?> clazz) { URL url = clazz.getResource("/" + clazz.getName().replace('.', '/').trim() + ".class"); String urlString = (url != null) ? url.toString() : ""; String jarPath = null; if (urlString.contains("jar:file:")) { jarPath = urlString.replace("jar:file:", ""); jarPath = jarPath.substring(0, jarPath.indexOf(".jar!") + 4); } return jarPath; }  static String getVersionNumberOrNull(); static String getJarFilePathOrNull(Class<?> clazz);  }
@Test public void testGetJarFilePathOrNull() { URL url = RunningFromJarTest.class.getResource("/" + RunningFromJarTest.class.getName().replace('.', '/').trim() + ".class"); String urlString = (url != null) ? url.toString() : ""; assertThat(urlString, startsWith("file:/")); assertThat(urlString, containsString(RunningFromJarTest.class.getSimpleName())); assertThat(RunningFromJar.getJarFilePathOrNull(RunningFromJarTest.class), nullValue()); }