Tuesday, August 21, 2012

How to deploy a war file using GlassFish

Before trying this out make sure you have installed GlassFish in your machine. Installation is just easy as downloading the zip archive from here and unzipping it to a desired location.

Creating a new domain

Open up a terminal, change directory to the GlassFish installation directory and run following.

This will enable you to use asadmin tool. Now execute the following command to create a new domain. after running this command you will probably have to give admin password and master password.
create-domain --adminport 5000 --profile developer --user admin domain2

Now open up another terminal and change directory to [glassfish-installation]/domains/ and you will see the newly created domain2 has appeared there.

Open up a browser and go to http://localhost:5000/. This will bring you the GlassFish GUI admin console. This is one place you can deploy your war file. But in this post I'm not much focusing on it, instead I will show you how to do this using the command line. But GUI lovers, for any consolation I have put some screenshots which you can follow if you prefer the GUI way.

In case you want to delete a domain use the following command.
delete-domain domain2

Starting the domain

To start domain2 run following command.
start-domain domain2

Deploying a war file

Use the following command to deploy your war file.
deploy --port 5000 --host localhost /home/pavithra/workspace/NewDemoService/WebServicesJaxWs/NewDemoService.war 

After deploying the war file I can access the WSDL file I want to access using the URL http://localhost:8080/NewDemoService/NewDemoService?WSDL

To change default 8080 (HTTP) port which specifies where the web application context roots are available for a Web browser to connect to, you can use the --instanceport parameter when creating the domain. See the following command.
create-domain --adminport 5000 --profile developer --user admin --instanceport 9000 domain2

Undeploying a war file

To undeploy NewDemoService.war file you need to use the following command. Note that here you don't have to use the full name but literal "NewDemoService".
undeploy --port 5000 --host localhost NewDemoService

Stopping a domain

To stop the domain "domain2" use the following command.
stop-domain domain2

After this if you try to deploy to this particular domain, it will complain.

Auto Deploy

To perform auto deploy, copy NewDemoService.war file in to [glassfish-installation-directory]/domains/domain2/autodeploy directory. If autodeploy succeeds, you will see NewDemoService.war_deployed has created. This will deploy your war file automatically.

Monday, August 20, 2012

Getting started with JAX-WS

JAX-WS stands for Java API for XML Web Services. It is a Java programming language API for creating web services and clients that communicate using XML. This post is a quick start for JAX-WS.

GlassFish integrated with Eclipse.

Creating the JAX-WS Web Service
  1. In Eclipse create a Dynamic Web Project called "com.eviac.blog.jaxwsproj". Make GlassFish as the Target Runtime.
  2. Create a new class called "SampleWS" in the created project. This will be the implementation class of the web service.
    package com.eviac.blog.jaxws.service;
    import javax.jws.WebMethod;
    import javax.jws.WebService;
    public class SampleWS {
     public int sum(int a, int b) {
      return a + b;
     public int multiply(int a, int b) {
      return a * b;
  3. Open a terminal and navigate to the root of the project directory. Create a directory called wsdl inside WebContent/WEB-INF/. Use the following command to create web service artifacts. Make sure your JAVA_ HOME is set properly or this command will not work. Also make sure to build the project before running this command or it will complain class not found.
    wsgen -classpath build/classes/ -wsdl -r WebContent/WEB-INF/wsdl -s src -d build/classes/ com.eviac.blog.jaxws.service.SampleWS
  4. Refresh the project to discover created artifacts. Open the created WSDL-file inside wsdl folder. Search for REPLACE_WITH_ACTUAL_URL and replace it with the web service URL: http://localhost:8080/com.eviac.blog.jaxwsproj/SampleWSService, and save the file.
  5. Deploy the project in Glassfish by right-clicking the project, click Run As -> Run on Server and select the Glassfish server.
Creating the JAX-WS client
  1. Create a Java project in eclipse called "com.eviac.blog.jaxwsclientproj". Open up a new terminal and go to the project root. Use the following command to generate the classes you need to access the web service. Here you will need to use the URL of the WSDL file.
    wsimport -s src -d bin http://localhost:8080/com.eviac.blog.jaxwsproj/SampleWSService?wsdl
  2. Create a new class called "SampleWSClient" in the project.
    package com.eviac.blog.jaxws.client;
    import javax.xml.ws.WebServiceRef;
    import com.eviac.blog.jaxws.service.SampleWS;
    import com.eviac.blog.jaxws.service.SampleWSService;
    public class SampleWSClient {
     @WebServiceRef(wsdlLocation = "http://localhost:8080/com.eviac.blog.jaxwsproj/SampleWSService?wsdl")
     private static SampleWSService Samplews;
     public static void main(String[] args) {
      SampleWSClient wsClient = new SampleWSClient();
     public void run() {
      Samplews = new SampleWSService();
      SampleWS port = Samplews.getSampleWSPort();
      System.out.println("multiplication Result= "+ port.multiply(10, 20));
      System.out.println("Addition Result= "+port.sum(10, 20));
  3. Right click on the project and click on Run As -> Java Application. This will result following.
    multiplication Result= 200
    Addition Result= 30

Sunday, August 19, 2012

Integrating GlassFish with Eclipse 3.7

This post will guide you to integrate GlassFish 3.0.1 with Eclipse 3.7.
  1. Download the zip archive for GlassFish from here.
  2. Unzip it using the following command.
    unzip glassfish-3.0.1.zip 
  3. Move GlassFish installation directory to a more suitable place.
    sudo mv glassfishv3 /opt
  4. Open Eclipse.
    Navigate to New -> Server and click on download additional server adapters link and install Oracle GlassFish server tools. Restart Eclipse, when the installation is finished.
    After restarting Eclipse, navigate to New -> server.
    Select GlassFish Server Open Source Edition 3 (Java EE 6) and click next.
    Select a working JRE environment, select the installation path of your Glassfish application server and click next.
    In this screen keep the default settings and click next.
    In this screen it lets you select existing projects to be added to the newly configured server adapter, just click Finish.
    After a successful configuration, server adapter will be appeared in the servers tab. Start the server by right-clicking on it.

Friday, August 17, 2012

Installing Tomcat 7 on Ubuntu 12.04

This post is about installing Apache Tomcat7 on Ubuntu 12.04. Before this installation make sure java is installed in your machine. You can refer this post if you haven't done that already.
  1. Download Tomcat 7 tar.gz binary distribution from here.
  2. Unpack it using following command.
    tar xvzf apache-tomcat-7.0.29.tar.gz 
  3. Move it to a more appropriate location using following command.
    sudo mv apache-tomcat-7.0.29/ /usr/share/tomcat7
  4. Open up /usr/share/tomcat7/bin/catalina.sh file using following command
    gedit /usr/share/tomcat7/bin/catalina.sh 
  5. Add following two lines in there after the first line.
  6. Open up /usr/share/tomcat7/conf/tomcat-users.xml and uncomment user and role entries there. Then add a manager-gui role and a user by adding following lines there.
    <role rolename="manager-gui"/>
    <user username="pavithra" password="tomcat" roles="manager-gui"/>
  7. Start the Tomcat server using following command.
    sudo /usr/share/tomcat7/bin/catalina.sh run
  8. Verify Tomcat installation using the following URL.
  9. To login as the manager, use the following URL, provide the relevant username and password.

Friday, August 10, 2012

Installing Maven 3.0.4 on Ubuntu 12.04

To install Apache Maven 3.0.4 on Ubuntu 12.04 use following steps
  1. Get Maven 3.0.4 binary distribution using following command
    wget http://www.gtlib.gatech.edu/pub/apache/maven/binaries/apache-maven-3.0.4-bin.tar.gz
  2. Unpack the binary distribution using following command
    tar -zxf apache-maven-3.0.4-bin.tar.gz
  3. Move uncompressed "apache-maven-3.0.4" directory to the /usr/local directory using this command
    sudo cp -R apache-maven-3.0.4 /usr/local
  4. Create a symbolic link to /usr/bin using following command
    sudo ln -s /usr/local/apache-maven-3.0.4/bin/mvn /usr/bin/mvn
  5. Verify for correct installation using following command
    mvn –version
    It should print following
    Apache Maven 3.0.4 (r1232337; 2012-01-17 14:14:56+0530)
    Maven home: /usr/local/apache-maven-3.0.4
    Java version: 1.6.0_24, vendor: Sun Microsystems Inc.
    Java home: /usr/lib/jvm/jdk-6u32/jre
    Default locale: en_US, platform encoding: UTF-8
    OS name: "linux", version: "3.2.0-27-generic", arch: "amd64", family: "unix"

Tuesday, August 7, 2012

Installing LaTeX on Ubuntu

LaTeX is a markup language for describing a document. It can also be defined as a document preparation system. LaTeX is mainly used to create technical or scientific articles, papers, reports, books or PhD thesis.

There are number of LaTeX distributions you can install on Ubuntu. One such distribution is TeX Live.
  1. To install Tex Live LaTeX distribution on ubuntu use the following command.
    sudo apt-get install texlive-full
  2. To edit LaTeX documents we need an editor. There are number of LaTeX editors you can find. I recommend Texmaker, a cross platform LaTeX editor. To install Texmaker on Ubuntu use following command.
    sudo apt-get install texmaker
  3. To open Texmaker on Ubuntu use following command.
  4. Now Let's create a simple document using Texmaker. Click on File -> New and insert following lines in the blank document.
      Hello world!
  5. Now save the document as a 'tex' file by clicking File -> Save. Compile the document clicking the arrow Quick Build.
You can learn more about LaTeX by refferring this link.

Saturday, August 4, 2012

Installing Apache Thrift on Ubuntu

In a previous post I wrote how to install Apache thrift on Windows. In this post I will guide you how to install Apache Thrift on Ubuntu 12.04.
  1. Install dependencies using the following command
    sudo apt-get install libboost-dev libboost-test-dev libboost-program-options-dev libevent-dev automake libtool flex bison pkg-config g++ libssl-dev
  2. Download tar.gz archive from the this link, extract it in your home directory using this command
    tar -xvzf thrift-0.8.0.tar.gz
  3. Change into the Thrift installation directory(the one extracted) and carry on the following command
  4. Run the following command within Thrift installation directory
  5. After that carry on the following command
    sudo make install
  6. To verify Thrift has installed properly, use the following command
    thrift -version 

Friday, August 3, 2012

Installing Apache Cassandra on Ubuntu

Apache Cassandra is a distributed, extremely scalable, highly available and fault tolerant NoSQL database initiated by facebook, later open sourced as an apache project. Cassandra data model is inspired by Google Bigtable and it's distribution model is inspired by Amazon Dynamo. If you are interested to know more about Cassandra you can refer to the paper written by Facebook.

This post will guide you how to install Cassandra on Ubuntu 12.04.
  1. Install the new updates using following commands
    sudo apt-get update
    sudo apt-get upgrade
  2. open /etc/apt/sources.list using the following command
    sudo gedit /etc/apt/sources.list
    and add the following lines to it
    deb http://www.apache.org/dist/cassandra/debian 10x main
    deb-src http://www.apache.org/dist/cassandra/debian 10x main
  3. Run update again and you will get the following error. This means you need to add the PUBLIC_KEY. In next step you will understand how to add this PUBLIC_KEY.
    GPG error: http://www.apache.org unstable Release: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 4BD736A82B5C1B00
  4. Register and add a PUBLIC_KEY key and update again, note that you may need to change the key accordingly
    gpg --keyserver wwwkeys.pgp.net --recv-keys 4BD736A82B5C1B00
    sudo apt-key add ~/.gnupg/pubring.gpg
    sudo apt-get update
  5. Install Cassandra using the following command
    sudo apt-get install cassandra
  6. Start Cassandra server using the following command
    sudo cassandra -f
    After starting the Cassandra server you will see it has started listening for thrift clients.
    INFO 12:18:29,140 Listening for thrift clients...
  7. To stop Cassandra server process first find the Process ID for Cassandra and kill it.

    To find the process ID use following command
    ps auwx | grep cassandra
    Output will be something like this. According to that 3595 is the process ID for cassandra.
    root      3595  0.0  0.0  60048  1908 pts/0    S+   12:18   0:00 sudo cassandra -f
    To kill the process use the following command
    sudo kill <pid>
    After killing the process you will see Cassandra server has stopped listening to thrift clients.
    INFO 13:04:08,663 Stop listening to thrift clients
    INFO 13:04:08,666 Waiting for messaging service to quiesce
    INFO 13:04:08,667 MessagingService shutting down server thread.
  8. Use following command to start Cassandra as a service
    sudo /etc/init.d/cassandra start
  9. Use following command to stop Cassandra service
    sudo /etc/init.d/cassandra stop
Installation will create following directories. Uses of them are mentioned within the brackets.
  • /var/lib/cassandra (data directories)
  • /var/log/cassandra (log directory)
  • /var/run/cassandra (runtime files)
  • /usr/share/cassandra (environment settings)
  • /usr/share/cassandra/lib (JAR files)
  • /usr/bin (binary files)
  • /usr/sbin
  • /etc/cassandra (configuration files)
  • /etc/init.d (service startup script)
  • /etc/security/limits.d (cassandra user limits)
  • /etc/default
Installing JNA (Java Native Access) on Linux platforms can improve Cassandra memory usage. To install JNA, download jna.jar from here and add it to /usr/share/cassandra/lib directory.

If you get the following error while you try to start a Cassandra server, means that cassandra is already running in the background somewhere. You will need to kill the process that is running in the background first. You can probably use the above mentioned stop command to stop any Cassandra servers running background.
Error: Exception thrown by the agent : java.rmi.server.ExportException: Port already in use: 7199; nested exception is: 
 java.net.BindException: Address already in use
Here I have done the packaged installation, alternatively you can install Cassandra binary tarball installation on Ubuntu. Use this link for that.


Official Package To Install On Debian(tm) (not a product of Debian(tm))

Installing Oracle java6 on Ubuntu

If you have already installed Ubuntu 12.04 you probably have realized that Sun java(oracle java) does not come prepacked with Ubuntu like it used to be , instead OpenJDK comes with it. Here is how you can install Oracle java on Ubuntu 12.04 manually.
  1. Download jdk-6u32-linux-x64.bin from this link. If you have used 32-bit Ubuntu installation, download jdk-6u32-linux-x32.bin instead.
  2. To make the downloaded bin file executable use the following command
    chmod +x jdk-6u32-linux-x64.bin
  3. To extract the bin file use the following command
  4. Using the following command create a folder called "jvm" inside /usr/lib if it is not already existing
    sudo mkdir /usr/lib/jvm
  5. Move the extracted folder into the newly created jvm folder
    sudo mv jdk1.6.0_32 /usr/lib/jvm/
  6. To install the Java source use following commands
    sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/jdk1.6.0_32/bin/javac 1
    sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk1.6.0_32/bin/java 1
    sudo update-alternatives --install /usr/bin/javaws javaws /usr/lib/jvm/jdk1.6.0_32/bin/javaws 1
  7. To make this default java
    sudo update-alternatives --config javac
    sudo update-alternatives --config java
    sudo update-alternatives --config javaws
  8. To make symlinks point to the new Java location use the following command
    ls -la /etc/alternatives/java*
  9. To verify Java has installed correctly use this command
    java -version
  10. To set JAVA_HOME variable and add to your PATH open up /etc/bash.bashrc file using following command
    sudo gedit /etc/bash.bashrc 
  11. Now add the following lines to it.
    export JAVA_HOME
    export PATH 
  12. After doing that open up a new terminal and run following commands to verify JAVA_HOME has set correctly.
    $echo $JAVA_HOME
    Should output this ---> /usr/lib/jvm/jdk1.6.0_32
    $echo $PATH
    Should output this ---> [Other paths]:/usr/lib/jvm/jdk1.6.0_32/bin