java 如何列出jar包中jar包里的所有文件和目录?
天蓬老师
天蓬老师 2017-04-18 10:52:55
[Java讨论组]

如这样一个目录/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下的文件目录如何得到?

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

全部回复(2)
巴扎黑
        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());
            }
        }
ringa_lee

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...

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号