Search code examples
javaweb-servicessoapjax-ws

Unable to create Provider: class com.sun.xml.ws.spi.ProviderImpl cannot be cast to class javax.xml.ws.spi.Provider


I extracted a jar from a wsdl using the wsimport command (JDK 1.8).

Now, I'm trying to use that jar to execute a specific web service and it shows me this error:

Exception in thread "main" javax.xml.ws.WebServiceException: Unable to create Provider: class com.sun.xml.ws.spi.ProviderImpl cannot be cast to class javax.xml.ws.spi.Provider (com.sun.xml.ws.spi.ProviderImpl and javax.xml.ws.spi.Provider are in unnamed module of loader 'app')
    at javax.xml.ws.spi.Provider.provider(Provider.java:88)
    at javax.xml.ws.Service.<init>(Service.java:56)
    at com.Service01.<init>(Service01.java:42)
    at com.kyc.MainApp.main(MainApp.java:45)

Here are the dependencies that I have used in the pom.xml:

        <dependency>
            <groupId>javax.xml.soap</groupId>
            <artifactId>javax.xml.soap-api</artifactId>
            <version>1.4.0</version>
        </dependency>
        <dependency>
            <groupId>jakarta.xml.soap</groupId>
            <artifactId>jakarta.xml.soap-api</artifactId>
            <version>3.0.0</version>
        </dependency>
        <dependency>
            <groupId>javax.xml.ws</groupId>
            <artifactId>jaxws-api</artifactId>
            <version>2.1</version>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.ws</groupId>
            <artifactId>rt</artifactId>
            <version>4.0.1</version>
        </dependency>

        <dependency>
            <groupId>com.sun.xml.ws</groupId>
            <artifactId>jaxws-rt</artifactId>
            <version>4.0.1</version>
        </dependency>
        <dependency>
            <groupId>jakarta.xml.ws</groupId>
            <artifactId>jakarta.xml.ws-api</artifactId>
            <version>4.0.0</version>
        </dependency>
        <dependency>
            <groupId>WSIMPOTJAR</groupId>
            <artifactId>WSIMPOTJAR</artifactId>
            <version>1.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.3.1</version>
        </dependency>

        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-impl</artifactId>
            <version>2.2.5-b10</version>
        </dependency>

PS: I have tried these solutions below but neither one of them worked.


Solution

  • The things that needs to be cleaned up in the pom.xml.

    • Please remove all the jakarta dependencies as your code is using javax libraries.

    • Found out that jaxws-api version 2.1 (to be specific, versions <= 2.3.x) is not working with the jaxws-rt versions > 2.3.x if you are running your code on JDK versions > 1.8.

    I have added scenarios on what issues I have encountered on JDK 17:

    • jaxws-api <= 2.1 and jaxws-rt > 2.3.x

    This error will come:

    Exception in thread "main" javax.xml.ws.WebServiceException: Unable to create Provider: class com.sun.xml.ws.spi.ProviderImpl cannot be cast to class javax.xml.ws.spi.Provider (com.sun.xml.ws.spi.ProviderImpl and javax.xml.ws.spi.Provider are in unnamed module of loader 'app')
    
    • jaxws-api >= 2.2.x and <= 2.3.x and jaxws-rt > 2.3.x

    This error will come:

    Caused by: java.lang.ClassNotFoundException: com.sun.xml.internal.ws.spi.ProviderImpl
    

    Note: I saw that this issue only happens when you are running the code on JDK version > 1.8.

    Better run your code on JDK 1.8 only. Then you will not get the above issues.

    Or

    Otherwise, you stay within 2.3.x version for jaxws-rt for now if you want to run the code on higher JDKs.


    Solution:

    I have adjusted the POM to solve the issue (for JDK >= 1.8):

        <dependency>
            <groupId>javax.xml.soap</groupId>
            <artifactId>javax.xml.soap-api</artifactId>
            <version>1.4.0</version>
        </dependency>
        <dependency>
            <groupId>javax.xml.ws</groupId>
            <artifactId>jaxws-api</artifactId>
            <version>2.3.1</version>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.ws</groupId>
            <artifactId>jaxws-rt</artifactId>
            <version>2.3.7</version>
        </dependency>
        <dependency>
            <groupId>WSIMPOTJAR</groupId>
            <artifactId>WSIMPOTJAR</artifactId>
            <version>1.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.3.1</version>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-impl</artifactId>
            <version>2.3.9</version>
        </dependency>
    

    This should work.