Sunday, May 25, 2014

Java WebServices Part 1 - Without a Server calling WebService Program

Posted by Sharath Chandra
Hi Friends, 

Recently I learnt a topic that without Server also we can write a WebService program and we can call it from localhost. For this we need Java 6 (as this feature is applicable since Java 6 only) 

Here I will explain you how to write a Java Web Service Program without a Server. For this we need to do 4 Steps. 

#Server End (though you can use same machine as server)
1) Create a Remote Logic (This logic needs to execute at Client Side as WebService invoke) 
2) Publishing Web Service 

#Client End (though you can use same machine as client as well) 
3) Create Web Service classes using wsimport command. 
4) Invoke the Web Service from Client Logic 

Though it seems a bit long but its very easy. Here you go step-by-step process.  

1) Create a Remote Logic 

Here in this step we will write a simple logic and make this logic as "Web Service" which can be called from anywhere (after publishing this logic!) 

In my example I wanted to write a Calculating Interest earning program. So I wrote like this 



import javax.jws.WebService;

 * @Author Sharath Chandra

public class CalculateInterest {

* @param principle 
* @param interestRate
* @param numberOfMonths

* @return double interestAmount = (principle * interestRate * 
                       numberOfMonths) / 100;

* @author Sharath

public double getInterest(double principle, double interestRate,
double numberOfMonths) {
double interestAmount = (principle * interestRate * numberOfMonths) / 100;
return interestAmount;



Here in this class getInterest(..) method acts as a WebService call as the class level annotation marked as @WebSerivce (javax.jws.WebService class ) 

Now we will move to second step - for publishing this Web Service. 

2) Publishing Web Service 



 * @author Sharath Chandra 
public class CalculateInterestPublisher {

private static final String WEBSERVICE_PUBLISHED = "Webservice Published";

public static void main(String[] args) {
new CalculateInterest());


This is a simple Java Program where it will publishes the WebServices using EndPoint class from package. 

This EndPoint.publish(..) method publishes the WebService call - this publish(..) method requires a URL and the object of WebService class) 

In the above example :

http://localhost:9839/Shar/CalcIntrst?wsdl (?wsdl we need to append at the end of the publish url so that we can get wsdl file) 

- localhost: as you are hosting the remote logic also in your local only. If you are hosting this logic in different system and trying to access this logic from another machine - at that time you have to publish it with an IP Address 
- 9839: here I took a random port number, on running of this java program it will tell if the port is already being used by another program. At that time you need to change the port number accordingly. 
/Shar/CalcIntrst: a random string I named it for my Publisher URL. You may give anything you want here to make it simple. 

On Successful run of this Publisher program it will publish the WebService and you may check whether this Web Service working or not by acceessing the publish url itself. 

If the WebService is working then it will load as an XML file mentioning all the details of your Web Service class and methods..etc. 

Make sure that this publisher program should be in running mode then only client-side programs can be use. (In other terms after execution of this Publisher java class don't kill that java thread / assume it as server and make sure that it run) 

This will conclude the Server side Logic.


3) Create Web Service classes using wsimport command. 

In order to make use of the Web Service call we need to create webservices classes - so for that we need to do run this command in Command Prompt. 

<<PATH>>>wsimport <<wsdl URL i.e. Published URL>> -d <<Source To Save Path>> -p <<Client Side Package Pattern>> -keep 

- keep: Keeps the Source files as well at client side. 

Example URL - For above my program: 

F:\MyLearnings\StrutsPractice\Practices\src>wsimport http://localhost:9839/Shar/CalcIntrst?wsdl -d F:\MyLearnings\StrutsPractice\Practices\src\ -p

.webservice.client -keep

This will create the below files: 

  • / CalculateInterest.class
  • / CalculateIntersetService.class
  • / GetInterest.class
  • / GetInterestResponse.class
  • / ObjectFactory.class
  • / package-info.class 

After this command run - if you are using IDE then you need to refresh the folders in order to replicate these changes in your IDE as well. 

Then we can move to final step... 

4) Invoke the Web Service from Client Logic 

Here in the final step we need to write the Client Logic in order to invoke the Web Service. In other words you are getting benefited from Web Service which you created in Step 1 and 2. 



public class BankingOperations {

public static void main(String s[]) {
try {
System.out.println("Welcome to BANKING CORE OPERATIONS");
.println("Please enter the amount (Only DIGITS) to be deposit for Fixed Deposit Scheme");
BufferedReader br = new BufferedReader(new InputStreamReader(;
double principleAmount = 0.0d;

principleAmount = Double.parseDouble(br.readLine());
.println("Please enter the number of years to be deposited");

double numberOfMonths = Integer.parseInt(br.readLine());

.println("Interest Rate will be 14.5% as per guidelines.");

double interestRate = 14.5;

.println("Please wait while we calculate the interest amount");

CalculateInterestService calIntrstServc = new CalculateInterestService();
CalculateInterest calculator = calIntrstServc
System.out.println("Interest amount will be "
+ calculator.getInterest(principleAmount, interestRate,

} catch (IOException iOe) {
} catch (NumberFormatException e) {


This client logic is basic java program where it is accepting the Prinicipal amount, the number of years, interest rate from the Command Line Arguments then it will send these params to the WebService call and calculates the Interest that you are going to earn. 

CalculateInterestService calIntrstServc = new CalculateInterestService(); 

This CalculateInterestService is the generated class from wsimport command - In order to know this class you need to access the Publisher URL from browser and in that xml you have to take the WebService class name from <service> tag. 

CalculateInterest calculator = calIntrstServc .getCalculateInterestPort();

This CalculateInterest class can be known from CalculateInterestService - Remove the appeneded Service and CalculateInterest and get the object of this class from CalculateInterestService.getCalculateInterestPort().. method. 

Then you can send the input to webservice and it calculates from the remote logic. 


Please enter the amount (Only DIGITS) to be deposit for Fixed Deposit Scheme
Please enter the number of years to be deposited
Interest Rate will be 14.5% as per guidelines.
Please wait while we calculate the interest amount

Interest amount will be 7250.0

For any doubts / explanations contact me [email protected] 

Here I wanted to Thank my Colleague and Guru Mr. Bahniman for this explanation to me.