
If you are working with SOAP web services in Java, you may have encountered the error “Package javax.jws does not exist”. This error occurs because the package javax.jws
is not included in the standard Java Development Kit (JDK) starting from Java 9. This package is part of the Java API for XML Web Services (JAX-WS), which is used to create SOAP web services in Java.
In this article, we will guide you through the steps to resolve this issue.
To fix the "Package javax.jws does not exist" error in Java for SOAP web services, you need to add the JAX-WS reference implementation as a dependency to your project. This can be done by including the Maven dependency com.sun.xml.ws:jaxws-ri:2.3.0
in your pom.xml
file. After adding the dependency, you will be able to import the necessary classes from the javax.jws
package and continue developing SOAP web services in Java.
Understanding the Issue
The javax.jws
package contains annotations that are used to define web service endpoints, operations, and parameters. These annotations are used by the JAX-WS runtime to generate the necessary code to create and consume SOAP web services.
Starting from Java 9, the JAX-WS API and its related technologies have been removed from the JDK. They are now part of the Java EE platform, which is not included in the standard JDK distribution. This is why you are seeing the “Package javax.jws does not exist” error.
Solution: Adding JAX-WS Reference Implementation
To resolve this issue, you can use the reference implementation of JAX-WS, which provides the necessary dependencies. The reference implementation is available as a Maven dependency that you can add to your project.
Adding the Maven Dependency
To add the JAX-WS reference implementation to your project, include the following Maven dependency in your pom.xml
file:
<dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-ri</artifactId>
<version>2.3.0</version>
<type>pom</type>
</dependency>
Here’s what each element in the dependency means:
groupId
: The group that the dependency belongs to. In this case, it’scom.sun.xml.ws
.artifactId
: The ID of the artifact. In this case, it’sjaxws-ri
.version
: The version of the artifact. In this case, it’s2.3.0
. You should update this to the latest version if a newer version is available.type
: The type of the artifact. In this case, it’spom
, which means that this is a Project Object Model file that contains project information and configuration details used by Maven to build the project.
Using the javax.jws
Package
After adding the Maven dependency, you should be able to import the necessary classes from the javax.jws
package in your code:
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
@WebService
@SOAPBinding(style=SOAPBinding.Style.RPC)
public class Hello {
@WebMethod
public String sayHello(@WebParam(name="name") String name) {
return "Hello " + name;
}
}
In this code:
@WebService
is an annotation that marks the class as a web service endpoint.@SOAPBinding(style=SOAPBinding.Style.RPC)
is an annotation that specifies the SOAP binding style. In this case, it’s RPC (Remote Procedure Call).@WebMethod
is an annotation that marks a method as a web service operation.@WebParam(name="name")
is an annotation that specifies the name of a web service operation parameter.
By using the JAX-WS reference implementation, you can continue developing SOAP web services in Java, even if you are using Java 9 or later.
Conclusion
The removal of the JAX-WS API from the JDK starting from Java 9 has caused some confusion among developers. However, the solution is relatively simple: just add the JAX-WS reference implementation as a dependency to your project.
We hope this article has helped you understand how to fix the “Package javax.jws does not exist” error in Java for SOAP web services. If you have any questions or comments, please feel free to leave them below.
You are getting this error because starting from Java 9, the javax.jws
package is not included in the standard JDK distribution. It has been removed from the JDK and is now part of the Java EE platform.
To resolve this error, you can add the JAX-WS reference implementation as a Maven dependency in your project. This provides the necessary dependencies for the javax.jws
package.