V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
twogoods
V2EX  ›  Java

Getting Class from jar within a jar list Class from jar within a jar( 如何列出 jar 包中 jar 包里的类)

  •  
  •   twogoods · 2017-03-30 17:31:19 +08:00 · 1712 次点击
    这是一个创建于 2794 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

    3 条回复    2017-03-31 15:14:33 +08:00
    leafin
        1
    leafin  
       2017-03-31 11:19:21 +08:00
    用 JarURLConnection ,给你写了一段,已测试

    public static void main(String[] args) throws Exception {
    try {
    String baseFile = "D:/admin.war";
    JarFile jarFile = new JarFile(baseFile);
    Enumeration<JarEntry> entries = jarFile.entries();
    while (entries.hasMoreElements()) {
    JarEntry jarEntry = entries.nextElement();
    if(jarEntry.getName().endsWith(".jar")){
    System.out.println("list jar file of " + jarEntry.getName());
    URL url = new URL("jar:file:" + baseFile + "!/" + jarEntry.getName());
    JarURLConnection urlConnection = (JarURLConnection) url.openConnection();
    JarInputStream jarInputStream = new JarInputStream(urlConnection.getInputStream());
    JarEntry entry;
    int i = 0;
    while((entry = jarInputStream.getNextJarEntry()) != null) {
    if(entry.isDirectory()){
    continue;
    }
    if(i++ >= 5){
    System.out.println();
    break;
    }
    System.out.println(entry.getName());
    }
    }
    }
    }catch (Exception e){
    e.printStackTrace();
    }
    }
    leafin
        2
    leafin  
       2017-03-31 11:19:46 +08:00
    不会排版,抱歉了
    twogoods
        3
    twogoods  
    OP
       2017-03-31 15:14:33 +08:00
    @leafin work !感谢!
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2744 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 14:39 · PVG 22:39 · LAX 06:39 · JFK 09:39
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.