Tuesday, June 19, 2012

Access restriction on class due to restriction on required library rt.jar for OperatingSystemMXBean ?

Its work for me :
  1. Go to the Build Path settings in the project properties.
  2. Remove the JRE System Library
  3. Add it back; Select "Add Library" and select the JRE System Library. The default worked for me

Wednesday, June 13, 2012

How to convert InputStream to String in Java?

/**
 *
 */
package convert.stream.to.string;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * @author abdul
 *
 */
public class StreamToString {

    /**
     * @param args
     */
    public static void main(String[] args) {
        StreamToString streamToString = new StreamToString();
     
        //intilize an InputStream
        InputStream is = new ByteArrayInputStream("file content:\nData1\nData2".getBytes());
     
        /*
         * Call the method to convert the stream to string
         */
        System.out.println(streamToString.convertStreamToString(is));
    }

    private String convertStreamToString(InputStream is) {
        /*
         * To convert the InputStream to String we use the BufferedReader.readLine()
         * method. We iterate until the BufferedReader return null which means
         * there's no more data to read. Each line will appended to a StringBuilder
         * and returned as String.
         */
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();

        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }
}



Output:
file content:
Data1
Data2



How to find exact version of Red Hat Linux


[root@localhost ]# cat /etc/redhat-release
Red Hat Enterprise Linux Server release 5.2 (Tikanga)


For others:

Slackware: /etc/slackware-version
Mandrake: /etc/mandrake-release
Red Hat: /etc/redhat-release
Fedora: /etc/fedora-release

Tuesday, June 12, 2012

How to configure session timeout for putty

Click on the upper left hand corner of your putty screen, then click on 'Change Settings', then 'Connection'. I have my 'keepalive' set to 300.
Now It will time out after 5 minutes.

Thursday, April 26, 2012

Get Sub List of Java ArrayList Example

package com.subList.demo;

import java.util.ArrayList;
import java.util.List;

/**
 * This Java Example shows how to get sub list of java ArrayList using subList method.
 *
 * @author abdul
 */
public class SubListDemo {

    public static void main(String args[]) {

        //create an ArrayList object
        List<String> list = new ArrayList<String>();
        int range = 4;

        //Add elements to Arraylist
        list.add("A");
        list.add("B");
        list.add("C");
        list.add("D");
        list.add("E");
        list.add("F");
        list.add("G");
        list.add("H");
        list.add("I");
        list.add("J");
        list.add("K");
        list.add("L");
        list.add("M");
        list.add("N");
        list.add("O");
        display("Displaying all elements of the list : ", list);

        if(list.size() > range) {
            int from = 0;
            int to = range;
            List<String> lt = null;
            do {
                //Check whether "to" value never exceeds from the list size
                //otherwise it would throw error while fetching subList
                if(list.size() - from > range ) {

                    // To get a sub list of Java ArrayList use List subList(int startIndex, int endIndex) method.
                    //This method returns an object of type List containing elements from startIndex to endIndex - 1
                    lt = list.subList(from, to);

                    display("SubList from " + from + " to " + to, lt);
                } else {
                    lt = list.subList(from, list.size());
                    display("SubList from " + from + " to " + list.size(),lt);
                }
                from += range;
                to += range;
            }while(from < list.size());
        } else {
            display("Displaying all elements of the list : " , list);
        }
    }

    /**
     * Display elements of sub list.
     * @param message
     * @param list sublist of the list to print
     */
    private static void display(String message,List<String> list) {
        System.out.println(message + "\nList: " + list);
    }
}

Output:
Displaying all elements of the list :
List: [A, B, C, D, E, F, G, H, I, J, K, L, M, N, O]
SubList from 0 to 4
List: [A, B, C, D]
SubList from 4 to 8
List: [E, F, G, H]
SubList from 8 to 12
List: [I, J, K, L]
SubList from 12 to 15
List: [M, N, O]


Related link:
http://docs.oracle.com/javase/1.4.2/docs/api/java/util/List.html

Wednesday, April 25, 2012

Debug Java applications remotely with Eclipse



Remote debugging is a way of debugging any process running on some other location from your development machine. Local debugging is the best way in my opinion and should always be preferred over remote debugging but if local debugging is not possible and there is no way to debug your process then remote debugging is the solution.


Many of us work on a project which runs on Linux operating system and we do development mostly on Windows.
Eclipse provides us most useful feature called "Remote debugging" by using which you can debug your Linux running process from your windows machine.

Now let's see how we can setup remote debugging in Eclipse:

Just take a example of a simple program that we want to be debugged:


package com.tutoial.debugger;

/**
 * @author abdul
 *
 */
public class Debug {
    public static void main(String args[]) {
        for(int i=1; i<=10;i++) {
            System.out.println("2 * "+ i + " = " + 2*i);
        }
    }
}


Convert it into the jar.
From Eclipse , You can do it by following step:
1. Select the file that you want to export, Right click and choose "export".
2 .Goto Java-> jar and click Next
3. Browse the path where you want to export and click "finish".





Now, In order to remote debug a java application, Start your application with the following JVM debug options:

java -Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y suspend=y -jar debug.jar


This will start java applicaiton debug.jar into debug mode using Java Debug Wire Protocol (jdwp) protocol and it will listen on port 8000 ,suspend=y will ensure that that application will not start running until eclipse connect it on speicified debug port.

After running it you should see something like that:
          Listening for transport dt_socket at address: 8000


Go to Eclipse and open debug dialog (you can press Alt+R, B). Create a new Remote Java Application Configuration. Select connection type as "Socket attach" and specify Connection Properties (for our example host would be localhost, and port would be 8000).




It also important to note that application must be start before eclipse tries to connect it other wise Eclipse will throw error "Failed to connect to remote VM. Connection refused" or "Connection refused: connect"

Enjoy..!!!!


Related link:

http://help.eclipse.org/helios/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Ftasks%2Ftask-remotejava_launch_config.htm
http://www.ibm.com/developerworks/java/library/os-eclipse-javadebug/index.html?ca=dgr-jw22os-eclipse-javadebug/index.html&S_TACT=105AGX59&S_CMP=GRsitejw22

How TOPT Works: Generating OTPs Without Internet Connection

Introduction Have you ever wondered how authentication apps like RSA Authenticator generate One-Time Passwords (OTPs) without requiring an i...