java.lang.LinkageError: JAXB 2.1 API is being loaded from the bootstrap classloader with JAX-WS web service

6.7k views Asked by At

I know this is the very basic example of jax-ws web services but I am not able to resolve this error. I am following this tutorial http://www.mkyong.com/webservices/jax-ws/deploy-jax-ws-web-services-on-tomcat/ and made the war file using build.xml using ant. But when i copy paste this war file to apache tomcat's webapps directory, it gets deployed but this url localhost:8084/HelloWorld/hello gives Error 404 (I have apache tomcat running at port no 8084). I have included the contents of the war file below:

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, 
Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_3.dtd">

<web-app>
    <listener>
        <listener-class>
                com.sun.xml.ws.transport.http.servlet.WSServletContextListener
        </listener-class>
    </listener>
    <servlet>
        <servlet-name>hello</servlet-name>
        <servlet-class>
            com.sun.xml.ws.transport.http.servlet.WSServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>hello</servlet-name>
        <url-pattern>/hello</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>120</session-timeout>
    </session-config>
</web-app>

sun-jaxws.xml

<?xml version="1.0" encoding="UTF-8"?>
<endpoints
  xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime"
  version="2.0">
  <endpoint
      name="HelloWorld"
      implementation="com.mkyong.ws.HelloWorldImpl"
      url-pattern="/hello"/>
</endpoints>

build.xml

<project name="HelloWorld" default="dist" basedir=".">
    <description>
        Web Services build file
    </description>
  <!-- set global properties for this build -->
  <property name="src" location="src"/>
  <property name="build" location="build"/>
  <property name="dist"  location="dist"/>
  <property name="webcontent"  location="WebContent"/>

  <target name="init">
        <!-- Create the time stamp -->
        <tstamp/>
        <!-- Create the build directory structure used by compile -->
        <mkdir dir="${build}"/>
  </target>

  <target name="compile" depends="init"
    description="compile the source " >
        <!-- Compile the java code from ${src} into ${build} -->
        <javac srcdir="${src}" destdir="${build}"/>
  </target>

  <target name="war" depends="compile"
    description="generate the distribution war" >

    <!-- Create the war distribution directory -->
    <mkdir dir="${dist}/war"/>

    <!-- Follow standard WAR structure -->
    <copydir dest="${dist}/war/build/WEB-INF/" src="${webcontent}/WEB-INF/" />
    <copydir dest="${dist}/war/build/WEB-INF/classes/" src="${build}" />

    <jar jarfile="${dist}/war/HelloWorld-${DSTAMP}.war" basedir="${dist}/war/build/"/>
  </target>

</project>

HelloWorld.java

package com.mkyong.ws;

import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

//Service Endpoint Interface
@WebService
@SOAPBinding(style = Style.RPC)
public interface HelloWorld{

    @WebMethod String getHelloWorldAsString();

}

HelloWorldImpl.java

package com.mkyong.ws;

import javax.jws.WebService;

//Service Implementation Bean

@WebService(endpointInterface = "com.mkyong.ws.HelloWorld")
public class HelloWorldImpl implements HelloWorld{

    @Override
    public String getHelloWorldAsString() {
        return "Hello World JAX-WS";
    }
}

Please point out where I am going wrong

Edit: I got the following exception in Tomcat Log:

SEVERE: ContainerBase.addChild: start: 
org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/HelloWorld]]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:633)
    at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1105)
    at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1664)
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
    at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NoClassDefFoundError: javax/servlet/ServletContainerInitializer
    at java.lang.ClassLoader.findBootstrapClass(Native Method)
    at java.lang.ClassLoader.findBootstrapClass0(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1629)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
    at java.lang.ClassLoader.loadClassInternal(Unknown Source)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    at org.apache.catalina.startup.ContextConfig.getServletContainerInitializer(ContextConfig.java:1655)
    at org.apache.catalina.startup.ContextConfig.processServletContainerInitializers(ContextConfig.java:1565)
    at org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1273)
    at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:878)
    at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:369)
    at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
    at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5173)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    ... 11 more
Nov 4, 2012 5:25:35 PM org.apache.catalina.startup.HostConfig deployDirectory
SEVERE: Error deploying web application directory F:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\HelloWorld
java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/HelloWorld]]
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:904)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:633)
    at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1105)
    at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1664)
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
    at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
2

There are 2 answers

0
Yunus_pat On

If you are using JDK version 6 or lower. upgrade it. I had faced same issue, after upgrading JDK serion to 7 it gone.

4
kolossus On

You're experiencing this error as a result of a discrepancy between the version of the libraries that your program needs Vs the version that is shipped by default with your JDK. You're running a JAX-WS 2.2 (requiring a JAXB version of 2.2 minimum) webservice example but your JDK 6 and your tomcat installation ship with JAXB version 2.1, resulting in the exception you have.

To resolve this, use the JDK's endorsed standards override mechanism that essentially allows you to override default libraries/jars that are packaged as part of a vanilla installation of the JDK with newer versions of same.

  1. Download the latest copies of the JAXB api and JAX-WS libraries and place in the <JDK_HOME>\lib\endorsed path, where <JDK_HOME> refers to the installation folder of your JDK (I'm presuming windows here). If the endorsed folder doesn't exist in that path, create it and place the fresh copy there.

  2. Take the same downloads an place in your <TOMCAT_HOME>\endorsed\ path for your tomcat installation. As before, if endorsed doesn't exist, create it.

You're good to go