Saturday, October 20, 2012


Create Web Service in Java Using Apache Axis2 and Eclipse

Web services are application components which communicate using open protocols. Using Web Services we can publish our application's functions to everyone. This tutorial provides step by step instructions to develop Web Services using Axis2 Web Services / SOAP / WSDL engine and Eclipse IDE. Let's start.

1. Setup the Development Environment


1.1. First you need to set up the development environment. Following things are needed if you want to create Web Services using Axis2 and Eclipse IDE. 
Some Eclipse versions have compatibility issues with Axis2. This tutorial is tested with Apache Axis2 1.5.2, Eclipse Helios and Apache Tomcat 6.

1) Apache Axis2 Binary Distribution - Download 
2) Apache Axis2 WAR Distribution - Download 
3) Apache Tomcat - Download 
4) Eclipse IDE – Download 
5) Java installed in your Computer – Download 

1.2. Then you have to set the environment variables for Java and Tomcat. There following variables should be added.


?
1
2
3
JAVA_HOME :- Set the value to jdk directory (e.g. C:\Program Files\Java\jdk1.6.0_21)
TOMCAT_HOME :- Set the value to top level directory of your Tomcat install (e.g. D:\programs\apache-tomcat-6.0.29)
PATH :- Set the value to bin directory of your jdk (e.g. C:\Program Files\Java\jdk1.6.0_21\bin)


1.3. Now you have to add runtime environment to eclipse. There go to Windows –-> Preferences and Select the Server --> Runtime Environments.

There select Apache Tomcat v6.0 and in the next window browse your Apache installation directory and click finish.

1.4. Then click on the Web Service –-> Axis2 Preferences and browse the top level directory of Apache Axis2 Binary Distribution.

2. Creating the Web Service Using Bottom-Up Approach


2.1 First create a new Dynamic Web Project (File --> New –-> Other…) and choose Web --> Dynamic Web Project.

2.2 Set Apache Tomcat as the Target Runtime and click Modify to install Axis2 Web Services project facet.

2.3 Select Axis2 Web Services

2.4 Click OK and then Next. There you can choose folders and click Finish when you are done.

3. Create Web Service Class


Now you can create a Java class that you would want to expose as a Web Service. I’m going to create new class called FirstWebService and create public method called addTwoNumbers which takes two integers as input and return the addition of them.

3.1 Right Click on MyFirstWebService in Project Explorer and select New –-> Class and give suitable package name and class name. I have given com.sencide as package name and FirstWebService as class name.


?
1
2
3
4
5
6
package com.sencide;
public class FirstWebService {
 public int addTwoNumbers(int firstNumber, int secondNumber){
  return firstNumber + secondNumber;
 }
}


3.2 Then, select File --> New –-> Other and choose Web Service.

3.3 Select the FirstWebService class as service implementation and to make sure that the Configuration is setup correctly click on Server runtime.

3.4 There set the Web Service runtime as Axis2 (Default one is Axis) and click Ok.

3.5 Click Next and make sure Generate a default service.xml file is selected. 

3.6 Click Next and Start the Server and after server is started you can Finish if you do not want to publish the Web service to a test UDDI repository.

You can go to http://localhost:8888/MyFirstWebService/services/listServices to see your running service which is deployed by Axis2. You can see the WSDL by clicking the link FirstWebService.

We have to use Eclipse every time when we want to run the service if we do not create .aar (Axis Archive) file and deploy it to the server. So let’s see how we can create it.

4. Create .aar (Axis Archive) file and Deploying Service


4.1 In your eclipse workspace and go to MyFirstWebService folder and there you can find our web service inside services folder. Go to that directory using command prompt and give following command. It will create the FirstWebService.aar file there.


?
1
jar cvf FirstWebService.aar com META-INF





4.2 Then copy the axis2.war file you can find inside the Apache Axis2 WAR Distribution (You downloaded this at the first step) to the webapps directory of Apache Tomcat. Now stop the Apache Tomcat server which is running on Eclipse IDE. 

4.3 Using command prompt start the Apache Tomcat (Go to bin directory and run the file startup.bat). Now there will be new directory called axis2 inside the webapps directory. Now if you go to the http://localhost:8080/axis2/ you can see the home page of Axis2 Web Application.

4.4 Then click the link Administration and login using username : admin and password : axis2. There you can see upload service link on top left and there you can upload the created FirstWebService.aar file. This is equal to manually copping the FirstWebService.aar to webapps\axis2\WEB-INF\services directory.

4.5 Now when you list the services by going to http://localhost:8080/axis2/services/listServices you should be able to see our newly added service.

5. Creating a Web service client


5.1 Select File --> New --> Other… and choose Web Service Client

5.2 Set he newly created Axis2 Web service (http://localhost:8080/axis2/services/FirstWebService?wsdl) as the Service definition. Then configure the Server runtime as previously and click finish. 

5.3 This will generate two new classes called FirstWebServiceStub.java and FirstWebServiceCallbackHandler.java. Now we can create test class for client and use our web service. Create new class called TestClient.java and paste following code.


?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.sencide;

import java.rmi.RemoteException;
import com.sencide.FirstWebServiceStub.AddTwoNumbers;
import com.sencide.FirstWebServiceStub.AddTwoNumbersResponse;

public class TestClient {

 public static void main(String[] args) throws RemoteException {
   
  FirstWebServiceStub stub = new FirstWebServiceStub();
  AddTwoNumbers atn = new AddTwoNumbers();
  atn.setFirstNumber(5);
  atn.setSecondNumber(7);
  AddTwoNumbersResponse res = stub.addTwoNumbers(atn);
  System.out.println(res.get_return());
   
 }
}


Now you can run the above code as java application and you will get the output as 12 since we are adding 7 and 5. 
Download the Eclipse project of the above example (Password:sara)

SourceLink:ArticleSource

Linux and eclipse Trouble shoot


Sub Eclipse - problem :
 we will get an error like as follow 
Failed to load JavaHL Library.
These are the errors that were encountered:
no libsvnjavahl-1 in java.library.path
no svnjavahl-1 in java.library.path
no svnjavahl in java.library.path
java.library.path = /usr/java/jdk1.6.0_30/jre/lib/i386/client:/usr/java/jdk1.6.0_30/jre/lib/i386::/usr/java/packages/lib/i386:/lib:/usr/lib
To resolve the above problem 
   $ yum install subversion-javahl

Installing java in Ubuntu Linux:


There is a PPA (Personal Package Archives) made available by Roberto Ferramosca. To add this PPA, run the following command from the command line:

1       $ sudo add-apt-repository ppa:ferramroberto/java
2       $ sudo apt-get update

You can now install the JDK with the following command:

1       $ sudo apt-get install sun-java6-jdk

I you’d like to install the JRE or the Java Plugin along with the JDK, use the following:

1       sudo apt-get install sun-java6-jdk sun-java6-jre sun-java6-plugin sun-java6-fonts

You must now set the Sun JDK as the default. You can see how to achieve this in a previous post Configuring Java on Kubuntu 10.10.

Friday, October 19, 2012

Run Eclipse In Linux


1. Install Eclipse in Linux:
Download the Eclipse :
for 32-bit :Eclipse_32-bit
For 64- bit :Eclipse_64-bit
For more info :http://www.eclipse.org/downloads/
2. Extract the eclipse-jee-indigo-sr2.xx.tar.gz to your desired directory (Eg:/home/username/)
3. open “eclipse.ini” file and change/add  vm argument parameter as follow:

-vmargs
-Dorg.eclipse.swt.browser.UseWebKitGTK=true (new)
-Dosgi.requiredJavaVersion=1.5
-XX:MaxPermSize=512m
-Xms256m
-Xmx512m

4. Save the file
5. Open terminal
username@localhost$:
6. login as root user
$su
$ **********(Password)
7. type the following command in terminal, Create the reference of .so file to the current user
$ ln -s /usr/lib/libwebkitgtk-1.0.so.0 /usr/lib/libwebkit-1.0.so.2
8. Now click on eclipse icon to run it.

Note: Please make sure that JDK should be installed in your system.

Struts 2 with Eclipse

Best Article to Learn struts2.x application development in eclipse IDE avaliable here :Struts 2.x + Eclipse