<p>In Java, you often see a META-INF folder containing some meta files. What is the purpose of this folder and what can I put there?</p>

[{'answer_id': 70221, 'author': 'aku', 'author_id': 1196, 'author_profile': 'https://Stackoverflow.com/users/1196', 'pm_score': 8, 'selected': False, 'text': '<p>From <a href="http://docs.oracle.com/javase/7/docs/technotes/guides/jar/jar.html" rel="noreferrer">the official JAR File Specification</a> (link goes to the Java 7 version, but the text hasn\'t changed since at least v1.3):</p>\n<blockquote>\n<h2>The META-INF directory</h2>\n<p>The following files/directories in the META-INF directory are recognized and interpreted by the Java 2 Platform to configure applications, extensions, class loaders and services:</p>\n<ul>\n<li><code>MANIFEST.MF</code></li>\n</ul>\n<p>The manifest file that is used to define extension and package related data.</p>\n<ul>\n<li><code>INDEX.LIST</code></li>\n</ul>\n<p>This file is generated by the new &quot;<code>-i</code>&quot; option of the jar tool, which contains location information for packages defined in an application or extension.  It is part of the JarIndex implementation and used by class loaders to speed up their class loading process.</p>\n<ul>\n<li><code>x.SF</code></li>\n</ul>\n<p>The signature file for the JAR file. \'x\' stands for the base file name.</p>\n<ul>\n<li><code>x.DSA</code></li>\n</ul>\n<p>The signature block file associated with the signature file with the same base file name. This file stores the digital signature of the corresponding signature file.</p>\n<ul>\n<li><code>services/</code></li>\n</ul>\n<p>This directory stores all the service provider configuration files.</p>\n</blockquote>\n<p>New since Java 9 implementing <a href="https://openjdk.java.net/jeps/238" rel="noreferrer">JEP 238</a> are multi-release JARs. One will see a sub folder <code>versions</code>. This is a feature which allows to package classes which are meant for different Java version in one jar.</p>\n'}, {'answer_id': 70236, 'author': 'Brian Matthews', 'author_id': 1969, 'author_profile': 'https://Stackoverflow.com/users/1969', 'pm_score': 4, 'selected': False, 'text': '<p>The META-INF folder is the home for the <a href="http://java.sun.com/developer/Books/javaprogramming/JAR/basics/manifest.html" rel="noreferrer">MANIFEST.MF</a> 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.</p>\n'}, {'answer_id': 70253, 'author': 'Daniel Spiewak', 'author_id': 9815, 'author_profile': 'https://Stackoverflow.com/users/9815', 'pm_score': 7, 'selected': True, 'text': '<p>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:</p>\n\n<pre><code>&lt;jar ...&gt;\n    &lt;manifest&gt;\n        &lt;attribute name="Main-Class" value="MyApplication"/&gt;\n    &lt;/manifest&gt;\n&lt;/jar&gt;\n</code></pre>\n\n<p>At least, I think that\'s easy...  :-)</p>\n\n<p>The point is that META-INF should be considered an internal Java <em>meta</em> 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.</p>\n'}, {'answer_id': 315588, 'author': 'user38748', 'author_id': 38748, 'author_profile': 'https://Stackoverflow.com/users/38748', 'pm_score': 5, 'selected': False, 'text': '<p>I\'ve noticed that some Java libraries have started using META-INF as a directory in which to include configuration files that should be packaged and included in the CLASSPATH along with JARs.  For example, Spring allows you to import XML Files that are on the classpath using:</p>\n\n<pre><code>&lt;import resource="classpath:/META-INF/cxf/cxf.xml" /&gt;\n&lt;import resource="classpath:/META-INF/cxf/cxf-extensions-*.xml" /&gt;\n</code></pre>\n\n<p>In this example, I\'m quoting straight out of the <a href="http://docs.huihoo.com/apache/cxf/2.0/configuration.html" rel="noreferrer">Apache CXF User Guide</a>.  On a project I worked on in which we had to allow multiple levels of configuration via Spring, we followed this convention and put our configuration files in META-INF.</p>\n\n<p>When I reflect on this decision, I don\'t know what exactly would be wrong with simply including the configuration files in a specific Java package, rather than in META-INF.  But it seems to be an emerging de facto standard; either that, or an emerging anti-pattern :-)</p>\n'}, {'answer_id': 1601585, 'author': 'sasuke', 'author_id': 99463, 'author_profile': 'https://Stackoverflow.com/users/99463', 'pm_score': 3, 'selected': False, 'text': "<p>Just to add to the information here, in case of a WAR file, the META-INF/MANIFEST.MF file provides the developer a facility to initiate a deploy time check by the container which ensures that the container can find all the classes your application depends on. This ensures that in case you missed a JAR, you don't have to wait till your application blows at runtime to realize that it's missing.</p>\n"}, {'answer_id': 1609825, 'author': 'f0ster', 'author_id': 157460, 'author_profile': 'https://Stackoverflow.com/users/157460', 'pm_score': 2, 'selected': False, 'text': '<p>If you\'re using JPA1, you might have to drop a <code>persistence.xml</code> file in there which specifies the name of a persistence-unit you might want to use.  A persistence-unit provides a convenient way of specifying a set of metadata files, and classes, and jars that contain all classes to be persisted in a grouping.</p>\n\n<pre><code>import javax.persistence.EntityManagerFactory;\nimport javax.persistence.Persistence;\n\n// ...\n\nEntityManagerFactory emf =\n      Persistence.createEntityManagerFactory(persistenceUnitName);\n</code></pre>\n\n<p>See more here:\n<a href="http://www.datanucleus.org/products/datanucleus/jpa/emf.html" rel="nofollow noreferrer">http://www.datanucleus.org/products/datanucleus/jpa/emf.html</a></p>\n'}, {'answer_id': 7721172, 'author': 'Steve Cohen', 'author_id': 811299, 'author_profile': 'https://Stackoverflow.com/users/811299', 'pm_score': 3, 'selected': False, 'text': '<p>I have been thinking about this issue recently.  There really doesn\'t seem to be any restriction on use of META-INF.  There are certain strictures, of course, about the necessity of putting the manifest there, but there don\'t appear to be any prohibitions about putting other stuff there.</p>\n\n<p>Why is this the case?</p>\n\n<p>The cxf case may be legit.  Here\'s another place where this non-standard is recommended to get around a nasty bug in JBoss-ws that prevents server-side validation against the schema of a wsdl.</p>\n\n<p><a href="http://community.jboss.org/message/570377#570377" rel="noreferrer">http://community.jboss.org/message/570377#570377</a></p>\n\n<p>But there really don\'t seem to be any standards, any thou-shalt-nots.  Usually these things are very rigorously defined, but for some reason, it seems there are no standards here.  Odd.  It seems like META-INF has become a catchall place for any needed configuration that can\'t easily be handled some other way.</p>\n'}, {'answer_id': 14340251, 'author': 'Grim', 'author_id': 843943, 'author_profile': 'https://Stackoverflow.com/users/843943', 'pm_score': 4, 'selected': False, 'text': '<p>You can also place static resources in there.</p>\n<p>In example:</p>\n<pre><code>META-INF/resources/button.jpg \n</code></pre>\n<p>and get them in web3.0-container via</p>\n<pre><code>http://localhost/myapp/button.jpg\n</code></pre>\n<p><a href="https://web.archive.org/web/20150918224750/https://blogs.oracle.com/alexismp/entry/web_inf_lib_jar_meta" rel="nofollow noreferrer">&gt; Read more</a></p>\n<p>The /META-INF/MANIFEST.MF has a special meaning:</p>\n<ol>\n<li>If you run a jar using <code>java -jar myjar.jar org.myserver.MyMainClass</code> you can move the main class definition into the jar so you can shrink the call into <code>java -jar myjar.jar</code>.</li>\n<li>You can define Metainformations to packages if you use <code>java.lang.Package.getPackage(&quot;org.myserver&quot;).getImplementationTitle()</code>.</li>\n<li>You can reference digital certificates you like to use in Applet/Webstart mode.</li>\n</ol>\n'}, {'answer_id': 27905756, 'author': 'EliuX', 'author_id': 3233398, 'author_profile': 'https://Stackoverflow.com/users/3233398', 'pm_score': 4, 'selected': False, 'text': '<h2>META-INF in Maven</h2>\n<p>In Maven the <strong>META-INF</strong> folder is understood because of the <a href="http://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html" rel="nofollow noreferrer">Standard Directory Layout</a>, which by name convention package your project resources within JARs: any directories or files placed within the <em>${basedir}/src/main/resources</em> directory are packaged into your JAR with the exact same structure starting at the base of the JAR.</p>\n<p>The Folder <em>${basedir}/src/main/resources/META-INF</em> usually contains <em>.properties</em> files while in the jar contains a generated <strong>MANIFEST.MF</strong>, <strong>pom.properties</strong>, the  <strong>pom.xml</strong>, among other files. Also frameworks like <a href="https://spring.io/blog/2013/12/19/serving-static-web-content-with-spring-boot" rel="nofollow noreferrer">Spring</a> use <code>classpath:/META-INF/resources/</code> to serve web resources.</p>\n<p>For more information see <a href="http://maven.apache.org/guides/getting-started/#How_do_I_add_resources_to_my_JAR" rel="nofollow noreferrer">How do I add resources to my Maven Project</a>.</p>\n'}, {'answer_id': 27925121, 'author': 'Tugrul', 'author_id': 1172945, 'author_profile': 'https://Stackoverflow.com/users/1172945', 'pm_score': 2, 'selected': False, 'text': '<p>All answers are correct. Meta-inf has many purposes. In addition, here is an example about using tomcat container.</p>\n\n<p>Go to \n<a href="http://tomcat.apache.org/tomcat-7.0-doc/config/context.html" rel="nofollow">Tomcat Doc</a> and check\n" <strong>Standard Implementation > copyXML</strong> " attribute.</p>\n\n<p>Description is below.</p>\n\n<blockquote>\n  <p>Set to true if you want a context XML descriptor embedded inside the application (located at /META-INF/context.xml) to be copied to the owning Host\'s xmlBase when the application is deployed. On subsequent starts, the copied context XML descriptor will be used in preference to any context XML descriptor embedded inside the application even if the descriptor embedded inside the application is more recent. The flag\'s value defaults to false. Note if the deployXML attribute of the owning Host is false or if the copyXML attribute of the owning Host is true, this attribute will have no effect.</p>\n</blockquote>\n'}, {'answer_id': 33640221, 'author': 'Prateek Joshi', 'author_id': 4281711, 'author_profile': 'https://Stackoverflow.com/users/4281711', 'pm_score': 0, 'selected': False, 'text': '<p>You have MANIFEST.MF file inside your META-INF folder. You can <strong>define optional or external dependencies</strong> that you must have access to.</p>\n\n<p><strong>Example:</strong></p>\n\n<p>Consider you have deployed your app and your container(at run time) found out that your app requires a newer version of a library which is not  inside lib folder, in that case if you have defined the optional newer version in <code>MANIFEST.MF</code> then your app will refer to dependency from there (and will not crash).</p>\n\n<p><code>Source:</code> Head First Jsp &amp; Servlet</p>\n'}, {'answer_id': 36800847, 'author': 'Readren', 'author_id': 1154271, 'author_profile': 'https://Stackoverflow.com/users/1154271', 'pm_score': 3, 'selected': False, 'text': '<p>Adding to the information here, the META-INF is a special folder which the <code>ClassLoader</code> treats differently from other folders in the jar.\nElements nested inside the META-INF folder are not mixed with the elements outside of it. </p>\n\n<p>Think of it like another root. From the <code>Enumerator&lt;URL&gt; ClassLoader#getSystemResources(String path)</code> method et al perspective:</p>\n\n<p><em>When the given path starts with "META-INF", the method searches for resources that are nested inside the META-INF folders of all the jars in the class path.</em></p>\n\n<p>When the given path doesn\'t start with "META-INF", the method searches for resources in all the other folders (outside the META-INF) of all the jars and directories in the class path.</p>\n\n<p>If you know about another folder name that the <code>getSystemResources</code> method treats specially, please comment about it.</p>\n'}, {'answer_id': 70407970, 'author': 'k_o_', 'author_id': 3351474, 'author_profile': 'https://Stackoverflow.com/users/3351474', 'pm_score': 0, 'selected': False, 'text': '<p>As an addition the META-INF folder is now also used for <a href="https://openjdk.java.net/jeps/238" rel="nofollow noreferrer">multi-release jars</a>. This is a feature which allows to package classes which are meant for different Java version in one jar, e.g. include a class for Java 11 with new features offered by Java 11 in a jar also working for Java 8, where a different class for Java 8 with less features in contained. E.g this can be useful if a newer Java version is offering enhanced, different or new API methods which would not work in earlier version due to API violations. One will see a sub folder <code>versions</code> then.</p>\n'}]
['https://Stackoverflow.com/questions/70216', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/11429/']