Skip to main content

XJC:Plugin Custom plugin causes ClassCastException

18 replies [Last post]
cybersliver
Offline
Joined: 2004-10-17
Points: 0

Hi,

I've created a custom plugin for XJC, but when running the ant XJCTask, I get the following exception:

BUILD FAILED
java.util.ServiceConfigurationError: com.sun.tools.xjc.Plugin: Provider se.ericsson.jaxb.validate.PluginImpl could not be instantiated: java.lang.ClassCastException
at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1225)
at org.apache.tools.ant.Project.executeTarget(Project.java:1185)
at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:40)
at org.eclipse.ant.internal.ui.antsupport.EclipseDefaultExecutor.executeTargets(EclipseDefaultExecutor.java:32)
at org.apache.tools.ant.Project.executeTargets(Project.java:1068)
at org.eclipse.ant.internal.ui.antsupport.InternalAntRunner.run(InternalAntRunner.java:423)
at org.eclipse.ant.internal.ui.antsupport.InternalAntRunner.main(InternalAntRunner.java:137)
Caused by: java.util.ServiceConfigurationError: com.sun.tools.xjc.Plugin: Provider se.ericsson.jaxb.validate.PluginImpl could not be instantiated: java.lang.ClassCastException
at java.util.ServiceLoader.fail(ServiceLoader.java:207)
at java.util.ServiceLoader.access$100(ServiceLoader.java:164)
at java.util.ServiceLoader$LazyIterator.next(ServiceLoader.java:353)
at java.util.ServiceLoader$1.next(ServiceLoader.java:421)
at com.sun.tools.xjc.Options.findServices(Options.java:884)
at com.sun.tools.xjc.Options.getAllPlugins(Options.java:336)
at com.sun.tools.xjc.Options.parseArgument(Options.java:632)
at com.sun.tools.xjc.Options.parseArguments(Options.java:742)
at com.sun.tools.xjc.XJC2Task._doXJC(XJC2Task.java:444)
at com.sun.tools.xjc.XJC2Task.doXJC(XJC2Task.java:434)
at com.sun.tools.xjc.XJC2Task.execute(XJC2Task.java:369)
at com.sun.istack.tools.ProtectedTask.execute(ProtectedTask.java:55)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:275)
at org.apache.tools.ant.Task.perform(Task.java:364)
at org.apache.tools.ant.Target.execute(Target.java:341)
at org.apache.tools.ant.Target.performTasks(Target.java:369)
at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1216)
... 6 more
Caused by: java.lang.ClassCastException
at java.lang.Class.cast(Class.java:2990)
at java.util.ServiceLoader$LazyIterator.next(ServiceLoader.java:345)
... 20 more
--- Nested Exception ---
java.util.ServiceConfigurationError: com.sun.tools.xjc.Plugin: Provider se.ericsson.jaxb.validate.PluginImpl could not be instantiated: java.lang.ClassCastException
at java.util.ServiceLoader.fail(ServiceLoader.java:207)
at java.util.ServiceLoader.access$100(ServiceLoader.java:164)
at java.util.ServiceLoader$LazyIterator.next(ServiceLoader.java:353)
at java.util.ServiceLoader$1.next(ServiceLoader.java:421)
at com.sun.tools.xjc.Options.findServices(Options.java:884)
at com.sun.tools.xjc.Options.getAllPlugins(Options.java:336)
at com.sun.tools.xjc.Options.parseArgument(Options.java:632)
at com.sun.tools.xjc.Options.parseArguments(Options.java:742)
at com.sun.tools.xjc.XJC2Task._doXJC(XJC2Task.java:444)
at com.sun.tools.xjc.XJC2Task.doXJC(XJC2Task.java:434)
at com.sun.tools.xjc.XJC2Task.execute(XJC2Task.java:369)
at com.sun.istack.tools.ProtectedTask.execute(ProtectedTask.java:55)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:275)
at org.apache.tools.ant.Task.perform(Task.java:364)
at org.apache.tools.ant.Target.execute(Target.java:341)
at org.apache.tools.ant.Target.performTasks(Target.java:369)
at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1216)
at org.apache.tools.ant.Project.executeTarget(Project.java:1185)
at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:40)
at org.eclipse.ant.internal.ui.antsupport.EclipseDefaultExecutor.executeTargets(EclipseDefaultExecutor.java:32)
at org.apache.tools.ant.Project.executeTargets(Project.java:1068)
at org.eclipse.ant.internal.ui.antsupport.InternalAntRunner.run(InternalAntRunner.java:423)
at org.eclipse.ant.internal.ui.antsupport.InternalAntRunner.main(InternalAntRunner.java:137)
Caused by: java.lang.ClassCastException
at java.lang.Class.cast(Class.java:2990)
at java.util.ServiceLoader$LazyIterator.next(ServiceLoader.java:345)
... 20 more

I've set the java.endorsed.dirs to the JAXB-RI/lib I'm using for javac and XJCTask.

Any idea what's wrong?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
datakey
Offline
Joined: 2010-04-10
Points: 0

No update about this issue?
I'm getting the same error using the plugin namespace-prefix, from jaxb2-commons Java.net project.
With the 'package workaround' works great.

Thanks.

datakey
Offline
Joined: 2010-04-10
Points: 0

I sent a mail to JAXB list (users@jaxb.java.net) if there is something new.
If package-change workaround doesn't fit in some cases, instead of calling com.sun.tools.xjc.XJCTask the jaxb-xjc.jar can be invoked directly using <java> task:

<java jar="${jaxb.dir}/jaxb-xjc.jar" fork="true">
<arg path="schema.xsd" />
<arg value="-d" />
<arg path="destdir" />
<arg value="-b" />
<arg path="bindings.xjb" />
<arg value="-extension" />
<arg value="-classpath" />
<arg pathref="custom-plugin.path" />
<arg value="-Xnamespace-prefix" />
</java>
dma_k
Offline
Joined: 2006-11-03
Points: 0

I have finally decided to move plugins to package "com.sun.tools.xjc.addon" and recompiled jaxws-maven-plugin with all necessary JAXB plugins included. One who is interested can trace my results here:

http://centurion.dynalias.com/w/%D0%BF%D1%80%D0%BE%D0%B3%D1%80%D0%B0%D0%...

Message was edited by: dma_k

wasppit
Offline
Joined: 2005-11-02
Points: 0

I think I understand.

The ParallelWorldClassLoader relies on a MaskingClassLoader, which filters, among others, package "com.sun.tools.xjc".

If your plugin is in that package, it gets loaded in the right classloader, otherwise the plugin and its superclass are loaded with a different classloader, resulting in two versions of the com.sun.tools.xjc.Plugin class (i.e. two Class instances for the same class).

This seems to be a bug.

gordan
Offline
Joined: 2005-01-06
Points: 0

I got the same problem and your solution also works for me.
You must have lost hours (or days, or weeks) on that one !

Have you contacted kohsuke or filed a bug report ?
I would be glad to vote for it.

jaylogan
Offline
Joined: 2004-05-04
Points: 0

I am running into the same problem just trying to use an existing plugin.

I think the following bug is the source:
https://jaxb.dev.java.net/issues/show_bug.cgi?id=645

It has been fixed but scheduled for 2.1.13 release. Does anyone know whether the fix is in the 2.2 EA release?

Martin Grebac

Hi,
fix for 645 is not in 2.2EA, but you can use 2.2 nightlies to get the fix.
MartiNG

metro@javadesktop.org wrote:
> I am running into the same problem just trying to use an existing plugin.
>
> I think the following bug is the source:
> https://jaxb.dev.java.net/issues/show_bug.cgi?id=645
>
> It has been fixed but scheduled for 2.1.13 release. Does anyone know whether the fix is in the 2.2 EA release?
> [Message sent by forum member 'jaylogan' ]
>
> http://forums.java.net/jive/thread.jspa?messageID=372252
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@metro.dev.java.net
> For additional commands, e-mail: users-help@metro.dev.java.net
>
>

--
Martin Grebac, http://blogs.sun.com/mgrebac

Web Technologies & Standards
Sun Microsystems Czech

ICQ: 93478885

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@metro.dev.java.net
For additional commands, e-mail: users-help@metro.dev.java.net

elygre
Offline
Joined: 2006-10-23
Points: 0

I've had the same problem, and this page kept coming up in google. So, for the benefit of anybody else who ends up here, this is the way to go:

* I never succeeded running the plain RI ant task under Java 6
* Using the JAXB2 Commons ant task, everything works nicely, no hassle at all
* Download the distro from http://confluence.highsource.org/display/J2B/Home
* Include the file jaxb2-basics-ant-0.5.0.jar in your ant taskdef classpath
* Use classname="org.jvnet.jaxb2_commons.xjc.XJC2Task" in you ant taskdef

See http://confluence.highsource.org/display/J2B/JAXB2+Basics+XJC+Ant+Task for more information.

kohsuke
Offline
Joined: 2003-06-09
Points: 0

Please refer to section 7.1.2 on https://jaxb.dev.java.net/guide/Migrating_JAXB_2_0_applications_to_JavaS...

Specifically, don't add anything but the API jar to endorsed class path.

dma_k
Offline
Joined: 2006-11-03
Points: 0

Kohsuke, I don't see any reason, why endorsing of "jaxb-api.jar" should solve the problem, as we deal with wrong com.sun.tools.xjc.Plugin class version... Does not work for me.

I have also seen some solution at https://issues.apache.org/jira/browse/CXF-1880 (see attached patch file, which says, plugin should be in com.sun.tools.xjc.addon package). Is it reasonable as well?

I want to enable plugins for jaxws-maven-plugin (from org.codehaus.mojo), which depends on jaxws-tools v2.1.7. I have checked dependencies many times... no results.

Also setting this property for debugging does not have any impact. Is there any secret here?

System.setProperty("com.sun.tools.xjc.Options.findServices", "true");

> Please refer to section 7.1.2 on https://jaxb.dev.java.net/guide/Migrating_JAXB_2_0_applications_to_JavaS...

Message was edited by: dma_k

arthur83
Offline
Joined: 2007-06-29
Points: 0

Hi,

I'm currently developing also an XJCPlugin. Under Java JRE 1.5 everything works fine. But under JRE 1.6 I get the same error: java.util.ServiceConfigurationError: com.sun.tools.xjc.Plugin: Provider "MyPlugin" could not be instantiated: java.lang.ClassCastException.

Is there already a solution available? I have searched on the jaxb website and found, that this problem exists for approximately one year (see e.g. here http://www.nabble.com/Re:-Making-XJC-plugins-work-with-JAXB-as-endorsed-...)

Here is also a solution posted:
http://weblogs.java.net/blog/kohsuke/archive/2007/02/running_jaxbws.html

But unfortunatelly it does not work, although I have downloaded the latest nightly build snapshot of jaxb (kohsuke on Wednesday, March 12, 2008 at 11:47:43 AM).

I would be very thankful if someone could help me fixing this problem.

Regards,
Arthur

arthur83
Offline
Joined: 2007-06-29
Points: 0

Hi,

I have found the following workaround:
the package-name of the plugin must have the prefix "com.sun.tools.xjc.addon". In my case it is "com.sun.tools.xjc.addon.visitor".
Then it woks, at least under JDK 1.6.0.0_05.

I have tried other package-names, but it does not work. I think there is a problem with the classloader of java. Maybe this can help to fix this bug in JAXB.

Regards,
Arthur

wasppit
Offline
Joined: 2005-11-02
Points: 0

Hi,

Could you please elaborate on the package name workaround? Is it the package name of your class?

I'm getting ClassCastExceptions for my XJC plugins and nothing seems to help. Mind you, I'm using Metro 1.2 + Java 6, Maven 2.


I must say I'm quite frustrated by the whole Metro/JaxWS/Jaxb + Java 6 endorsed story.
I'm beginning to think that bundling the whole contraption into the JDK was a bad idea to begin with - the first thing people do is to try to override to a decent version anyway.

alski
Offline
Joined: 2005-04-05
Points: 0

Woah. Thanks for the heads up. Sure enough, this was my problem as well. I went so far as to patch the jaxws-maven-plugin to try and track down the ClassCastException seen because of the two class loaders. This is hideous to say the least!

snajper
Offline
Joined: 2004-10-01
Points: 0

Hi,
I just tried your plugin and it works for me with JAXB 2.0.5 and 2.1.6, with both command line and ant task invocation. See attached project. With JAXB_HOME leading to JAXB dir, and invoking 'ant tryxjcplugin' I get following output (I added some System.out to your plugin implementation):

Snajpers-MacPro:XJCTestProject snajper$ ant tryxjcplugin
Buildfile: build.xml

tryxjcplugin:
[xjc] Consider using /
so that XJC won't do unnecessary compilation
[xjc] Compiling file:/Users/snajper/NetBeansProjects/XJCTestProject/schema1.xsd

[xjc] Run Method successully invoked!

[xjc] Writing output to /Users/snajper/NetBeansProjects/XJCTestProject/xjcout

BUILD SUCCESSFUL
Total time: 0 seconds

So would you please describe your environment more closely? JDK used, jaxb version used, how do you invoke xjc ... best if you could change the attached project to reproduce your problem and send it back to me so that I can try it? Thanks,
MartinG

snajper
Offline
Joined: 2004-10-01
Points: 0

I just got to try this on JDK6u5, and I see it is failing. Looking at it.

sysinsight
Offline
Joined: 2003-07-02
Points: 0

It looks like JDK6 comes with JAXB2.0 bundled ... Probably you need to add JAXB2.1 API as endorsed jar to override JAXB2.0. (http://java.sun.com/javase/6/docs/technotes/guides/standards/)

cybersliver
Offline
Joined: 2004-10-17
Points: 0

I added the JAXB-2.1 libraries as endorsed in both cases, when compiling the plugin and when running the XJC ant task.

The same issue occurs.
Unfortunately I'm unable to reproduce the exact same issue, since I had to rebuild by eclipse project, resulting in ant not running at all...