users@jersey.java.net

Re: [Jersey] Using jersey as a REST client in an ant taskdef? Failing horribly...

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Mon, 01 Mar 2010 13:25:35 +0100

Hi Alex,

This could be a class loading issue with ant. Can you try executing in
forked mode?

The JAX-RS class loading code, in FactoryFinder, is as follows:

     private static Object newInstance(String className,
             ClassLoader classLoader) throws ClassNotFoundException {
         try {
             Class spiClass;
             if (classLoader == null) {
                 spiClass = Class.forName(className);
             } else {
                 try {
                     spiClass = Class.forName(className, false,
classLoader);
                 } catch (ClassNotFoundException ex) {
                     spiClass = Class.forName(className);
                 }
             }
             return spiClass.newInstance();
         } catch (ClassNotFoundException x) {
             throw x;
         } catch (Exception x) {
             throw new ClassNotFoundException(
                     "Provider " + className + " could not be
instantiated: " + x,
                     x);
         }

And the "classLoader" is obtained from:

     static ClassLoader getContextClassLoader() {
         return AccessController.doPrivileged(
             new PrivilegedAction<ClassLoader>() {
                 public ClassLoader run() {
                     ClassLoader cl = null;
                     try {
                         cl =
Thread.currentThread().getContextClassLoader();
                     } catch (SecurityException ex) { }
                     return cl;
                 }
         });
     }


So basically the context class loader is used (if possible) otherwise
the class loader of the current class, RuntimeDelegate, is used.

 From the stack trace the context class loader is used but it appears
that ant does not set this to what is expected.

Paul.

On Feb 20, 2010, at 2:16 AM, Alex Sherwin wrote:

> We use Jersey all the time in web apps and standalone java apps, and
> I thought it should be easy to use it in a custom ant task.
> However, I've had nothing but problems so far...
>
> I've tried the latest 1.0.x and 1.1.x releases without success.
>
> I've got jersey-bundle, jersey-apache-client and commons-
> httpclient-3.1 (plus other deps) on the path (verified with ant in
> verbose mode) and get this error:
>
> BUILD FAILED
> java.lang.ExceptionInInitializerError
> at com.acadiasoft.ant.task.http.HttpTask.doGet(HttpTask.java:50)
> at com.acadiasoft.ant.task.http.HttpTask.execute(HttpTask.java:
> 31)
> at
> org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:288)
> at sun.reflect.GeneratedMethodAccessor3.invoke(Unknown Source)
> at
> sun
> .reflect
> .DelegatingMethodAccessorImpl
> .invoke(DelegatingMethodAccessorImpl.java:25)
> at java.lang.reflect.Method.invoke(Method.java:597)
> at
> org
> .apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:
> 106)
> at org.apache.tools.ant.Task.perform(Task.java:348)
> at org.apache.tools.ant.Target.execute(Target.java:357)
> at org.apache.tools.ant.Target.performTasks(Target.java:385)
> at
> org.apache.tools.ant.Project.executeSortedTargets(Project.java:1337)
> at org.apache.tools.ant.Project.executeTarget(Project.java:1306)
> at
> org
> .apache
> .tools
> .ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
> at org.apache.tools.ant.Project.executeTargets(Project.java:
> 1189)
> at org.apache.tools.ant.Main.runBuild(Main.java:758)
> at org.apache.tools.ant.Main.startAnt(Main.java:217)
> at org.apache.tools.ant.launch.Launcher.run(Launcher.java:257)
> at org.apache.tools.ant.launch.Launcher.main(Launcher.java:104)
> Caused by: java.lang.RuntimeException:
> java.lang.ClassNotFoundException:
> com.sun.ws.rs.ext.RuntimeDelegateImpl
> at
> javax.ws.rs.ext.RuntimeDelegate.getInstance(RuntimeDelegate.java:112)
> at javax.ws.rs.core.MediaType.<clinit>(MediaType.java:44)
> ... 18 more
> Caused by: java.lang.ClassNotFoundException:
> com.sun.ws.rs.ext.RuntimeDelegateImpl
> at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
> at java.security.AccessController.doPrivileged(Native Method)
> at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
> at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
> at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
> at
> javax.ws.rs.ext.FactoryFinder.newInstance(FactoryFinder.java:44)
> at javax.ws.rs.ext.FactoryFinder.find(FactoryFinder.java:141)
> at
> javax.ws.rs.ext.RuntimeDelegate.getInstance(RuntimeDelegate.java:95)
> ... 19 more
>
> Total time: 1 second
>
>
> If I run the same code through a unit test (invoked from eclipse),
> with the same classpath as ant is using, it works fine...
>
> Is there something I'm missing... why should it matter that its
> running as a custom ant taskdef?
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>