13

I get the following exception when I try to create a WebTarget from a Jersey client.

My code

Client client = ClientBuilder.newClient();
WebTarget baseTarget = client.target("http://127.0.0.1:9000");

Exception stack trace

java.lang.IllegalStateException: No generator was provided
and there is no default generator registered
    at    org.glassfish.hk2.internal.ServiceLocatorFactoryImpl.internalCreate(ServiceLocatorFactoryImpl.java:266)
    at org.glassfish.hk2.internal.ServiceLocatorFactoryImpl.create(ServiceLocatorFactoryImpl.java:247)
    at org.glassfish.jersey.internal.inject.Injections._createLocator(Injections.java:138)
    at org.glassfish.jersey.internal.inject.Injections.createLocator(Injections.java:109)
    at org.glassfish.jersey.internal.RuntimeDelegateImpl.<init>(RuntimeDelegateImpl.java:61)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at java.lang.Class.newInstance(Class.java:374)
    at javax.ws.rs.ext.FactoryFinder.newInstance(FactoryFinder.java:118)
    at javax.ws.rs.ext.FactoryFinder.find(FactoryFinder.java:225)
    at javax.ws.rs.ext.RuntimeDelegate.findDelegate(RuntimeDelegate.java:135)
    at javax.ws.rs.ext.RuntimeDelegate.getInstance(RuntimeDelegate.java:120)
    at javax.ws.rs.core.UriBuilder.newInstance(UriBuilder.java:95)
    at javax.ws.rs.core.UriBuilder.fromUri(UriBuilder.java:119)
    at org.glassfish.jersey.client.JerseyWebTarget.<init>(JerseyWebTarget.java:71)
    at org.glassfish.jersey.client.JerseyClient.target(JerseyClient.java:185)
    at org.glassfish.jersey.client.JerseyClient.target(JerseyClient.java:70)

What could be the issue ?

Adi
  • 2,248
  • 1
  • 12
  • 22
jubi
  • 133
  • 1
  • 1
  • 5
  • Another reason (in my case) why this issue can occur is when someone is trying to relocate org.glassfish packages while shading, as mentioned here https://github.com/KostyaSha/docker-java-shade/issues/1 To quote from there 'Class org.glassfish.hk2.extension.ServiceLocatorGenerator gets relocated to shade1.org.glassfish.hk2.extension.ServiceLocatorGenerator but the META-INF/services/org.glassfish.hk2.extension.ServiceLocatorGenerator file doesn't, so the implementation can't be looked up.' – Deven Jul 29 '19 at 13:50

4 Answers4

24

Add the following archives from the Jersey-ext-directory to your classpath:

hk2-api-*.jar

hk2-locator-*.jar

hk2-utils-*.jar

javax-inject-*.jar

jersey-guava-*.jar

sinclair
  • 2,406
  • 4
  • 20
  • 47
  • I have the same issue and have all these jar files in my classpath but still getting the exception. I also noticed that there is the following warning: WARNING: Cannot find a default implementation of the HK2 ServiceLocatorGenerator – Petros P Dec 16 '14 at 00:15
  • I experienced the exact same issue and it turned out there was a conflict of jars. So just to put it into perspective, my runtime is OSGI where it was running just fine but when I was trying to execute jersey client using plain java class via Eclipse, it would throw the exception. The solution worked for me was I removed all classpath dependencies that were used by OSGI and I added the libraries mentioned in this reply. In addition I had to add Jersey Client and Jersey Common libraries. I hope this helps. I wish jersey distribution would just bundle them up so developers do not have to search. – MG Developer May 07 '15 at 16:24
  • I forgot hk2-locator-*.jar which caused the above error. When added my problem solved. – Yergalem Jun 06 '17 at 19:27
  • @sinclair what do you mean by hk2-api-*.jar? Do you all jars beginning with hk2-api? – Deven Jul 28 '19 at 10:08
  • @Deven hk2-api-.jar e.g. hk2-api-2.5.0.jar – sinclair Jul 29 '19 at 05:19
6

Register hk2-locator and javassist as bundles on your OSGi framework.

Jersey uses hk2-locator to find services on OSGi.

1

I had this same issue and I could finally solve it by starting my bundle which uses Jersey to publish an endpoint at level 2 in the Felix Framework (OSGi-5.4). I am not sure why, but seems like the HK2-Locator dependencies:

  • hk2-api-2.4.0-b10.jar -> Level 1
  • hk2-locator-2.4.0-b10.jar -> Level 1
  • hk2-utils-2.4.0-b10.jar -> Level 1
  • org.apache.servicemix.bundles.javax-inject-1_2.jar -> Level 1
  • jersey-guava-2.22.1.jar -> Level 1

need to start first before the bundle who uses them:

  • your-bundle-1.0.0.jar -> Level 2

Once you set your bundle to start on this new level then make sure to execute frameworklevel 2 in Felix console to start the bundles under that level. Restart the framework and it will work.

Joe Almore
  • 3,245
  • 8
  • 40
  • 65
0

I had this issue in Weblogic. The default implementation can be found inside the jar file: hk2-locator.jar: org.jvnet.hk2.external.generator.ServiceLocatorGeneratorImpl

The problem is that this jar file contains the packages in this way: org.jvnet... instead of this other: org.glassfish...

In the case of Weblogic I had the next configuration in weblogic.xml:

<wls:container-descriptor>
    <wls:prefer-application-packages>
        <wls:package-name>javax.ws.rs.*</wls:package-name>
        <wls:package-name>org.glassfish.jersey.*</wls:package-name>
        <wls:package-name>org.glassfish.hk2.*</wls:package-name>
    </wls:prefer-application-packages>
</wls:container-descriptor>

So I had to add a new entry:

<wls:package-name>org.jvnet.*</wls:package-name>
ravenskater
  • 484
  • 1
  • 4
  • 14