Java 在 Eclipse 中创建 META-INF/services 文件夹

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6376736/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 05:26:51  来源:igfitidea点击:

creating META-INF/services folder in Eclipse

javaeclipsemeta-infservice-provider

提问by tom

I am trying to create an extensible app in Java and chose to use SPI. According to this tutorialI am following, I am supposed to create a specific file in META-INF/servicesin my jar, but don't know how. I know how to create jar with META-INF, but can't find a way how to create the servicesfolder and particular file in this folder. (I am using Eclipse)

我正在尝试用 Java 创建一个可扩展的应用程序并选择使用 SPI。根据我正在关注的本教程,我应该META-INF/services在我的 jar 中创建一个特定的文件,但不知道如何。我知道如何使用 .jar 创建 jar META-INF,但找不到如何services在此文件夹中创建文件夹和特定文件的方法。(我正在使用 Eclipse)

Thanks for your help.

谢谢你的帮助。

采纳答案by CoolBeans

Since the tutorial is instructing to create it manually rather than using ant or maven or other build tools then just create a folder named META-INFat the root level with a sub-folder named servicesand a file called MANIFEST.MF.

由于本教程指示手动创建它,而不是使用 ant 或 maven 或其他构建工具,因此只需在根级别创建一个名为META-INF的文件夹,其中包含一个名为services的子文件夹和一个名为MANIFEST.MF的文件。

I am not sure how you plan to execute your jar file, for now you can just put this one line in your MANIFEST.MF:

我不确定你打算如何执行你的 jar 文件,现在你可以把这一行放在你的MANIFEST.MF

Manifest-Version: 1.0

For your services folder, you need to create a file named com.example.dictionary.spi.Dictionarywith the following one line content as mentioned in the tutorial:-

对于您的服务文件夹,您需要创建一个以com.example.dictionary.spi.Dictionary教程中提到的以下一行内容命名的文件:-

com.example.dictionary.GeneralDictionary

FYI - META-INFis an internal Java metadirectory. Generally, you want to rely on your packaging tool such as ant or maven to build the content of META-INF folder rather than doing it by yourself.

仅供参考 - META-INF是一个内部 Java目录。通常,您希望依靠您的打包工具(例如 ant 或 maven)来构建 META-INF 文件夹的内容,而不是自己完成。

You can see the details on the content of META-INF folder here:-

您可以在此处查看有关 META-INF 文件夹内容的详细信息:-

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/

回答by Edwin Buck

Assuming you are building your jar file with Apache's Ant, you need to add a metainf fileset, or better yet, use a services directive like so

假设您正在使用 Apache 的 Ant构建您的jar 文件,您需要添加一个 metainf 文件集,或者更好的是,使用像这样的服务指令

<jar jarfile="pinky.jar">
  <fileset dir="build/classes"/>
  <service type="javax.script.ScriptEngineFactory"
           provider="org.acme.PinkyLanguage"/>
</jar>

If you don't use apache's Ant, then you need to add a file to your jar

如果你不使用 apache 的 Ant,那么你需要在你的 jar 中添加一个文件

jar -uf jarfile.jar META-INF/services/name.of.general.service

which contains the name of the service's implementation

其中包含服务实现的名称

com.mycorp.services.Fooservice

If you are generating the JAR file using Eclipse's "Runnable Jar file export" under the project, select "save as ANT script" and then modify the ant script to pack in the services as described above.

如果你是在项目下使用Eclipse的“Runnable Jar file export”生成JAR文件,选择“另存为ANT脚本”,然后修改ant脚本,把服务打包进去,如上所述。