Wednesday, August 28, 2013

Remote Debugging in Java and Eclipse

Why Remote Debugging?

Debugging is very important to find out problems in the software system. It is good idea to find all the bugs and glitches in the system in development period, but no one can guarantee that a system is "bug-free". Nowadays, we have advanced Integrated Development Environments(IDE's) which makes development and debugging very easy. So, if we find out the problem of the software in the development phase, IDE make it easy to find out. But what if we find out the problem in the software system that is deployed to the client's computer where we can't(not allowed to) install our development environment. It sometimes is impossible to install IDE to a system with low resources(such as low memory or processor power etc.).

Remote debugging covers the problems mentioned above. We can remotely debug the software system installed in a computer anywhere in the network. Remote debugging is quite useful to find out system specific bugs. There are many real examples of bugs which do not exist in the development system, however, they exist in the client's system where we deploy our software.

How Remote Debugging?

Well, we will discuss remote debugging in Java (It is possible in other programming language also) with Eclipse as IDE (I guess it is the most popular IDE). To make the process simple, I have divided whole process into steps. The whole process can be divided into two parts:


Java Configuration(Remote)

For class file
To enable remote debugging for a software as a class file ( e.g. HelloWorld.class ), we use the following command to execute.
   java -Xdebug -Xrunjdwp:transport=dt_socket,address=4444,server=y,suspend=y HelloWorld
  
For JAR file
To enable remote debugging for a software as a JAR file ( e.g. HelloWorld.jar ), we use the following command:
   java -Xdebug -Xrunjdwp:transport=dt_socket,address=4444,server=y,suspend=y -jar HelloWorld.jar
  
For Tomcat Server
If the application is running under tomcat server, we have to modify catalina.sh file in $CATALINA_BASE directory. 
Open the file, and you will see CATALINA_OPTS, just un-comment it and replace with this:
   CATALINA_OPTS="-Dcom.sun.management.jmxremote=true 
          -Dcom.sun.management.jmxremote.port=4444 
          -Dcom.sun.management.jmxremote.authenticate=false 
          -Dcom.sun.management.jmxremote.ssl=false"  
  
And you have to stop the tomcat server if it is already started:
   service tomcat stop
  

Go to bin directory under $CATALINA_BASE (e.g. /opt/tomcat/bin) and execute the following command(
  catalina.sh jpda start
  

Eclipse Configuration(Local)
Eclipse configuration is quite straight-forward.

1. Select the project you want to remote debug.

2. Go to "Debug Configurations..." under Run menu.

3. Create a new Remote Java Application.

4. Provide:
Connection Type: Standard(Socket Attach). 
Host: Remote hostname or IP ( e.g. 192.168.1.83 )
Port :4444
Let the option "Allow termination of remote VM" as disselected. That's it.

References: 
1. http://tomcat.apache.org/tomcat-7.0-doc/monitoring.html#Enabling_JMX_Remote
2. http://javarevisited.blogspot.de/2011/02/how-to-setup-remote-debugging-in.html

Thursday, August 8, 2013

Pending Posts

>> Run Job Periodically in a specified time
>> How Java Table works?
>> Simple XML Serialisation(lightweight XML processing tool appropriate for android)
ULR:http://simple.sourceforge.net/home.php

Monday, April 8, 2013

How to create DEBIAN(.deb) installer

Sometimes we feel frustrated when we have to manage large number of files which we need to deploy some application. The files might be scripts, some packages and source codes also. So, to make the process simple, we just create a Debian file installer which packages everything we need. The most interesting thing is we can define the directory structure for the application, like if we need some folder after installation, we can define folder inside the debian installer. We also have post-installation, pre-remove and post remove script files to make the task more simple.

So, what do we need to create a Debian installer?
I have found two important explanations on how we can make it possible. Lets have a look at
http://www.ibm.com/developerworks/linux/library/l-debpkg/index.html

The following also helped me(it seems to be quite old article though)
http://tldp.org/HOWTO/html_single/Debian-Binary-Package-Building-HOWTO/

We need the following two packages

build-essentials (it is already intalled in most of the linux systems)
dpkg-dev

We use the following commands to create the package:

dpkg -b directory package.deb

There is a good tool called lentian, we can install it to find out the problem. It is a like required tool while creating the debian installer.

The above mentioned links are quite informative.

Thursday, April 4, 2013

Ubuntu Tips

1) How install and set Mate-Desktop as remote desktop
  sudo apt install mate-environment
  sudo apt install xrdp
  [sudo apt install vino]
  echo mate-session> ~/.xsession

   Restart the system

The most usable and popular Ubuntu tools:
Dia: Drawing structured diagrams
LibreOffice4: For office purpose
Inkscape: Vector Drawing tool
OpenSSH: SSH server
Chromium-browser: Browsing tool
BeyondComparer/Meld: File or Folder comparison
VirtualBox: For virtually running Operating systems
CloneZilla: Creating clone image of hard disk or partition
Gparted: For hard disk partition
WPS: Replaces office
rdesktop: Remote desktop



Cleaning commands before creating image:

1) apt-get autoremove
2) apt-get autoclean
3) apt-get clean
4) history -c
5) clean browser histories, caches 




How to make USB bootable for windows OS? 
  • Format your USB as FAT32 in GParted
  • Open UNetbootin and get it as far as the stage where it brings up the USB partition to install to e.g. /dev/sdb1 - Don't install the ISO, though
  • Leaving UNetbootin open as is, reopen GParted
  • Format the USB in GParted as NTFS
  • If GParted doesn't automatically add the "boot" flag, add it yourself
  • Now, go back to UNetbootin, which you've left open in the meantime, and click OK
Doing so, UNetbootin will think you are using a FAT32 partition and will let you use NTFS format.
EDIT: at this point UNetbootin complained the USB device was unmounted. Keeping UNetbootin open I ran sudo mount /dev/sdb1 /mnt. Then I hit "OK" on the still-open UNetbootin. It wrote the USB image to the USB.

Format drive to NTFS, How?
Now to create a bootable Windows 7 USB Drive while using Ubuntu, then you need to make sure you have a Windows 7 .ISO file (you can create it from the DVD) and a 4GB USB flash drive (or larger).
Install Gparted and format the USB drive to NTFS. In Ubuntu, use the following command to install Gparted:1
sudo apt-get install gparted
To be able to format a drive to NTFS, you'll also need ntfs-3g - install it using the following command:1
sudo apt-get install ntfs-3g


Upgrading Ubuntu Kernel

Upgrading ubuntu kernel is easy; just needs a couple of steps:

#Update
sudo aptitude update
#search
sudo aptitutde linux-image
#upgrade
sudo aptitude safe-upgrade
#reboot
sudo init6

For more info
http://www.tcpdump.com/kb/os/linux/ubuntu-kernel-upgrade-procedure.html


Environment Variables

To define and load environment variables permanently in the system, we need to define it in the /etc/environment file as supersuer(since other users dont have permission to write the file). Then save it, after you save it, the newly defined environment variables are not loaded. Normally, they are loaded after you reboot it.

BTW, if you are hurry enough and do not have time to reboot the system, you just use

source /etc/environment

Thats it :)



Friday, March 22, 2013

Creating Debian Installer


Creating Debian installer(.deb) for linux based system makes the installation process very simple because we do not need to worry about the required files and dependencies because everything will be there in the package. Here I am going some basic steps on how can we create the debian installer:

( I am using Ubuntu 12.04 LTS)

1. Prepare Development Environment


$ sudo apt-get install packaging-dev

This installs everything you need.
(For detail: http://developer.ubuntu.com/packaging/html/getting-set-up.html)

2. Packaging

Install build-essential if it is not already installed in the system.

For detail here: http://developer.ubuntu.com/packaging/html/packaging-new-software.html#





Wednesday, March 20, 2013

Java Confusions

1) Why static block?

Java static blocks are used to initialize the static variables. The static block code is loaded when Java Virtual Machine(JVM) loads the class.  Since we know, static variables can not be included in constructor, only member variables can be initialized in constructor. So, for static variables, the static blocks work like a constructor. We can have any number of static blocks that are executed at the time class is loaded. See the example below:

class MyClass { 
 static Map labels = new HashMap(); 
 static { labels.put(5.5, "five and a half"); 
 labels.put(7.1, "seven point 1"); } 
 //... 
}

2) Why final keyword for classes, variables or arguments

Please have a look at my previous article which clarified in somehow more detail:
Final keyword always confusing! 


(TO be continued...)

Monday, March 18, 2013

XML serialization and de-serialization in Java

It is sometimes a great issue when one wants to serialize their objects into XML files. We have JAXB(Java Architecture for XML Binding) API which can serialize and also de-serialize the java object. I will introduce frequently used annotation used for XML serialization with example:

1. Annotating XML variables


@XmlRootElement(namespace="className" or name="className")
@XmlType(propOrder={"property", "list"})
public class className{
   private int attribute;
   private String property;
   private String value;
   private List list;

  @XmlAttribute(value="attribute")
  public int getAttribute(){
   return this.attribute;
  }

@XmlElement(value="property")
  public String getProperty(){
   return this.property;
  }
/*
   @XmlValue
   public String getValue(){
   return this.value;
   }
*/

@XmlElementWrapper(name="lists")
@XmlElement(name="list")
public List getList(){
  return this.list;
 }
}

We can define the annotations for extended objects as follows:

@XmlElementRefs({@XmlElementRef(type=extendedClass1.class),@XmlElementRef(type=extendedClass2.class),@XmlElementRef(type=extendedClass3.class)})
public baseClassObject getObject(){
       return this.object;
}
 

2. Serializing Object


JAXBContext context = JAXBContext.newInstance(className.class);
Marshaller m = context.createMarshaller();
//Format the output
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
m.marshal(classNameObject, fileObject); 
// or to just print to the console
m.marshal(classNameObject, System.out);


3. DeSerialising Object

JAXBContext context = JAXBContext.newInstance(className.class);
Unmarshaller m = context.createUnmarshaller();
classNameObject = (className)m.unmarshal(fileObject);




Saturday, February 23, 2013

Installing OpenCV in Ubuntu

If you want to install OpenCV in ubuntu, the clearcut explanation is provided here:

The use of opencv library to create a sample application in eclipse has been described here



Things to know:
1. The installation of opencv will create include files in /usr/local/include and library files in /usr/local/lib

2. Since /usr/local/include location is already included by eclipse, so we do not need to include it. But the library files need to be included as explained in the second link above.