JAVA擷取CLASSPATH路徑的方法詳解_java

來源:互聯網
上載者:User

ClassLoader 提供了兩個方法用於從裝載的類路徑中取得資源:

  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.getClass().getResourceAsStream(String name) ;擷取流,靜態化方法中則使用ClassLoader.getSystemResourceAsStream (String name) ; 。

      下面是一些得到classpath和當前類的絕對路徑的一些方法。你可能需要使用其中的一些方法來得到你需要的資源的絕對路徑。

1.this.getClass().getResource("")

得到的是當前類class檔案的URI目錄。不包括自己!
如:file:/D:/workspace/jbpmtest3/bin/com/test/

2.this.getClass().getResource("/")

得到的是當前的classpath的絕對URI路徑 。
如:file:/D:/workspace/jbpmtest3/bin/

3.this.getClass() .getClassLoader().getResource("")

得到的也是當前ClassPath的絕對URI路徑 。
如:file:/D:/workspace/jbpmtest3/bin/

4.ClassLoader.getSystemResource("")

得到的也是當前ClassPath的絕對URI路徑 。
如:file:/D:/workspace/jbpmtest3/bin/

5.Thread.currentThread().getContextClassLoader ().getResource("")

得到的也是當前ClassPath的絕對URI路徑 。
如:file:/D:/workspace/jbpmtest3/bin/

6.ServletActionContext.getServletContext().getRealPath(“/”)

Web應用程式 中,得到Web應用程式的根目錄的絕對路徑。這樣,我們只需要提供相對於Web應用程式根目錄的路徑,就可以構建出定位資源的絕對路徑。

如:file:/D:/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/WebProject

注意點:

1.盡量不要使用相對於System.getProperty("user.dir")目前使用者目錄的相對路徑。這是一顆定時炸 彈,隨時可能要你的命。

2.盡量使用URI形式的絕對路徑資源。它可以很容易的轉變為URI,URL,File對象。

3.盡量使用相對classpath的相對路徑。不要使用絕對路徑。使用上面ClassLoaderUtil類的public static URL getExtendResource(String relativePath)方法已經能夠使用相對於classpath的相對路徑定位所有位置的資源。

4.絕對不要使用硬式編碼絕對路徑。因為,我們完全可以使用ClassLoader類的getResource("")方法得到當前classpath的絕對路徑。如果你一定要指定一個絕對路徑,那麼使用設定檔,也比寫入程式碼要好得多!

獲得CLASSPATH之外路徑的方法:

URL base = this.getClass().getResource(""); //先獲得本類的所在位置,如/home/popeye/testjava/build/classes/net/ 
      String path = new File(base.getFile(), "……/……/……/"+name).getCanonicalPath(); //就可以得到/home/popeye/testjava/name

另外,如果從ANT啟動程式,this.getClass().getResource("")取出來的比較怪,直接用JAVA命令列調試就可成功。

下面在看下Java 中擷取類路徑 classpath 的方法

廢話補多說了,直接給大家貼關鍵代碼了,具體代碼如下所示:

<span style="font-size: 18px;">System.out.println("++++++++++++++++++++++++");String path = System.getProperty("java.class.path");String path2 = FreeMarkerWriter.class.getProtectionDomain().getCodeSource().getLocation().getPath();String path3 = FreeMarkerWriter.class.getProtectionDomain().getCodeSource().getLocation().getFile();String path4 = Thread.currentThread().getContextClassLoader().getResource("").getPath();System.out.println("path 1 = " + path);System.out.println("path 2 = " + path2);System.out.println("path 3 = " + path3);System.out.println("path 4 = " + path4);System.out.println("++++++++++++++++++++++++");</span>

以上所述是小編給大家介紹的Java 中擷取類路徑 classpath 的方法簡單代碼,希望對大家有所協助,如果大家有任何疑問歡迎給我留言,小編會及時回複大家的!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.