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

12
我正在尝试在Java中创建一个可扩展的应用程序,并选择使用SPI。根据我正在遵循的这个教程,我应该在我的jar包中的META-INF/services目录下创建特定的文件,但我不知道该如何操作。 我知道如何使用META-INF创建jar文件夹,但找不到创建services文件夹及其内部文件的方法。(我正在使用Eclipse)
感谢您的帮助。
2个回答

12

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

我不确定您计划如何执行您的jar文件,现在您可以在MANIFEST.MF中放入这一行:

Manifest-Version: 1.0

对于您的服务文件夹,您需要创建一个名为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/

谢谢,简单而且完成了任务。 - tom

5
假设您正在使用Apache Ant构建jar文件, 您需要添加一个metainf文件集,或者更好的方法是使用如下的services指令。
<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脚本以按上述方式打包服务。

他正在跟随教程,教程要求他手动创建它。 - CoolBeans
到目前为止,我一直在使用Eclipse中的向导构建jar。 - tom
1
@tom - дёҖж—ҰдҪ ејҖе§Ӣжһ„е»әдјҒдёҡеә”з”ЁзЁӢеәҸпјҢдҪ дјҡжғіиҰҒдҪҝз”ЁеғҸantжҲ–mavenиҝҷж ·зҡ„жһ„е»әе·Ҙе…·гҖӮ然иҖҢпјҢдҪҝз”Ёeclipseжһ„е»әjarsжқҘејҖе§Ӣд№ҹжҳҜеҸҜд»Ҙзҡ„гҖӮ - CoolBeans
非常感谢。虽然我将CoolBeans的回答标记为采纳,因为它更容易,但你的回答也绝对有所帮助。 - tom
@tom,如果你想使用jar导出器,请使用可运行的jar文件导出,它有一个选项可以将导出保存为ant构建文件。保存它,然后随心所欲地编辑它。 - Edwin Buck

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接