The following code works fine: (Java7)
String s = this.getClass().getResource("").getPath(); if (s.contains("jar!")) { // we have a jar file // format: file:/location...jar!...path-in-the-jar // we only want to have location :) int excl = s.lastIndexOf("!"); s = s.substring(0, excl); s = s.substring("file:/".length()); Path workingDirPath = workingDir = Files.createTempDirectory("demo") try (JarFile jf = new JarFile(s);){ Enumeration<JarEntry> entries = jf.entries(); while (entries.hasMoreElements()) { JarEntry je = entries.nextElement(); String name = je.getName(); if (je.isDirectory()) { // directory found Path dir = workingDirPath.resolve(name); Files.createDirectory(dir); } else { Path file = workingDirPath.resolve(name); try (InputStream is = jf.getInputStream(je);) { Files.copy(is, file, StandardCopyOption.REPLACE_EXISTING); } } } } } else { // debug mode: no jar }
koppor
source share