What's the Purpose of Meta-Inf

What's the purpose of META-INF?

Generally speaking, you should not put anything into META-INF yourself. Instead, you should rely upon whatever you use to package up your JAR. This is one of the areas where I think Ant really excels: specifying JAR file manifest attributes. It's very easy to say something like:

<jar ...>
<manifest>
<attribute name="Main-Class" value="MyApplication"/>
</manifest>
</jar>

At least, I think that's easy... :-)

The point is that META-INF should be considered an internal Java meta directory. Don't mess with it! Any files you want to include with your JAR should be placed in some other sub-directory or at the root of the JAR itself.

What are the purposes of files in META-INF folder of an APK file?

An android APK file is actually a jar file (java archive), it is just a plain zip file with or without compression. Jar files are used by all types of java applications, they have a specific structure - the META-INF folder contains the manifest information and other metadata about the java package carried by the jar file.

The purposes of these files are as follows:

  1. MANIFEST.MF: It contains various information used by the java run-time environment when loading the jar file, such as which is the main class to be run from the jar file, version of package, build number, creator of the package, security policies/permissions of java applets and java webstart packages, the list of file names in the jar along with their SHA1 digests, etc.
  2. CERT.SF: This contains the list of all files along with their SHA-1 digest.
  3. CERT.RSA: This contains the signed contents of the CERT.SF file along with the certificate chain of the public key used for signing the contents.

As an example, Refer to a sample apk file here.
If you download and expand this file using a file decompression program like 7zip to your desktop, you can see a sample of these files.

In the extracted directory, go to sub-directory META-INF and view the contents of the files manifest.mf and *.sf files. Here are the first few lines of these files:

File MANIFEST.SF:

Manifest-Version: 1.0
Created-By: 1.7.0_60 (Oracle Corporation)

Name: res/drawable-xxhdpi-v4/common_plus_signin_btn_text_dark_pressed.9.png
SHA1-Digest: Db3E0/I85K9Aik2yJ4X1dDP3Wq0=

Name: res/drawable-xhdpi-v4/opt_more_item_close_press.9.png
SHA1-Digest: Xxm9cr4gDbEEnnYvxRWfzcIXBEM=

Name: res/anim/accessibility_guide_translate_out.xml
SHA1-Digest: dp8PyrXMy2IBxgTz19x7DATpqz8=

The file MCTN.SF contains the digests of the file listings in MANIFEST.MF along with an empty line:

Signature-Version: 1.0
SHA1-Digest-Manifest-Main-Attributes: Sen4TNWb3NQLczkzN1idKh81Rjc=
Created-By: 1.7.0_60 (Oracle Corporation)
SHA1-Digest-Manifest: NAWTDC05HK+hfNtQ91J4AoL9F7s=

Name: res/drawable-xxhdpi-v4/common_plus_signin_btn_text_dark_pressed.9.png
SHA1-Digest: pvIZkdVTEuilCdx8UkrlY6ufPlw=

Name: res/anim/accessibility_guide_translate_out.xml
SHA1-Digest: XeX9Q2w41PRm3KiZ5p07x3CY6hc=

The file MCTN.RSA contains the signature in base64 encoding generated over file MCTN.SF.

See this reference for details on how to verify the signatures of APK packages - http://theether.net/kb/100207

role of META-INF directory

For web, the META-INF directory is typically found in Java based webapplications such as jsp (Java server pages). You can also find them in Java related webservers such as Apache-Tomcat.

The file you mentioned:

MANIFEST.MF: The manifest file that is used to define extension and package related data. So mostly it can contain human readable info like version, producer,.. but also machine readable info like module dependencies in external frameworks you may be using

Everything inside META-INF:

The files/directories in the META-INF directory are recognized and interpreted by the Java Platform to configure applications, extensions, class loaders and services.

When you don't "deviate" of the normal path (eg: you have no additional frameworks,...), you don't need certain files and thus nothing happens if you delete them. Java will create a default manifest file with every application.

What are the BOOT-INF and META-INF directories in a Spring-Boot JAR?

The META-INF folder is the home for the MANIFEST.MF file. This file contains meta data about the contents of the JAR. For example, there is an entry called Main-Class that specifies the name of the Java class with the static main() for executable JAR files.

for more details

BOOT-INF :
Spring Boot applications load from the BOOT-INF folder. Therefore the application classes must be placed in a nested BOOT-INF/classes directory. Dependencies should be placed in a nested BOOT-INF/lib directory.

more about Spring Boot packaging

Why is META-INF called META-INF?

Jar files are actually ZIP files with extra information and possibly better indexing. When packing the extra information into the ZIP file it becomes important to place it in a location which is not likely to conflict with normal ZIP file contents.

The selection of META-INF as a directory in which information about the JAR file could be stored was an attempt to use directories as a name space within the ZIP file. Basically, if it was stored in META-INF, it was Meta-data Information, or (picking amongst the possible meanings of meta), "self information" or "abstract information".

This allowed the storage of information which would impact the handling of the ZIP file within the ZIP file itself, without breaking the ZIP file packaging. This was key, as it prevented the need to create a new, portable compression format and popularize it for the purpose of Java code packaging. As people became accustomed to the utility of META-INF, new "name spaces" in the ZIP file were created for specific packaging formats, for example "WEB-INF".

The first "extensions" to the ZIP file format was the addition of a manifest, or MANIFEST.MF and the optional addition of an index list (to speed item extraction). Now such files (due to having a known extension name space) have been able to extend in many other ways, like the recent addition of the sub-directory "services" for service registration.

When you consider that ZIP files contain files and directories, it only makes sense to use a directory as a name space.

Why some resource files are put under META-INF directory

Lot of Java (EE) APIs have a contract that when you put a specific configuration/metadata file in the META-INF folder of your (or a 3rd party) JAR, then the API will automatically do the API-specific job, such as scanning classes, preloading specific classes and/or executing specific code based on the meta information.

An example provided by the standard Java SE API is the ServiceLoader. Among others, the JDBC 4.0 compatible drivers implement this. This way just dropping the JDBC driver JAR file folder will automatically load the driver class during Java application's startup/initialization without the need for any manual Class.forName("com.example.Driver") line in your code.

Further there is also the Java EE 6 provided JSF 2.0 API which scans during application's startup all JAR files for a faces-config.xml file in the META-INF folder. If present, it then will then take it as a hint to scan the entire JAR file for classes implementing the JSF specific annotations like @ManagedBean so that they get auto-instantiated and auto-configured. This saves time in potentially expensive job of scanning thousands of classes in all JARs in the entire classpath. In older versions of those API's the configuration was usually done by (verbose) XML files.

All with all, the major goal is to save the developer from code and/or configuration boilerplate. The JAR's META-INF folder is used for configuration files/hints. Some API's indeed also put static files/resources in there for own use. The META-INF folder is also part of the classpath, so the loading of those files by the classloader is easy done.

What is the correct META-INF directory location for context.xml in Tomcat?

The documentation is indeed not specific about this.

It is src/main/webapp/META-INF since the file needs to end up deployed in the web root/META-INF folder, not in WEB-INF/classes/META-INF.



Related Topics



Leave a reply



Submit