Here you can find the source of findResourceInJarPackage(URL url, String packageName, String packageDirName, boolean recursive, List
private static void findResourceInJarPackage(URL url, String packageName, String packageDirName, boolean recursive, List<String> resources) throws IOException
//package com.java2s; import java.io.IOException; import java.net.JarURLConnection; import java.net.URL; import java.util.Enumeration; import java.util.List; import java.util.jar.JarEntry; import java.util.jar.JarFile; public class Main { private static void findResourceInJarPackage(URL url, String packageName, String packageDirName, boolean recursive, List<String> resources) throws IOException { JarFile jar = ((JarURLConnection) url.openConnection()).getJarFile(); Enumeration<JarEntry> entries = jar.entries(); while (entries.hasMoreElements()) { JarEntry entry = entries.nextElement(); String name = entry.getName(); if (name.charAt(0) == '/') name = name.substring(1); if (name.startsWith(packageDirName)) { int idx = name.lastIndexOf('/'); if (idx != -1) packageName = name.substring(0, idx).replace('/', '.'); if ((idx != -1) || recursive) { // it's not inside a deeper dir if (!entry.isDirectory()) resources.add(packageName + "." + name.substring(packageName.length() + 1)); }//from w ww . j ava 2 s. c o m } } } }