47

I am using jersey for my project and tring to parse a URI from a string.

UriBuilder.fromUri("http://localhost:8000").build();

The code is simple, but I get a error below

java.lang.ClassNotFoundException: org.glassfish.jersey.internal.RuntimeDelegateImpl

It seems the program can not find the delegate. I already imported javax.ws.rs.core.UriBuilder and have jersey-common 2.0 that should contain the delegate in my build path. But I still get this error.

Does someone know how to fix it? Thanks!

Nathan
  • 8,093
  • 8
  • 50
  • 76
Spark8006
  • 635
  • 1
  • 7
  • 15

7 Answers7

87

If you're using Maven, use the following dependency:

<dependency>
    <groupId>org.glassfish.jersey.core</groupId>
    <artifactId>jersey-common</artifactId>
    <version>2.22.2</version>
    <scope>test</scope>
</dependency>

For Gradle, the following will work:

testImplementation 'org.glassfish.jersey.core:jersey-common:2.22.2'
Community
  • 1
  • 1
Archimedes Trajano
  • 35,625
  • 19
  • 175
  • 265
  • 3
    Can You explain why it needs this dependency – Ismail Iqbal Oct 12 '16 at 05:23
  • 6
    If you look at javax.ws.rs.ext.RuntimeDelegate#findDelegate, you'll see that it defaults to using org.glassfish.jersey.internal.RuntimeDelegateImpl. In production you probably have some other implementation, but this is not available to your tests. – Andrew Swan Oct 18 '16 at 05:27
  • I had removed some cxf dependencies when transitioning from cxf to spring-mvc. Adding the dependency in as you suggested fixed the problem. – mojave Jul 25 '17 at 23:30
  • +1 for Andrew Swan's explanation - it was very helpful and I can see why in the code now. I too am porting from cxf and was confused as to why Jersey was suddenly required for the unit tests. – java-addict301 Jul 28 '17 at 16:20
  • 2
    Why do you specify test scope? Is this class unsuitable for production? – MiguelMunoz Jan 23 '18 at 10:15
  • No. In production you would use whatever was provided. – Archimedes Trajano Jan 23 '18 at 14:07
9

Developing against a Wildfly 10.1 runtime I didn't want to introduce Jersey into my builds. With Gradle I used

testRuntime "org.jboss.resteasy:resteasy-jaxrs:$versions.resteasy"

resteasy version is 3.0.19.Final. This jar contains

META-INF/services/javax.ws.rs.ext.RuntimeDelegate

with an entry

org.jboss.resteasy.spi.ResteasyProviderFactory
Thomas Newman
  • 164
  • 1
  • 6
  • Thanks! I use quarkus which also uses resteasy. I added this and now the error is `Provider org.jboss.resteasy.spi.ResteasyProviderFactory could not be instantiated`. Any ideas? – Hamburml May 20 '23 at 20:30
5

In my case the problem was for another Jar being used named: javax.ws.rs-api-2.0.jar

Removing that jar solved my problem.

The jar that I have used:

<include name="jersey-client-1.9.jar" />
<include name="jersey-core-1.9.jar" />
<include name="jersey-multipart-1.9.jar" />
Shahadet
  • 51
  • 1
  • 4
4
<dependency>
    <groupId>org.glassfish.jersey.core</groupId>
    <artifactId>jersey-common</artifactId>
    <version>2.26</version>
    <scope>test</scope>
</dependency>

I ran into issues with Java 8 and jersey-common 2.22.2 but 2.26 worked.

1

tl;dr if you're using jersey-client 1.x within an app that has a "provider" class also present, and you have rs-api-2.1.jar there, you need to add jersey-server 1.x or remove rs-api-2.1 if you happen to also have jsr311-api-1.1.1 present.

Appears this is what is happening:

Apparently jersey-client, when it is first coming up, tries to create a "mime map" of different mime types.

jersey-core 1.x ContextResolverFactory:

  public void init(ProviderServices providersServices, InjectableProviderFactory ipf) {
    Map<Type, Map<MediaType, List<ContextResolver>>> rs = new HashMap();
    Set<ContextResolver> providers = providersServices.getProviders(ContextResolver.class);
    Iterator i$ = providers.iterator();

    while(i$.hasNext()) {
      ContextResolver provider = (ContextResolver)i$.next();
      List<MediaType> ms = MediaTypes.createMediaTypes((Produces)provider.getClass().getAnnotation(Produces.class));
       ...

So if there are any "providers" kicking around (ex: @Produces ({MediaType.WILDCARD})), it tries to lookup their type and add it to the mime type lists. However, it does it the following:

if you have rs-api-2.1.jar:

 private static RuntimeDelegate findDelegate() {
   try {
Object delegate = FactoryFinder.find("javax.ws.rs.ext.RuntimeDelegate", "org.glassfish.jersey.internal.RuntimeDelegateImpl", RuntimeDelegate.class);

If you have the following other dependency, it appears to implement it slightly differently:

jsr311-api-1.1.1.jar:

 private static RuntimeDelegate findDelegate() {
   try {
     Object delegate = FactoryFinder.find("javax.ws.rs.ext.RuntimeDelegate", "com.sun.ws.rs.ext.RuntimeDelegateImpl");

Jersey 1.x client happens to provide ./jersey-client-.19/com/sun/ws/rs/ext/RuntimeDelegateImpl.class

Whereas Jersey 1.x server provides:

./jersey-server-1.19/com/sun/jersey/server/impl/provider/RuntimeDelegateImpl.class

jersey 2.0 is "org.glassfish.jersey..." and provides "org.glassfish.jersey.internal.RuntimeDelegateImpl" (apparently sanely)

So it appears to me that rs-api-2.1 basically targets jersey 2.x by default. But happens to work with jersey-client 1.x if you also include jersey-server.

With jersey 2.x you don't need to include the "server to use the client" in such a weird way, seems to just work.

My hunch is either the API changed (you're not supposed to combine jersey 1.x with jsr-2? huh?), or maybe accidental flub, or bug, or poor design of jersey-client 1.x, who knows.

Adding things that happen to add "jersey 2.x" or "jersey-server 1.x" transitively also works.

These worked around the following runtime failure:

Caused by: java.lang.ExceptionInInitializerError: null
    at com.sun.jersey.core.spi.factory.ContextResolverFactory.init(ContextResolverFactory.java:86)
    at com.sun.jersey.api.client.Client.init(Client.java:340)
    at com.sun.jersey.api.client.Client.access$000(Client.java:119)
    at com.sun.jersey.api.client.Client$1.f(Client.java:192)
    at com.sun.jersey.api.client.Client$1.f(Client.java:188)
    at com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:193)
    at com.sun.jersey.api.client.Client.<init>(Client.java:188)
    at com.sun.jersey.api.client.Client.<init>(Client.java:171)
    at redacted
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244)
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:331)
    at org.familysearch.digitalarchive.aggregator.integration.ServiceAccountConfig$$EnhancerBySpringCGLIB$$a3409578.identityService(<generated>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
    ... 29 common frames omitted
Caused by: java.lang.RuntimeException: java.lang.ClassNotFoundException: org.glassfish.jersey.internal.RuntimeDelegateImpl
    at javax.ws.rs.ext.RuntimeDelegate.findDelegate(RuntimeDelegate.java:154)
    at javax.ws.rs.ext.RuntimeDelegate.getInstance(RuntimeDelegate.java:121)
    at javax.ws.rs.core.MediaType.valueOf(MediaType.java:196)
    at com.sun.jersey.core.header.MediaTypes.<clinit>(MediaTypes.java:65)
    ... 48 common frames omitted
Caused by: java.lang.ClassNotFoundException: org.glassfish.jersey.internal.RuntimeDelegateImpl
    at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
    at org.springframework.boot.loader.LaunchedURLClassLoader.loadClass(LaunchedURLClassLoader.java:151)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:264)
    at javax.ws.rs.ext.FactoryFinder.newInstance(FactoryFinder.java:111)
    at javax.ws.rs.ext.FactoryFinder.find(FactoryFinder.java:209)
    at javax.ws.rs.ext.RuntimeDelegate.findDelegate(RuntimeDelegate.java:136)
    ... 51 common frames omitted
rogerdpack
  • 62,887
  • 36
  • 269
  • 388
0

Please use UriComponents instead URI

UriComponents uriComponents = UriComponentsBuilder.fromUriString("http://localhost:8000").build();

Then you can get URI object like

uriComponents.toUri()
Danish Ali
  • 31
  • 8
-1
compile 'org.springframework.boot:spring-boot-starter-jersey:1.2.0.RELEASE'

This worked for me!!!