Build and Test a Java Web Service.


Let’s quickly write a Java Web Service, deploy and test.


OS: Windows
IDE: Eclipse Galileo
Java: Jdk 1.6
Server : JBoss

Step1: Set up your environment – Install Softwares & Configure

1. Download Jdk from the link above and extract. Set your PATH & CLASSPATH variables.

2. Download Eclipse from the link above and extract and copy the folder ‘eclipse to C:\

From within eclipse, Install new software (form help menu) and point to to install JBoss tools for eclipse. This will install JBoss 5.1 Runtime Environment server for eclipse.

3. Download JBoss Server from the link above and extract to C:\jboss-5.1.0.GA

Set JBOSS_HOME environment variable to point to installed C:\jboss-5.1.0.GA folder

4. Download and Install Metro Web Services

  1. Download “ and extract it.
  2. Copy to
  3. You need to have Apache ant installed in your system for next steps to work. if not please download latest ant from and install. Set PATH to <ANT-HOME>/bin.
  4. Edit file and change jboss510.home=C:\\jboss-5.1.0.GA
  5. comment out other jboss501.home etc in file.
  6. Form command line, go to the directory where you have and run “ant deploy-jboss510″ to patch your JBoss with Metro web services.

5. Start your JBoss from Eclipse

  1. Open Eclipse | Open Java EE perspective | Click on Servers Tab | Rt. Click | New | Server | Select JBos 5.x | Finish. Server will be added now.
  2. Double click on the server and change the timeout to a higher number 200 or so.
  3. Rt. Click on the server and Start.
  4. Check http://localhost:8080/ to see the server running.

Step2: Write your Web Service

  1. Eclipse | New | Dynamic Web Project
  2. Name your project as MyWebServices & Select JBoss as your ‘Target Runtime’ | Finish
  3. This will create the following folders into your workspace
  4. Create a package com.test withing src and create your as follows. Since you are using the JBoss Runtime in Eclipse, you don’t need to import any Jars.
  5. package com.test;
    import javax.jws.WebMethod;
    import javax.jws.WebParam;
    import javax.jws.WebService;
    import javax.jws.soap.SOAPBinding;
    @WebService(name = “TestWs”)
    style = SOAPBinding.Style.DOCUMENT,
    use = SOAPBinding.Use.LITERAL,
    parameterStyle = SOAPBinding.ParameterStyle.WRAPPED
    public class TestWs
    public String greet( @WebParam(name = “name”)
    String name )
    return “Hello” + name;

  6. Project | Build automatically and see your class file in build folder.
  7. Now open WebContent\WEB-INF\web.xml and add the following

Step3: Deploy your Web Service

Your Server is already Started.

Go to the Servers tab and Rt. click | Add and Remove | Select your Project ‘MyWebServices’ and move it to the ‘Configured’ box | Finish

This step will deploy your webservice and you will get a message in your console as follows

Buildfile: C:\eclipse\plugins\org.eclipse.jst.server.generic.jboss_1.6.1.v200904151730\buildfiles\jboss323.xml
[jar] Building jar: C:\TWorkspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\MyWebServices.war
[move] Moving 1 file to C:\jboss-5.1.0.GA\server\default\deploy

To view the WSDL follow the link http://localhost:8080/MyWebServices/TestWs?wsdl

Great! your Web Service is up and running.

Step4: Set up your WebService Client WorkSpace & Write Your Web Service Client.

  1. Create a Java Project to create your Client – Eclipse | New | Project | Java Project. Name your Project as ‘TestWsClient’
  2. Create the WebService Stubs using JBoss tool wsconsume.
    Go to C:\jboss-5.1.0.GA\bin
    Run wsconsume -k http://localhost:8080/MyWebServices/TestWs?wsdl
  3. This will generate the following Java files and their class files in C:\jboss-5.1.0.GA\bin\output\com\test directory.
  4. Create a package in your ‘TestWsClient’ project and copy these java files there.
  5. Create a package src.client and create your Client Class as follows.
    package client;

    import com.test.TestWs;
    import com.test.TestWsService;

    public class TestWsClient {
    @WebServiceRef (wsdlLocation=”http://localhost:8080/MyWebServices/TestWs?wsdl&#8221;)
    static TestWsService service;

    public static void main(String[] args) throws Exception {
    service = new TestWsService();
    TestWs test = service.getTestWsPort();
    System.out.println(test.greet(” Bob! “));

Step6: Test Your WebService

Build your project (set Project | Build Automatically)

Right Click your, Run As Java Application

Dang! it gets your service named ‘greet’ and says ‘Hello Bob!’