Setting File permission in Linux

There are three type of groups who can access a file in linux.

User: who created the file
Group: Group to which user belongs
Others: Other users (not belonging to above 2 categories).

To add/ modify permissions use following format

chmod u=rwx,g=rw,o=rx abc.txt

or a simpler option

chmod 765 abc.txt

Both of the above means same.

How to interpret these numbers


hence Read+Write+Exceite=4+2+1=7 or Read+ execute=4+1=5 and so on.

Longest repeating sub-sequence

Finding longest repeating sub-sequence or substring among a string is classic CS problem. Here is one simple solution where we use approach of finding all the substrings from beginning and sort the resulting array. In sorted array we will find longest common repeating sub-sequence.

Input: banana

Output: ana

//method to return longest subsequence
public static String findLongestSubsequence(String str)
String sub=””;

String arr[]=new String[str.length()];
//Firstly lets find all the substrings
for(int i=0;i<str.length();i++)

//as we have the list of all substrings, sort them

//compare strings with next string and find the maximum length common substring
for(int i=0;i<arr.length-1;i++)
//compare arr[i], arr[i+1]
int len=(arr[i].length()<arr[i+1].length())?arr[i].length():arr[i+1].length();
String temp=””;

for(int j=0;j<len;j++) { //compare two strings till a point where they are equal if(arr[i].charAt(j)==arr[i+1].charAt(j)) temp=arr[i].substring(0,j+1); else break; } sub=(temp.length()>sub.length())?temp:sub;

return sub;

There are other ways to find longest repeating sub-sequence. One  important way is to create a m*n matrix with a value showing length of subsequence till that point

b a n a n a
b   0 0 0 0 0 0
a   0 0 0 1 1 1
n   0 0 0 1 2 2
a   0 1 1 1 2 3
n   0 1 2 2 2 3
a   0 1 2 3 3 3

Another sophisticated way is to create a suffix tree. Read more here -


Checked vs Unchecked exceptions

Exceptions in Java can be broadly categorized into checked and unchecked exceptions. A checked exception is one for which compiler will force you to handle it (either catch it, or throw it back), whereas for an unchecked exception you are not forced to provide handler. So when you are creating a new exception, you can decide whether it is a checked (extending a checked exception) or unchecked exception (extending unchecked exception).

Now the challenge is, how to decide if an excpetion should be checked or unchecked. That is tricky.

“Runtime exceptions represent problems that are the result of a programming problem, and as such, the API client code cannot reasonably be expected to recover from them or to handle them in any way. Such problems include arithmetic exceptions, such as dividing by zero; pointer exceptions, such as trying to access an object through a null reference; and indexing exceptions, such as attempting to access an array element through an index that is too large or too small.”

And to sum it up

“Here’s the bottom line guideline: If a client can reasonably be expected to recover from an exception, make it a checked exception. If a client cannot do anything to recover from the exception, make it an unchecked exception.”

Text Source:

Cron to run every 30 minutes

How will you set up a job to run every 30 minutes, from midnight till 10 in morning, Monday to Friday?

*/30 0-10 * * 1-5 MyJob

Understanding the syntax

* * * * * *
| | | | | |
| | | | | +– Year (range: 1900-3000)
| | | | +—- Day of the Week (range: 1-7, 1 standing for Monday)
| | | +—— Month of the Year (range: 1-12)
| | +——– Day of the Month (range: 1-31)
| +———- Hour (range: 0-23)
+———— Minute (range: 0-59)

More on Cron Jobs

Split a String in controlled manner

You must have used String’s split method a lot of time to split based on a regex or a simple character or sub-string. But did you know that you can apply the split function in controlled manner. i.e. you can specify how many time split should occur or number of max sub-strings you can expect.

Example if I have string boo:and:foo and I will control number of splits to 2 like str.split(“:”,2), I will get 2 substrings, “boo” and “and:foo”.

Read more –,%20int%29

JPA- Java Persistence API

From the very beginning of software development and more so with web applications, the need for storing state and details of objects have been there. The most used manner to store information has been databases. And with storing data into database, need for a standardized way to store this data has been felt.

The most common way to persist data into database is JDBC, i.e. Java Database connectivity. JDBC is Java Specific implementation of ODBC (Open Database Connectivity). It allows simple way for Java to execute SQL commands and interact with Database. Though it is easy to use, JDBC has a major challenge of SQL portability across Database vendors. The SQL command which works fine for oracle might now work for mysql or ms-sql.

Another popular approach for data persistence is ORM or object relational mapping. The idea is to map Java objects (POJOs) directly to a table in database. There are many strong players like Toplink (now eclipse link) and hibernate in this space.

Then came another important player EJB- entity beans. With EJB 2.0, support for persistence was good, but at cost of implementing complex EJBs.

There is another specification for persistence, JDO, Java Data Objects, which is mostly used to save Objects directly and hence make a good case for non relational or no-sql database usage.

Because of all the above mentioned options available for persistence in Java, need for standards became very important, which resulted first in JPA 1.0 and later a more complete specification in form of JPA 2.0.

Creating a Spring MVC REST WebService

To get started let’s create a simple dynamic web project in Eclipse.

1. Modify web.xml to let it know about Spring



Additional Step: Add Spring Jars or Maven dependencies for Spring- core, aop, context, web and webmvc jars.

2. create springapp-servlet.xml

<?xml version=”1.0″ encoding=”UTF-8″?>

<beans xmlns=””
xmlns:mvc=”” xmlns:xsi=””
xmlns:p=”” xmlns:context=””
<!– the application context definition for the springapp DispatcherServlet –>

<mvc:annotation-driven />
<context:annotation-config />

<context:component-scan base-package=”com.kamal.test” />

<!–   <bean name=”/” class=”com.kamal.test.Hello”/> –>


Note that we are using annotation driven implementation. where our package com.kamal.test would be scanned for any available components.

3. Create the service

package com.kamal.test;

import javax.servlet.ServletException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

public class SampleService {
@RequestMapping(method = RequestMethod.GET, value=”/details/{id}”)

public @ResponseBody String LepService(@PathVariable(“id”) String id )
throws ServletException, IOException {
String success=”Sucess”;
//Do something here
return success;


Unicode value for your text


Following code demonstrate how to get unicode value for your text

public static String getUnicodeText(String original)
String unicode=””;
String temp=””;
//Loop through original string and convert each character to unicode equivalent
for( char c : original.toCharArray() ){
//We do not want to convert space
unicode+=” “;
//For symmetry we will keep unicodes of length 4
//Append \\u to let compiler know it is unicode
//return final unicode string created
return unicode;