Java – Creating a Simple JAX-WS WebService in Eclipse

eclipsejavajax-wsweb-services

I'm trying to create a simple web service in eclipse. First i created an empty java project and added the three following files in the src folder

  1. Greeting.java
package com.alfaisaliah;

import javax.jws.WebService;
import javax.jws.WebMethod;

@WebService
public interface Greeting {
    @WebMethod
    String sayHello(String name);
}
  1. GreetingImp.java
package com.alfaisaliah;

import javax.jws.WebService;

@WebService(endpointInterface="com.alfaisaliah.Greeting")
public class GreetingImp implements Greeting {

    @Override
    public String sayHello(String name) {
        return "Hello " + name;
    }
}
  1. WSPublisher
package com.alfaisaliah;

import javax.xml.ws.Endpoint;

public class WSPublisher {
    public static void main(String[] args){
        Endpoint.publish("http://localhost:8081/WS/Greeting", new GreetingImp());
    }
}

The tutorial I'm following doesn't specify any server to run the web service on! I'm wondering if I need to specify any server. I already have Tomcat v5.5 but am not using it in this example. Whenever I run this project as a java project I get some kind of error. Can anyone please help me identify where my problem is trying to run the web service. Here is the output of the eclipse console

Feb 26, 2012 12:01:00 PM com.sun.xml.internal.ws.model.RuntimeModeler getRequestWrapperClass
INFO: Dynamically creating request wrapper Class com.alfaisaliah.jaxws.SayHello

Feb 26, 2012 12:01:00 PM com.sun.xml.internal.ws.model.RuntimeModeler getResponseWrapperClass
INFO: Dynamically creating response wrapper bean Class com.alfaisaliah.jaxws.SayHelloResponse

Also when I run the project again it says that the address is already in use

Feb 26, 2012 12:01:00 PM com.sun.xml.internal.ws.model.RuntimeModeler getRequestWrapperClass
INFO: Dynamically creating request wrapper Class com.alfaisaliah.jaxws.SayHello

Feb 26, 2012 12:01:00 PM com.sun.xml.internal.ws.model.RuntimeModeler getResponseWrapperClass
INFO: Dynamically creating response wrapper bean Class com.alfaisaliah.jaxws.SayHelloResponse
Exception in thread "main" com.sun.xml.internal.ws.server.ServerRtException: Server Runtime Error: java.net.BindException: Address already in use

I would appreciate your help guys 🙂

Best Solution

The tutorial i'm following doesn't specify any server to run the web service on! I'm wondering if I need to specify any server.

You don't need a server with this code.
Your main in:

Endpoint.publish("http://localhost:8081/WS/Greeting", new GreetingImp());  

starts a light http server under the hood (available after JKD 1.6) and it deploys your web service handling all incoming/outgoing traffic.

The problem here is that you missed a step:
You have to generate the required artifacts using the wsgen tool (available in java).

Check out here: JAX WS tutorial for
wsgen -d build -s build -classpath build helloservice.endpoint.Hello
and read about wsgen.

To be honest I don't remember how you do it via Eclipse (actually I am not sure if this can work in Eclipse automatically without you needing to run wsgen yourself) but you can run it manually and just copy the generated artifacts in your project.

As for the

Server Runtime Error: java.net.BindException: Address already in use

This is self-explanatory: Just use another port. 8081 is already used.