jar文件操做--獲取jar包內的文件夾

Finally, I found the solution:java

final String path = "sample/folder"; final File jarFile = new File(getClass().getProtectionDomain().getCodeSource().getLocation().getPath()); if(jarFile.isFile()) { // Run with JAR file final JarFile jar = new JarFile(jarFile); final Enumeration<JarEntry> entries = jar.entries(); //gives ALL entries in jar while(entries.hasMoreElements()) { final String name = entries.nextElement().getName(); if (name.startsWith(path + "/")) { //filter according to the path System.out.println(name); } } jar.close(); } else { // Run with IDE final URL url = Launcher.class.getResource("/" + path); if (url != null) { try { final File apps = new File(url.toURI()); for (File app : apps.listFiles()) { System.out.println(app); } } catch (URISyntaxException ex) { // never happens } } }

The second block just work when you run the application on IDE (not with jar file), You can remove it if you don't like that.app

share improve this answer
 
 
Updated the correct answer. Although this is not what I would like to do, especially in a code with 34000+ source files... But it seems to be the only solution. Thank you. –  Mostafa Zeinali  Oct 8 at 11:03
相關文章
相關標籤/搜索