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

tom*_*tom 11 java eclipse meta-inf service-provider

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

谢谢你的帮助.

Coo*_*ans 11

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

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

Manifest-Version: 1.0
Run Code Online (Sandbox Code Playgroud)

对于您的services文件夹,您需要创建一个名为com.example.dictionary.spi.Dictionary以下一行命名的文件,如教程中所述: -

com.example.dictionary.GeneralDictionary
Run Code Online (Sandbox Code Playgroud)

仅供参考 - META-INF是一个内部Java 目录.通常,您希望依靠诸如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/
Run Code Online (Sandbox Code Playgroud)


Edw*_*uck 5

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

<jar jarfile="pinky.jar">
  <fileset dir="build/classes"/>
  <service type="javax.script.ScriptEngineFactory"
           provider="org.acme.PinkyLanguage"/>
</jar>
Run Code Online (Sandbox Code Playgroud)

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

jar -uf jarfile.jar META-INF/services/name.of.general.service
Run Code Online (Sandbox Code Playgroud)

其中包含服务实现的名称

com.mycorp.services.Fooservice
Run Code Online (Sandbox Code Playgroud)

如果使用项目下的Eclipse"Runnable Jar文件导出"生成JAR文件,请选择"另存为ANT脚本",然后修改ant脚本以打包服务,如上所述.