ClassLoader提供了兩個方法用於從裝載的類路徑中取得資源:java
public URL getResource(String name);
public InputStream getResourceAsStream(String name);測試
這裏name是資源的類路徑,它是相對與「/」根路徑下的位置。getResource獲得的是一個URL對象來定位資源,而getResourceAsStream取得該資源輸入流的引用保證程序能夠從正確的位置抽取數據。
但 是真正使用的不是ClassLoader的這兩個方法,而是Class的 getResource和getResourceAsStream方法,由於Class對象能夠從你的類獲得(如YourClass.class或 YourClass.getClass()),而ClassLoader則須要再調用一次YourClass.getClassLoader()方法,不 過根據JDK文檔的說法,Class對象的這兩個方法實際上是「委託」(delegate)給裝載它的ClassLoader來作的,因此只須要使用 Class對象的這兩個方法就能夠了。this
所以,直接調用 this.getClass().getResourceAsStream(String name);獲取流,靜態化方法中則使用ClassLoader.getSystemResourceAsStream(String name); 。編碼
下面是一些獲得classpath和當前類的絕對路徑的一些方法。你可能須要使用其中的一些方法來獲得你須要的資源的絕對路徑。url
1.this.getClass().getResource("")
獲得的是當前類class文件的URI目錄。不包括本身!
如:file:/D:/workspace/jbpmtest3/bin/com/test/spa
2.this.getClass().getResource("/")
獲得的是當前的classpath的絕對URI路徑。
如:file:/D:/workspace/jbpmtest3/bin/.net
3.this.getClass().getClassLoader().getResource("")
獲得的也是當前ClassPath的絕對URI路徑。
如:file:/D:/workspace/jbpmtest3/bin/對象
4.ClassLoader.getSystemResource("")
獲得的也是當前ClassPath的絕對URI路徑。
如:file:/D:/workspace/jbpmtest3/bin/ssl
5.Thread.currentThread().getContextClassLoader().getResource("")
獲得的也是當前ClassPath的絕對URI路徑。
如:file:/D:/workspace/jbpmtest3/bin/資源
注意點:
1.儘可能不要使用相對於System.getProperty("user.dir")當前用戶目錄的相對路徑。這是一顆定時炸彈,隨時可能要你的命。
2.儘可能使用URI形式的絕對路徑資源。它能夠很容易的轉變爲URI,URL,File對象。
3.儘可能使用相對classpath的相對路徑。不要使用絕對路徑。使用上面ClassLoaderUtil類的public static URL getExtendResource(String relativePath)方法已經可以使用相對於classpath的相對路徑定位全部位置的資源。
4.絕對不要使用硬編碼的絕對路徑。由於,咱們徹底能夠使用ClassLoader類的getResource("")方法獲得當前classpath的絕對路徑。若是你必定要指定一個絕對路徑,那麼使用配置文件,也比硬編碼要好得多!
package org.voovan.test.tools.compiler.classloader; import junit.framework.TestCase; import java.net.URL; /** * @USER echo * @TIME 2017-07-24 9:08 * @DESC 類加載器測試 **/ public class PathClassLoaderUnit extends TestCase { public void testRun() throws ClassNotFoundException { String classPath = "F:/IdeaProjects/Voovan/Common/target/test-classes/111"; //file:/F:/IdeaProjects/Voovan/Common/target/test-classes/org/voovan/test/tools/compiler/classloader/ URL url1 = this.getClass().getResource(""); //file:/F:/IdeaProjects/Voovan/Common/target/test-classes/ URL url2 = this.getClass().getResource("/"); classPath = url2.toString().substring(6); //file:/F:/IdeaProjects/Voovan/Common/target/test-classes/org/voovan/test/tools/compiler/classloader/ URL url3 = this.getClass().getClassLoader().getResource(""); URL url4 = this.getClass().getClassLoader().getResource("/"); // null URL url5 = ClassLoader.getSystemResource(""); //file:/F:/IdeaProjects/Voovan/Common/target/test-classes/org/voovan/test/tools/compiler/classloader/ URL url6 = Thread.currentThread().getContextClassLoader().getResource(""); //file:/F:/IdeaProjects/Voovan/Common/target/test-classes/org/voovan/test/tools/compiler/classloader/ PathClassLoader pathClassLoader = new PathClassLoader(classPath); pathClassLoader.packageName ="org.voovan"; pathClassLoader.findClass("org.voovan.Global"); } }
package org.voovan.test.tools.compiler.classloader; import java.io.*; /** * @USER echo * @TIME 2017-07-24 8:47 * @DESC 類加載器實現 **/ public class PathClassLoader extends ClassLoader { private String classPath; String packageName; public PathClassLoader(String classPath) { this.classPath = classPath; } protected Class<?> findClass(String name) throws ClassNotFoundException { if(name.startsWith(packageName)) { byte[] classByte = getData(name); if(classByte == null) { throw new ClassNotFoundException(); } else { return defineClass(name, classByte, 0, classByte.length); } } return super.loadClass(name); } private byte[] getData(String className) { String path = classPath + className.replace('.',File.separatorChar) + ".class"; try { InputStream in = new FileInputStream(path); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); byte [] buffer = new byte[2048]; int num = 0; if((num = in.read(buffer)) != -1) { byteArrayOutputStream.write(buffer,0,num); } return byteArrayOutputStream.toByteArray(); } catch (IOException e1){ e1.printStackTrace(); } return null; } }