【问题标题】:creating META-INF/services folder in Eclipse在 Eclipse 中创建 META-INF/services 文件夹
【发布时间】:2011-09-16 15:09:21
【问题描述】:

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

感谢您的帮助。

【问题讨论】:

    标签: java eclipse meta-inf service-provider


    【解决方案1】:

    由于教程指示手动创建它而不是使用 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 meta 目录。通常,您希望依靠您的打包工具,例如 ant 或 maven 来构建 META-INF 文件夹的内容,而不是自己动手。

    可以查看META-INF文件夹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/
    

    【讨论】:

    • 谢谢,简单,完成了工作
    • @tom - 没问题。欢迎来到堆栈溢出!
    【解决方案2】:

    假设您正在构建您的 jar file with Apache's Ant,您需要添加一个 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 脚本以打包服务。

    【讨论】:

    • 他正在按照教程要求他手动创建它。
    • 嗯,到目前为止,我一直在使用 Eclipse 中的向导构建 jar。
    • @tom - 一旦您开始构建企业应用程序,您可能希望转而使用像 antmaven 这样的构建工具。但是,使用 eclipse 来构建 jars 是可以的。
    • 非常感谢。尽管我将 CoolBeans 的答案标记为已接受,因为它对我来说更容易,但你的答案肯定也有帮助
    • @tom,如果要使用 jar 导出器,请使用可运行的 jar 文件导出,它可以选择将导出保存为 ant 构建文件。保存它,然后根据您的喜好对其进行编辑。
    猜你喜欢
    • 2017-06-22
    • 2021-08-10
    • 1970-01-01
    • 2012-05-06
    • 1970-01-01
    • 2011-11-03
    • 1970-01-01
    • 1970-01-01
    • 2020-11-03
    相关资源
    最近更新 更多