Here you can find the source of getResource(Class> cl, String path)
public static URL getResource(Class<?> cl, String path) throws IOException
//package com.java2s; //License from project: LGPL import java.io.IOException; import java.net.URL; public class Main { public static URL getResource(Class<?> cl, String path) throws IOException { String clp = cl.getName().replace('.', '/') + ".class"; URL clu = cl.getClassLoader().getResource(clp); String s = clu.toString(); if (s.endsWith(clp)) return new URL(s.substring(0, s.length() - clp.length()) + path); if (s.startsWith("jar:")) { String[] ss = s.split("!"); return new URL(ss[1] + "!/" + path); }//from w ww . jav a 2 s . c om return null; } }