score:139
Create a new source folder with the location src/main/resources
, then create your META-INF/services
folder in there and drop in your fully-qualified class name (FQCN) file. This should copy them into the jar file automatically. So for implementations of an interface with a FQCN of com.acme.MyInterface
, you'll have:
Project
| src
| | main
| | java
| | [your source code]
| | resources
| | META-INF
| | services
| | com.acme.MyInterface
Note that com.acme.MyInterface
is the name of the file, not a directory structure like a Java package. The name of the file is the FQCN of the interface that you're implementing, and in it, you'll have the FQCN of each implementation on its own line, e.g.:
com.example.MyInterfaceImpl
com.example.AnotherMyInterfaceImpl
It's worth noting that this applies to Gradle projects with the default source sets as well.
Once you do this, you can load all the implementations of the interface using ServiceLoader
:
ServiceLoader<MyInterface> loader = ServiceLoader.load(MyInterface.class);
for (MyInterface service : loader) {
// Prints com.example.MyInterfaceImpl and com.example.AnotherMyInterfaceImpl
System.out.println(service.class.getName());
}
Some things to note:
- All the implementations must have a no-args constructor
- Applications that use modules or custom classloaders may have to use the overloads of
ServiceLoader.load
If these conditions won't work for you, then you may want to switch to another system, e.g. a CDI-style framework like Spring, EJB, etc.
score:16
Alternatively, if your project does not use a standard directory structure (or your simply wish to have alternate resource directories), you can specify resource directories manually the POM file.
For example, if your META-INF/services
is located in a folder called resources
which lies in the root of your project directory, could specify it as follows:
<project>
...
<build>
...
<resources>
<resource>
<directory>resources</directory>
</resource>
</resources>
...
</build>
...
</project>
You can use this to specify several directories by adding multiple <resource>
elements.
score:25
By default Maven looks for resources at:
src/main/resources
So put it at
src/main/resources/META-INF/services
Source: stackoverflow.com
Related Query
- How to include a config file in the "META-INF/services" folder of a JAR using Maven
- How to include the jars referenced in "Java Build Path" section of an Eclipse project in a jar file generated with Ant using a build.xml file
- How to include an XML file into the root of a jar using Ant script?
- How can I load an xml resource file from within an executible Jar file and save it to the folder the jar is located in?
- Using eclipse to edit C++ code. How do I automatically include a default header file without editing all the code
- How to generate an executable jar file for a maven servlet project in eclipse and execute a class using the jar file?
- How can I add JAR files to the web-inf/lib folder in Eclipse?
- How to create a executable jar file for Testng and the runnnig point should be the Xml file
- how to create jar file using eclipse
- How to tell Maven to include the jar dependency, not the subproject source directory in Eclipse?
- How do you create or find a "userdata.img" file for ABI armeabi to copy into the AVD folder
- How to view the contents of jar file in eclipse or jar-file-explorer
- How to include a dependency to a jar file from an eclipse/osgi application?
- How to includes all images in jar file using eclipse
- How can I browse the source code in a jar file containing source code in Eclipse?
- How can I change the view / editor for a file in Eclipse using the keyboard?
- How to Run a Java Class File from the Command Line that was Compiled by Eclipse? External Jar problems
- How to include resource folder in executable .jar file in eclipse?
- Maven Checkstyle and Eclipse Checkstyle report similar but different results using the same config file
- How to generate a jar file for a java project and use the jar in an android project
- how to package jar file include classes in jar files
- How to get resource files from the runnable jar file which are in buildpath
- How do I save a file using the Eclipse compare editor
- How to make the runnable jar file for a maven eclipse project with hibernate configuration
- Eclipse API: How to get IFile from an IProject using only the file name?
- how to pack jar file without containing depedency jar libraries using eclipse?
- How to edit the existing java source file by using the plugin development API in eclipse?
- How to navigate to parent folder of current file in Project Explorer view using keyboard shortcut in Eclipse IDE?
- VLCJ - playing a video from the "res" folder works great in eclipse, but not from the executable JAR file
- How to create a jar file for the eclipse plugin project that should be able to download and run without eclipse software?
More Query from same tag
- -Dmaven.multiModuleProjectDirectory system propery is not set, Issue with maven 3.3.3 and eclipse luna
- Android Development Setup on Windows 8 Pro 64bit
- Which tools to use for Grails Development
- Java project from gradle project
- Create Eclipse Project Presets
- How to generate a custom serialVersionUID in Eclipse
- MessageDialog title is coming 'Not Responding'
- how to look at debugging error in android.(eclipse)?
- Spring Tool Suite says it could not create BootDashView (or any Spring view)
- I can't add new/update Android SDK Manager stuff
- OSGI Mylyn java.lang.ClassNotFoundException: AbstractRepositoryConnector
- V8 debugger in Eclipse: Logic, or weirdest bug ever?
- Text utf-8 in console works in eclipse, but fails to work with an exported jar
- No compiler is provided in this environment. maven build failed
- Is it possible to automatically add an import in all new classes in Eclipse?
- New Eclipse & Android Install - Conversion to Dalvik format failed with error 1
- Download bugzilla eclipse plugin?
- How do I import *.java text files into an Eclipse project?
- How to run monkeytalk scripts each time with new data?
- Eclipse code formatter: how to remove its effects?
- How can I know how much memory Eclipse plugins use (separately)
- org.eclipse.xtext.resource.FileNotFoundOnClasspathException: Couldn't find resource on classpath. URI was 'classpath:/../Less.xtext'
- Headless PDE Build and Java Compiler Compliance
- Import of excuting jar doesn't work in eclipse
- Joda Date and SWT DateTime conflict
- How to limit Eclipse's "References => Project" search to the project itself?
- Why can't it find Widget.TabPageIndicator?
- Custom project in Adobe Flex Builder 3?
- Shortcut for finding file on Eclipse
- How to stop eclipse to redownload the plugin from repository for a maven project