1

Intro

I am trying to create multiple services which fuzz the input (for testing purposes. I am using the concept of dependency injection using java.util.ServiceLoader<T> feature. I am using IntelliJ for this project.

Github Project Link: https://github.com/ghoshabhi/java-service-loader

Problem

I have created a META-INF/services directory with one file within it: htmlfuzzing.spi.HtmlFuzzer mentioning the path to my implementation class (htmlfuzzing.TagReplacer). I was trying to create a structure similar to an example on Oracle's website.

I also went ahead and marked the META-INF directory as resources by going to Project Settings > Modules :

enter image description here

Here's what my output directory looks like:

enter image description here

As you can see above, the implementation class' class files are not added to the out/ directory.

Here's my complete directory structure:

enter image description here

Question

I am obviously doing something wrong here, but I am not able to understand what. What is the correct way of adding or marking the META-INF directory as the resources or what is the correct way to include all the service implementation classes?

Any help or hint towards solving this is appreciated.

EDIT - 1

Added src/resource folder and placed the META-INF directory within it. But I still don't see the implementation class' class files in the out directory.

Also, one thing I have noticed is as soon as I mark any directory as resource in the project structure - it vanishes from the project view pan on the left. Anything odd there?

enter image description here

Abhishek Ghosh
  • 2,593
  • 3
  • 27
  • 60
  • In my opinion the way you have defined it you need one more folder between your src and your META-INF folder and you need to map this folder as resource folder. The point is that you want in your bin folder to have the folder META-INF picked up. Make a folder src/resource/META-INF and mark the resource folder as resource folder – Alexander Petrov Sep 12 '18 at 19:23
  • @AlexandarPetrov: I did as you said, but I still don't see any implementation classes in the `out` dir and they are not returned by the `serviceLoader.iterator()`. I have updated my question with a screenshot of the newly added `resource` dir. – Abhishek Ghosh Sep 12 '18 at 19:59
  • 1
    I actualy dont use IntelliJ :) just it made me impression that you specify straight the meta inf as a resource folder which I dont find very correct to be honest. But in your place i would create one maven project and stop dealing with IDE specific settings. I use Eclipse, but my projects are configured as maven projects. – Alexander Petrov Sep 12 '18 at 20:05
  • Thank you for the advice @AlexandarPetrov, but I have a restriction to not use maven. Need no build steps or anything :/ – Abhishek Ghosh Sep 12 '18 at 20:16
  • I expect that if you place the META-INF/services straight into your src folder and don\t declare the META-INF folder as resource it will end up in your binary folder and then it will work. – Alexander Petrov Sep 12 '18 at 20:21
  • @AlexandarPetrov: I have tried that too, i.e. simply have the META-INF as a folder wihtin `ModuleName/src/` - that module's class files do not end up in the `out` dir. – Abhishek Ghosh Sep 12 '18 at 20:45
  • I can see you don't have the same problem I had (but you may want to double-check in the file system anyway) but for some reason, IntelliJ had the following directory under resources: `META-INF.services`, as opposed to `META-INF/services`... Usually, you can create a folder and a subfolder using a `.`, but apparently not always... :| – Erk Dec 28 '20 at 01:52

0 Answers0