
如这样一个目录/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 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(); } } |
2 leafin 2017-03-31 11:19:46 +08:00 不会排版,抱歉了 |