Skip to content

Instantly share code, notes, and snippets.

@ducnhse130201
Created July 1, 2019 10:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ducnhse130201/b9bbeb4ddf707ffc7b3f1f51038f8fd0 to your computer and use it in GitHub Desktop.
Save ducnhse130201/b9bbeb4ddf707ffc7b3f1f51038f8fd0 to your computer and use it in GitHub Desktop.
get_jars.java (dump classpath from manifest)
package com.company;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.jar.Attributes;
import java.util.jar.Manifest;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Main {
public static void main(String[] args) throws IOException {
Stream<Path> walk = Files.walk(Paths.get("/home/peterjson/Desktop/libs/"));
List<String> result = walk.map(x -> x.toString()).filter(f -> f.endsWith(".jar")).collect(Collectors.toList());
for (String path : result) {
try{
URL entryUrl = new URL("jar:file:" + path + "!/META-INF/MANIFEST.MF");
InputStream is = entryUrl.openStream();
Manifest manifest = new Manifest(is);
Attributes mainAttributes = manifest.getMainAttributes();
String classpath = mainAttributes.getValue("Class-Path");
if(classpath != null) {
classpath = classpath.replace("../", "").replace("..\\", "").replace("./", "").replace(".\\", "");
if(classpath.contains(" ")){
String [] arr = classpath.split(" ");
for(String p: arr){
if(p.contains("/"))
System.out.println(p.substring(p.lastIndexOf("/")+1));
else if(p.contains("\\"))
System.out.println(p.substring(p.lastIndexOf("\\")+1));
else
System.out.println(p);
}
}
else{
if(classpath.contains("/"))
System.out.println(classpath.substring(classpath.lastIndexOf("/")+1));
else if(classpath.contains("/"))
System.out.println(classpath.substring(classpath.lastIndexOf("\\")+1));
else
System.out.println(classpath);
}
}
} catch (MalformedURLException ex) {
ex.printStackTrace();
} catch (FileNotFoundException ex) {
continue;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment