在Java中,您经常会看到包含一些元文件的meta - inf文件夹。这个文件夹的目的是什么?我可以在那里放什么?


当前回答

来自官方JAR文件规范(链接到Java 7版本,但文本至少从v1.3开始就没有改变):

The META-INF directory 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: MANIFEST.MF The manifest file that is used to define extension and package related data. INDEX.LIST This file is generated by the new "-i" 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. x.SF The signature file for the JAR file. 'x' stands for the base file name. x.DSA 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. services/ This directory stores all the service provider configuration files.

自Java 9实现JEP 238以来新增了多版本jar。一个会看到子文件夹的版本。这个特性允许将不同Java版本的类打包到一个jar中。

其他回答

你也可以在那里放置静态资源。

在示例:

META-INF/resources/button.jpg 

并通过web3.0容器获取它们

http://localhost/myapp/button.jpg

>阅读更多

/ meta - inf / MANIFEST。MF有特殊的含义:

如果你使用java -jar myjar.jar org.myserver.MyMainClass运行一个jar,你可以将主类定义移动到jar中,这样你就可以将调用缩小到java -jar myjar.jar。 如果你使用java.lang.Package.getPackage("org.myserver"). getimplementationtitle(),你可以为包定义元结构。 您可以在Applet/Webstart模式中引用您喜欢的数字证书。

你有舱单。MF文件在你的META-INF文件夹。您可以定义必须访问的可选或外部依赖项。

例子:

假设你已经部署了你的应用程序,你的容器(在运行时)发现你的应用程序需要一个不在lib文件夹内的库的新版本,在这种情况下,如果你在MANIFEST中定义了可选的新版本。MF,那么你的应用程序将从那里引用依赖(不会崩溃)。

来源:Head First Jsp & Servlet

如果您使用的是JPA1,则可能必须在其中放入一个persistence.xml文件,该文件指定您可能想要使用的持久化单元的名称。持久化单元提供了一种方便的方式,可以指定一组元数据文件、类和包含分组中要持久化的所有类的jar。

import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

// ...

EntityManagerFactory emf =
      Persistence.createEntityManagerFactory(persistenceUnitName);

点击这里查看更多信息: http://www.datanucleus.org/products/datanucleus/jpa/emf.html

来自官方JAR文件规范(链接到Java 7版本,但文本至少从v1.3开始就没有改变):

The META-INF directory 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: MANIFEST.MF The manifest file that is used to define extension and package related data. INDEX.LIST This file is generated by the new "-i" 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. x.SF The signature file for the JAR file. 'x' stands for the base file name. x.DSA 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. services/ This directory stores all the service provider configuration files.

自Java 9实现JEP 238以来新增了多版本jar。一个会看到子文件夹的版本。这个特性允许将不同Java版本的类打包到一个jar中。

META-INF文件夹是清单的主页。MF文件。该文件包含关于JAR内容的元数据。例如,有一个名为main - class的条目,它为可执行JAR文件指定了带有静态main()的Java类的名称。