![](/img/trans.png)
[英]How would I get a string from Resources, if I only know which resource at runtime?
[英]How to know at runtime the jar in which a resource exists?
我的jar文件中有一個圖像,並且我還在使用另一個也有圖像的jar文件。 我如何在運行時知道存在特定映像(資源)的jar文件。
上課我可以用
getClass().getProtectionDomain().getCodeSource().getLocation()
但就我而言,這是一個形象!
資源的URL格式為jar:url-of-jar-file!entry-path
。 忽略錯誤檢查:
URL resURL = getClassLoader().getResource(resName);
// ==> resURL = jar:ORIGINAL_URL!resName
String path = resURL.getPath();
// ==> path = ORIGINAL_URL!resName
URL jarURL = new URL(path.substring(0, path.indexOf('!'));
// ==> jarURL = ORIGINAL_URL
如果您有能力創建連接,則可以選擇執行以下操作:
JarURLConnection conn = (JarURLConnection) resUrl.openConnection();
URL jarURL = conn.getJarFileURL();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.