java 如何列出jar包中jar包里的所有文件和目录?
问题描述
如这样一个目录/Users/xxx/IdeaProjects/abc/web/target/web.jar!/BOOT-INF/lib/rest-0.0.1.jar!/com/tg/tiny/Users/xxx/IdeaProjects/abc/web/target/web.jar这个jar包下的文件目录可以这样得到
JarFile localJarFile = new JarFile(new File('/Users/xxx/IdeaProjects/abc/web/target/web.jar'));Enumeration<JarEntry> entries = localJarFile.entries();while (entries.hasMoreElements()) { JarEntry jarEntry = entries.nextElement(); System.out.println(jarEntry.getName());}
那么这个web.jar里的rest-0.0.1.jar下的文件目录如何得到?
问题解答
回答1:URL url = new URL('jar', null, 0, 'file:/Users/xxx/IdeaProjects/web/target/web.jar!/BOOT-INF/lib/rest.jar');URLConnection con = url.openConnection();if (con instanceof JarURLConnection) { JarURLConnection result = (JarURLConnection) con; JarInputStream jarInputStream = new JarInputStream(result.getInputStream()); JarEntry entry; while ((entry = jarInputStream.getNextJarEntry()) != null) {System.out.println(entry.getName()); }}回答2:
jar包是个文件。不是目录。需要通过classloader的getResourceAsStream()。
package edu.hxraid; import java.io.*; public class Resource { public void getResource() throws IOException{ //返回读取指定资源的输入流 InputStream is=this.getClass().getResourceAsStream('/resource/res.txt'); BufferedReader br=new BufferedReader(new InputStreamReader(is)); String s=''; while((s=br.readLine())!=null) System.out.println(s); } }
详细使用:http://www.myexception.cn/pro...
相关文章:
1. javascript - 求解答:实例对象调用constructor,此时constructor内的this的指向?2. Windows系统能否利用Docker使用Ubuntu吗?Ubuntu能使用本机的显卡吗?3. python的MySQLdb库中的executemany方法如何改变默认加上的单引号?4. 请教,关于python字典,合并相同值的键的实现方法5. css3中translate(-50%,-50%)对 transform-origin的奇葩影响?6. mysql5.7就没有官方性质的详细配置文件吗?求大神告知7. javascript - 如何将 windows 下编辑器中的 CRLF 替换为 LF?8. css - chrome浏览器input记录上次cookie信息后,有个黄色背景~如何去除!9. android - 京东移动端网页和其app加载的url所做的呈现不应该是完全一样的吗?10. html5 - 在一个页面中 初始了两个swiper 不知道哪里错了 一直不对

网公网安备