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


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

谢谢你的帮助。


6115
2018-06-16 18:25


起源



答案:


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

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

Manifest-Version: 1.0

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

com.example.dictionary.GeneralDictionary

仅供参考 - 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/

11
2018-06-16 18:27



谢谢,简单并完成了工作 - tom
@tom - 没问题。欢迎堆栈溢出! - CoolBeans


答案:


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

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

Manifest-Version: 1.0

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

com.example.dictionary.GeneralDictionary

仅供参考 - 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/

11
2018-06-16 18:27



谢谢,简单并完成了工作 - tom
@tom - 没问题。欢迎堆栈溢出! - CoolBeans


假设你正在构建你的 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“Runnable Jar文件导出”生成JAR文件,请选择“另存为ANT脚本”,然后修改ant脚本以打包服务,如上所述。


5
2018-06-16 18:29



他正在按照教程要求他手工创建它。 - CoolBeans
好吧,到目前为止,我一直在使用Eclipse中的向导构建jar。 - tom
@tom - 一旦你开始构建企业应用程序,你会想要转向使用像这样的构建工具 ant 要么 maven。但是,可以使用eclipse来构建jar开始。 - CoolBeans
非常感谢你。即使我将CoolBeans的答案标记为已被接受,因为它对我来说更容易,但你的答案肯定也是如此 - tom
@tom,如果你想使用jar导出器,使用runnable jar文件导出,它有一个选项将导出保存为ant build文件。保存它,然后根据您的内容进行编辑。 - Edwin Buck