Saturday, April 18, 2015

Ubuntu 14.04 - Install Apache Tomcate – Set CATALINA_HOME


How to Install Apache Tomcat on Ubuntu System and set CATALINA_HOME environment variable.


Note: Verify you have jdk already installed or not ? if NOT then follow This tutorial.

There are two way to install tomcat
  1. Using apt-get
  2. Manually

Method 1 : Install Apache Tomcat with apt-get using below command

$ apt-get install tomcat7

above command will install the latest one available, for me it is apache-tomcat-7.0.59


Method 2 : Install Apache Tomcate Manually
  • First download the required version Binary Distributions/ ZIP file from apache.  
  •  
  • Once done with the download extract the file to /opt/, move to opt folder and execute below commands
     
  • 
    $ cd /opt
    
    $ sudo tar -xvzf ~/Downloads/apache-tomcat-*.tar.gz
    
    
     
  • Once extract done, you will see the apache-tomcat folder under /opt folder.

Configure CATALINA_HOME


It is always helpful to set environment variable CATALINA_HOME for tomcate, I personally recommend to set it in .bashrc.

  • open bashrc using below command.
  •  
    
    $ sudo gedit ~/.bashrc
    
    
  • add below lines at the end of the bashrc file. DONE : Save and close the file. 
  •  
    
    # environment variable for tomcate 
    export CATALINA_HOME=/opt/apache-tomcat-7.0.59
    
    # environment variable for JAVA
    export JAVA_HOME=/usr/lib/jvm/java-7-oracle
    
    
     
  • make sure you have set the environment variable properly. 
  • 
    $ $CATALINA_HOME/bin/startup.sh
    
    
    Tomcat should be started.  
    
    $ $CATALINA_HOME/bin/shutdown.sh
    
    
    Tomcat should be shutdown.



Thursday, March 19, 2015

Ubuntu 14.04 | Install Latest Eclipse | Step by Step

How to Install The Latest Eclipse in Ubuntu 14.04

1. Verify you have jdk already installed or not ? if NOT then follow This tutorial.

2. Download latest Eclipse.

  •     Check your OS type > Click on Settings ICON  > System Settings > Details > Overview
  •     Then download eclipse from Here.

3. Extract Eclipse to default location /opt/ for global use.

  •  Open terminal and run the command below to extract Eclipse to /opt folder: 

     $ cd /opt  
     $ sudo tar -zxvf ~/Downloads/eclipse-*.tar.gz  
    
    • Once extract done, you will see the eclipse folder under /opt folder.
    4. Now Create a launcher for Eclipse
    • Go to Terminal and fire below command
     sudo gedit /usr/share/applications/eclipse.desktop  
    
    • command will create and open the eclipse.desktop file with gedit text editor, add below code to the file
     [Desktop Entry]  
     Name=Eclipse 4  
     Type=Application  
     Exec=/opt/eclipse/eclipse  
     Terminal=false  
     Icon=/opt/eclipse/icon.xpm  
     Comment=Integrated Development Environment  
     NoDisplay=false  
     Categories=Development;IDE;  
     Name[en]=Eclipse  
    
    • Save and Close the editor.
    DONE !! Now go to search and type eclipse and happy coding...

    Monday, March 16, 2015

    Ubuntu 14.04 | Install Oacle JDK | Step by Step

    Ubuntu users,... this article will help you to Install Oracle JAVA 7/8 (JDK/JRE) on Ubuntu 14.04 LTS, 12.04 LTS.


    There are two ways to install JDK in ubuntu.

    1) Using PPA.
    2) Manual installation.


    1) Using PPA.

    We can use WebUpd8 PPA (this will download the required files from Oracle).

    $ sudo apt-add-repository ppa:webupd8team/java  
    $ sudo apt-get update  
    $ sudo apt-get install oracle-java7-installer  
     
    OR
    $ sudo apt-get install oracle-java8-installer

    apt-get install will install the latest version available, now verify your Installed Java version using below command


    $ java -version 
     
    java version "1.7.0_56"
    


    Now set environment variables for the installed JAVA version.

    $ sudo update-alternatives --config java
    
    
    $ sudo apt-get install oracle-java7-set-default  
    
    OR
    $ sudo apt-get install oracle-java8-set-default



    2) Manual installation.
    In a manual installation we dont actually install java. The tar.gz provided by Oracle, we just extract those files to a location we want and add them to our path.

    So here is the manual process:


    1) Download a .tar.gz from Oracle official website

    2) Extract it to somewhere (/home/chirag/Downloads/);

    3) Move the extracted folder to /usr/lib/jvm.
    (This is not mandatory but it is the default place where Java runtime software is usually installed.)

     sudo mv /path/to/jdk /usr/lib/jvm/oracle_jdk7  
    

    4) Now configure ~/.bashrc to set the environment variable manually.

     sudo gedit ~/.bashrc
    

    and add below lines to the file - Save and close.
    export J2SDKDIR=/usr/lib/jvm/oracle_jdk7  
    export J2REDIR=/usr/lib/jvm/oracle_jdk7/jre  
    export PATH=$PATH:/usr/lib/jvm/oracle_jdk7/bin:/usr/lib/jvm/oracle_jdk7/db/bin:/usr/lib/jvm/oracle_jdk7/jre/bin  
    export JAVA_HOME=/usr/lib/jvm/oracle_jdk7  
    export DERBY_HOME=/usr/lib/jvm/oracle_jdk7/db
    

    Done! You are now ready to use java.


    Thursday, February 21, 2013

    CXF Webservice example | Step by Step | Using Maven Project (A Web Application).

    CXF Overview

    Apache CXF is an open source, fully featured Web services framework, its name CXF is originated from two projects (Celtix and XFire) combined work together to Apache. CXF supports JAX-WS, Binding, DataBinding and Transport implementation, the nice feature is its embeddable Web service component.

    We look though a step-by-step configuration for maven, tomcate with CXF to start the first web service project.

    We will basically go through the following steps:
    1) Download and install the required tools.
    2) How do I configure Eclipse to develop and run web services with maven?
    3) Develop a simple Hello World Web Service?
    4) How to deploy your Web Service?
    5) How to test your Web Service?


    Download and install the required tools.


    1) JDK ( Click Here) Download the latest version of JDK.
    2) Apache Tomcat ( Click Here ) Download Binary Distributions ZIP.
    3) Maven ( Click Here ) Download LINK (Binary zip).
    4) Apache CXF ( Click Here ) Download FILE (Binary distribution) ZIP.
    5) Eclipse IDE for Java EE version ( Click Here )
    6) Eclipse Maven Plugin.
    • Start Eclipse create new Workspace.
    • Goto HELP >> Install New Software
    • Add URL ( http://m2eclipse.sonatype.org/sites/m2e ) and check "Maven Integration for Eclipse"
    • Click on Finish.

    How do I configure Eclipse to develop and run web services with maven


    1) Configure Default JDK
    Open Eclipse and follow below instructions

    • Goto Window >> Preference
    • Select Java >> Installed JREs
    • Click on "Add", select "Standard VM".
    • Click on "Directory" and select JDK Home path where you have installed JDK.
    • Finish >> Now select the JDK we have added recently and click on OK.


    2) Configure Tomcat

    • Goto Window >> Preference
    • Select Servers >> Runtime Environments
    • Click on "Add", select "Tomcate version" you have downloaded >> Next.
    • Click on Brows and provede the home directory of Tomcate.
    • Select JDK which we recently setup in above steps.
    • Click on Finish >> OK.


    3) Configure Maven Local Repository

    • Goto Window >> Preference
    • Select Maven >> User Settings
    • click on "Brows" and select "settings.xml" from conf directory of maven ( e.g. D:\apache-maven-3.0.4\conf\settings.xml).
    • Once you provide the "settings.xml" you can see the Local Repository is read only, to change the location click on "open file" link provided beside the User Settings label.
    • Now go to "settings.xml" and Un-comment the Tag localRepository and provide your own local repository, where you want all JARs will be downloaded. ( e.g. D:\MavenRepository)
    • Verify the same path in "User Settings" and click on "Update Settings". It will downlod some Jars in Local Repository.


    4) Configure CXF

    • Goto Window >> Preference
    • Select Web Services >> CXF 2.x Preferences (This option will be available in Eclipse 3.6 or above)
    • Click on "Add", we have downloaded CXF so we need to "Brows" the home directory of CXF. click on OK >> Finish.
    • Select the default CXF and click on OK.


    Finally I also need to configure Tomcate to use the CXF Runtime. By default tomcate uses the Axis so i need to change it to CXF.

    • Goto Window >> Preference
    • Select Web Services >> Server and Runtime
    • Change the "Web service runtime" to Apache CXF 2.x.
    • Click OK to finalize the setup.


    Develop a simple Hello World Web Service with Maven Project


    Lets begin, First we will create a new Maven project.


    • Goto File >> New >> Other >> Maven >> Maven Project >> Click Next.

    • In "Select project name and location" wizard, hit "Next" to continue with default values, make sure that "Create a simple project (skip archetype selection)" option is UNCHECKED.

    • In "Select an Archetype" wizard, keep default selection "All Catalogs" at the "Catalog" drop down list, After the archetypes selection area is refreshed, select the "Artifact Id" as "maven-archetype-webapp" archetype and hit Next.

    • In the "Enter a group id for the artifact" wizard, you can define the name and main package of your project. We will set the "Group Id" as "com.b4interview.ws.provider" and the "Artifact Id" as "WSProvider".

    • Hit "Finish" to exit the wizard and to create your project.


    Maven will create below folder structure.

    1) /src/main/java : contains source files for the application.
    2) /src/test/java : contains all source files for unit tests.
    3) /src/main/webapp : contains essential files for creating a valid web application. (WEB-INF/web.xml)
    4) /target : contains the compiled and packaged deliverable
    5) The "pom.xml" is the project object model (POM) file, that contains all project related configuration.


    NOTE : In above case a new maven web project is created, but still we will not be able to deploy the same on tomcat.
    There is a bit confusion here to use the same project as Eclipse web project we must add WTP facet (follow the below instruction).

    • Right click on maven project >> Build Path >> Configure Build Path...
    • Select "Project Facets" >> Convert to faceted form...
        • OR
    • Right click on maven project >> Configure >> Convert to faceted form...
    • Select "Dynamic Web Module", by selecting the same you will be able to see the link "Further configuration available...".

    • Now click on "Further configuration available..." and change the location of "Content directory" ( WebContent ) as per the maven structure.

    • Then check "CXF 2.x Web Services" in Project Facets Dialog.
    • Click OK, project structure will be as below. Now we will be able to run project as "Run on Server".



    Done! You're ready for the code. Let's code for a web service provider example.

    1) First we will create some base by creating Model and Exception class.

    Entity.java
    package com.b4interview.ws.model;

    public class Entity {
    private String id;
    private String desc;

    // getter / setter Methods.
    }

    GenericException.java

    package com.b4interview.ws.exception;

    import javax.xml.ws.WebFault;

    @WebFault(name="genericException") //Indicates a generic exception that the operation/web service method can throw.
    public class GenericException extends Exception {
    private static final long serialVersionUID = 1L;

    private GenericException(){}
    public GenericException(String message){
    super(message);
    }
    public GenericException(String message, Throwable cause){
    super(message, cause);
    }
    }
    2) Write your Service Endpoint Interface (SEI), (a java interface) that will be exposed as a webservice.

    EntityService.java

    package com.b4interview.ws.service;

    import javax.jws.WebMethod;
    import javax.jws.WebParam;
    import javax.jws.WebService;
    import com.b4interview.ws.exception.GenericException;
    import com.b4interview.ws.model.Entity;

    @WebService //Indicates its a webservice.
    public interface EntityService {

    @WebMethod(operationName="getEntityData") //Indicates a webservice method.
    public Entity getEntityData(@WebParam(name="id")String id) throws GenericException; //Indicates a parameter used in the operations, argument to be displayed on the WSDL.
    }

    3) Create a Service Implementation as a webservice.

    EntityServiceImpl.java

    package com.b4interview.ws.service.impl;

    import javax.jws.WebService;
    import com.b4interview.ws.exception.GenericException;
    import com.b4interview.ws.model.Entity;
    import com.b4interview.ws.service.EntityService;

    @WebService(endpointInterface="com.b4interview.ws.service.EntityService", serviceName="entityService")
    public class EntityServiceImpl implements EntityService {

    public Entity getEntityData(String id) throws GenericException {
    Entity entity = new Entity();
    entity.setId("123");
    entity.setDesc("Hello World, Web Services");
    return entity;
    }
    }

    4) create a beans.xml, Configuration file for a webservices.
    We will create it in a new package ( com.b4interview.ws.conf ).

    beans.xml


    5) Finally Its time to configure web.xml for CXF.

    web.xml

    6) Start the server, and access http://localhost:8080/WSProvider/services
    You should be able to see the list of services. by clicking on it, WSDL will be displayed.