Issue
We have developed a custom login pre-action module where we want to call an external SOAP web service, this module was working on older Liferay versions.
After upgrading to Java 11, if we try calling this external SOAP web service, we are getting errors related to javax.xml.ws.spi.Provider or com.sun.xml.ws.spi.ProviderImpl
The root cause of the issue is the deprecation of JAX-WS (java.xml.ws) in the Java 11 JVM.
To execute the code in Java 11, we have tried several options having the following errors:
-
Using javax.xml.ws:jaxws-api dependencies:
2023-08-14 11:17:15.934 ERROR [http-nio-8080-exec-5][CustomLoginPreAction:189] Error: javax.xml.ws.WebServiceException: Error while searching for service [javax.xml.ws.spi.Provider]
javax.xml.ws.WebServiceException: Error while searching for service [javax.xml.ws.spi.Provider]
at javax.xml.ws.spi.FactoryFinder$1.createException(FactoryFinder.java:61)
at javax.xml.ws.spi.FactoryFinder$1.createException(FactoryFinder.java:58)
at javax.xml.ws.spi.ServiceLoaderUtil.firstByServiceLoader(ServiceLoaderUtil.java:70)
at javax.xml.ws.spi.FactoryFinder.find(FactoryFinder.java:89)
at javax.xml.ws.spi.Provider.provider(Provider.java:96)
at javax.xml.ws.Service.(Service.java:112)
[...ommited lines...]
Caused by: java.util.ServiceConfigurationError: javax.xml.ws.spi.Provider: com.liferay.jaxws.osgi.bridge.Provider not a subtype
at java.base/java.util.ServiceLoader.fail(ServiceLoader.java:589)
at java.base/java.util.ServiceLoader$LazyClassPathLookupIterator.hasNextService(ServiceLoader.java:1237)
at java.base/java.util.ServiceLoader$LazyClassPathLookupIterator.hasNext(ServiceLoader.java:1265)
at java.base/java.util.ServiceLoader$2.hasNext(ServiceLoader.java:1300)
at java.base/java.util.ServiceLoader$3.hasNext(ServiceLoader.java:1385)
at javax.xml.ws.spi.ServiceLoaderUtil.firstByServiceLoader(ServiceLoaderUtil.java:63)
... 106 more
- Using jakarta.xml.ws:jakarta.xml.ws-api dependencies:
2023-08-13 17:31:06.035 ERROR [http-nio-8080-exec-10][CustomLoginPreAction:190] Error: jakarta.xml.ws.WebServiceException: Provider com.sun.xml.ws.spi.ProviderImpl not found
jakarta.xml.ws.WebServiceException: Provider com.sun.xml.ws.spi.ProviderImpl not found
at jakarta.xml.ws.spi.FactoryFinder$1.createException(FactoryFinder.java:35)
at jakarta.xml.ws.spi.FactoryFinder$1.createException(FactoryFinder.java:32)
at jakarta.xml.ws.spi.ServiceLoaderUtil.newInstance(ServiceLoaderUtil.java:76)
at jakarta.xml.ws.spi.FactoryFinder.find(FactoryFinder.java:110)
at jakarta.xml.ws.spi.Provider.provider(Provider.java:64)
at jakarta.xml.ws.Service.(Service.java:82)
[...ommited lines...]
Caused by: java.lang.ClassNotFoundException: com.sun.xml.ws.spi.ProviderImpl
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1412)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1220)
at com.liferay.shielded.container.internal.ShieldedContainerClassLoader.findClass(ShieldedContainerClassLoader.java:79)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:589)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
at jakarta.xml.ws.spi.ServiceLoaderUtil.nullSafeLoadClass(ServiceLoaderUtil.java:62)
at jakarta.xml.ws.spi.ServiceLoaderUtil.safeLoadClass(ServiceLoaderUtil.java:97)
at jakarta.xml.ws.spi.ServiceLoaderUtil.newInstance(ServiceLoaderUtil.java:73)
... 163 more
Environment
- Liferay DXP 7.2
- Liferay DXP 7.3
- Liferay DXP 7.4
Resolution
After upgrading to Java 11, you have to use the jakarta.xml.ws:jakarta.xml.ws-api dependencies, but to avoid the ClassNotFoundException error you have to:
Fix module dependencies at compilation time
Add an additional dependence to com.sun.xml.ws:jaxws-rt in order to include the jaxws-rt.jar library.
There are several examples of how to replace java.xml.ws (JAX-WS) dependency with jakarta.xml.ws + com.sun.xml.ws after upgrading to Java 11, see:
- https://eclipse-ee4j.github.io/metro-jax-ws/
- https://stackoverflow.com/questions/48204141/replacements-for-deprecated-jpms-modules-with-java-ee-apis, check the "JAX-WS (java.xml.ws)" section
- https://www.baeldung.com/java-soap-web-service#jdk11maven-wsimport
Fix classloader problems at execution time
wouldn't be found .
In order to avoid this, you can make a change to the class loader of the current thread before calling the web service and restore it after the call, you have an example below:
Thread currentThread = Thread.currentThread();
ClassLoader threadClassLoader = currentThread.getContextClassLoader();
ClassLoader correctClassLoader = this.getClass().getClassLoader();
try {
//we are changing the classloader of the current thread
currentThread.setContextClassLoader(correctClassLoader);
// ===> Insert here the custom code that calls the external web service <===
}
finally {
//we are restoring the previous classloader of the thread to avoid problems
currentThread.setContextClassLoader(threadClassLoader);
}