Skip to content

Instantly share code, notes, and snippets.

@learning-frog
Created March 7, 2018 09:50
Show Gist options
  • Select an option

  • Save learning-frog/f7fd5b56dd13812ce4737c09c4acfe4c to your computer and use it in GitHub Desktop.

Select an option

Save learning-frog/f7fd5b56dd13812ce4737c09c4acfe4c to your computer and use it in GitHub Desktop.
Check which jar is used at runTime
Class theClass = Cluster.class;
// Find the path of the compiled class
String classPath = theClass.getResource(theClass.getSimpleName() + ".class").toString();
System.out.println("Class: " + classPath);
// Find the path of the lib which includes the class
String libPath = classPath.substring(0, classPath.lastIndexOf("!"));
System.out.println("Lib: " + libPath);
// Find the path of the file inside the lib jar
String filePath = libPath + "!/META-INF/MANIFEST.MF";
System.out.println("File: " + filePath);
// We look at the manifest file, getting two attributes out of it
Manifest manifest = new Manifest(new URL(filePath).openStream());
Attributes attr = manifest.getMainAttributes();
System.out.println("Manifest-Version: " + attr.getValue("Manifest-Version"));
System.out.println("Implementation-Version: " + attr.getValue("Implementation-Version"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment