Java??Jar???????????
???????????? ???????[ 2013/2/1 9:59:12 ] ????????
????????ClassLoader???Jar????????
/**
* ????????Jar???ж??????? Jar???????????????File???????????Stream?????????
*
* @author lihzh
* @throws URISyntaxException
* @throws IOException
* @data 2012-4-11 ????11:07:58
*/
@Test
public void testGetFileFromJarInClassPath() throws URISyntaxException??
IOException {
Enumeration<URL> urls = this.getClass().getClassLoader().getResources("conf/test.properties");
URL url = this.getClass().getClassLoader().getResource("conf/test.properties");
Assert.assertTrue(urls.hasMoreElements());
Assert.assertNotNull(url);
// ????????????÷????·??????????????????“/” ??????????????????????????????????????????
// ?????Class??getResource???????????????resolveName????????????????·???????????ClassLoader?????
// getResource??getResources???????????????????????
URL clzURL = this.getClass().getResource("/conf/test.properties");
URL nullURL = this.getClass().getResource("conf/test.properties");
Assert.assertNotNull(clzURL);
Assert.assertNull(nullURL);
URL thisClzURL = this.getClass().getResource("");
Assert.assertNotNull(thisClzURL);
// ?????????????
InputStream is = this.getClass().getResourceAsStream("/conf/test.properties");
Properties props = new Properties();
props.load(is);
Assert.assertTrue(props.containsKey("test.key"));
Assert.assertEquals("thisIsValue"?? props.getProperty("test.key"));
}
???????Jar???·???μ????????
/**
* ??ClassPath?е?Jar????????????μ????????
*
* @author lihzh
* @throws IOException
* @data 2012-4-13 ????10:22:24
*/
@Test
public void testGetFilesFromJarInClassPathWithDirPath() throws IOException {
String dirPath = "conf/";
URL url = this.getClass().getClassLoader().getResource(dirPath);
Assert.assertNotNull(url);
String urlStr = url.toString();
// ???!/ ????????????
String jarPath = urlStr.substring(0?? urlStr.indexOf("!/") + 2);
URL jarURL = new URL(jarPath);
JarURLConnection jarCon = (JarURLConnection) jarURL.openConnection();
JarFile jarFile = jarCon.getJarFile();
Enumeration<JarEntry> jarEntrys = jarFile.entries();
Assert.assertTrue(jarEntrys.hasMoreElements());
Properties props = new Properties();
while (jarEntrys.hasMoreElements()) {
JarEntry entry = jarEntrys.nextElement();
// ?????ж?·???????????????Spring??Ant-Style?????·???????????????
String name = entry.getName();
if (name.startsWith(dirPath) && !entry.isDirectory()) {
// ?????????????
InputStream is = this.getClass().getClassLoader().getResourceAsStream(name);
Assert.assertNotNull(is);
props.load(is);
}
}
Assert.assertTrue(props.containsKey("test.key"));
Assert.assertEquals("thisIsValue"?? props.getProperty("test.key"));
Assert.assertTrue(props.containsKey("test.key.two"));
Assert.assertEquals("thisIsAnotherValue"?? props.getProperty("test.key.two"));
}
???????????ClassPath?μ?Jar????????????????????????JarFile??????ɡ?·???????·???????????????url.getConnection???????????????????
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11