我正在尝试用Java创建一个可扩展的应用程序并选择使用SPI。根据这个 教程 我关注,我应该创建一个特定的文件 META-INF/services
在我的罐子里,但不知道如何。
我知道如何用jar创建jar META-INF
,但找不到如何创建的方法 services
文件夹和此文件夹中的特定文件。 (我正在使用Eclipse)
谢谢你的帮助。
我正在尝试用Java创建一个可扩展的应用程序并选择使用SPI。根据这个 教程 我关注,我应该创建一个特定的文件 META-INF/services
在我的罐子里,但不知道如何。
我知道如何用jar创建jar META-INF
,但找不到如何创建的方法 services
文件夹和此文件夹中的特定文件。 (我正在使用Eclipse)
谢谢你的帮助。
由于本教程指示手动创建它而不是使用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/
由于本教程指示手动创建它而不是使用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/
假设你正在构建你的 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脚本以打包服务,如上所述。