Using Java to read a .jar manifest file

ぃ、小莉子 提交于 2019-12-04 12:37:11

问题


so I am trying to see if a .jar is valid or not, by checking some values in the mainfest file. What is the best way to read and parse the file using java? I thought of using this command to extract the file

jar -xvf anyjar.jar META-INF/MANIFEST.MF

But can I just do something like:

File manifest = Command.exec("jar -xvf anyjar.jar META-INF/MAINFEST.MF");

Then use some buffered reader or something to parse the lines of the file?

Thanks for any help...


回答1:


The problem with using the jar tool is that it requires the full JDK to be installed. Many users of Java will only have the JRE installed, which does not include jar.

Also, jar would have to be on the user's PATH.

So instead I would recommend using the proper API, like this:

Manifest m = new JarFile("anyjar.jar").getManifest();

That should actually be easier!




回答2:


The Package class at java.lang.Package has methods to do what you want. Here is an easy way to get manifest contents using your java code:

String t = this.getClass().getPackage().getImplementationTitle();
String v = this.getClass().getPackage().getImplementationVersion();

I put this into a static method in a shared utility class.The method accepts a class handle object as a parameter. This way, any class in our system can get their own manifest information when they need it. Obviously the method could be easily modified to return an array or hashmap of values.

call the method:

    String ver = GeneralUtils.checkImplVersion(this);

the method in a file called GeneralUtils.java:

public static String checkImplVersion(Object classHandle)
{
   String v = classHandle.getClass().getPackage().getImplementationVersion();
   return v;
}

And to get manifest fields-values other than those you can get via the Package class (e.g. your own Build-Date), you get the Main Attibutes and work through those, asking for the particular one you want. This following code is a slight mod from a similar question I found, probably here on SO. (I would like to credit it but I lost it - sorry.)

put this in a try-catch block, passing in a classHandle (the "this" or MyClass.class ) to the method. "classHandle" is of type Class:

  String buildDateToReturn = null;
  try
  {
     String path = classHandle.getProtectionDomain().getCodeSource().getLocation().getPath();
     JarFile jar = new JarFile(path);  // or can give a File handle
     Manifest mf = jar.getManifest();
     final Attributes mattr = mf.getMainAttributes();
     LOGGER.trace(" --- getBuildDate: "
           +"\n\t path:     "+ path
           +"\n\t jar:      "+ jar.getName()
           +"\n\t manifest: "+ mf.getClass().getSimpleName()
           );

     for (Object key : mattr.keySet()) 
     {
        String val = mattr.getValue((Name)key);
        if (key != null && (key.toString()).contains("Build-Date"))
        {
           buildDateToReturn = val;
        }
     }
  }
  catch (IOException e)
  { ... }

  return buildDateToReturn;



回答3:


The easiest way is to use JarURLConnection class :

String className = getClass().getSimpleName() + ".class";
String classPath = getClass().getResource(className).toString();
if (!classPath.startsWith("jar")) {
    return DEFAULT_PROPERTY_VALUE;
}

URL url = new URL(classPath);
JarURLConnection jarConnection = (JarURLConnection) url.openConnection();
Manifest manifest = jarConnection.getManifest();
Attributes attributes = manifest.getMainAttributes();
return attributes.getValue(PROPERTY_NAME);

Because in some cases ...class.getProtectionDomain().getCodeSource().getLocation(); gives path with vfs:/, so this should be handled additionally.

Or, with ProtectionDomain:

File file = new File(getClass().getProtectionDomain().getCodeSource().getLocation().toURI());
if (file.isFile()) {
    JarFile jarFile = new JarFile(file);
    Manifest manifest = jarFile.getManifest();
    Attributes attributes = manifest.getMainAttributes();
    return attributes.getValue(PROPERTY_NAME);
}


来源:https://stackoverflow.com/questions/19347052/using-java-to-read-a-jar-manifest-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!