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

发布于 2024-11-15 22:46:55 字数 330 浏览 2 评论 0原文

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

感谢您的帮助。

I am trying to create an extensible app in Java and chose to use SPI. According to this tutorial I am following, I am supposed to create a specific file in META-INF/services in 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 services folder and particular file in this folder. (I am using Eclipse)

Thanks for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

指尖上的星空 2024-11-22 22:46:55

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

我不确定您打算如何执行您的 jar 文件,现在您只需将这一行放入您的 MANIFEST.MF 中:

Manifest-Version: 1.0

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

com.example.dictionary.GeneralDictionary

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

您可以查看 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/

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-INF at the root level with a sub-folder named services and a file called 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:

Manifest-Version: 1.0

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

com.example.dictionary.GeneralDictionary

FYI - META-INF is an internal Java meta directory. 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.

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

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/
檐上三寸雪 2024-11-22 22:46:55

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

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

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

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

,其中包含服务实现的名称

com.mycorp.services.Fooservice

如果您使用Eclipse下的“可运行Jar文件导出”生成JAR文件项目中,选择“另存为ANT脚本”,然后修改ant脚本以将其打包到服务中,如上所述。

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

<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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文