How to Write Own Caching in Java | Improve your Java application Performance By Using Cache

Caching is very important when you want to improve your Java application performance
In My Project there was some performance issue during show huge date in screen. In that case
i have improve my java application performance by written own Cache Utility class, Which is below

public class UtilCache
{
 private UtilCache(String cacheName, int sizeLimit, int maxInMemory, long expireTimeMillis, boolean useSoftReference, boolean useFileSystemStore, String propName, String... propNames) {
        this.name = cacheName;
        this.sizeLimit = sizeLimit;
        this.maxInMemory = maxInMemory;
        this.expireTimeNanos = TimeUnit.NANOSECONDS.convert(expireTimeMillis, TimeUnit.MILLISECONDS);
        this.useSoftReference = useSoftReference;
        this.useFileSystemStore = useFileSystemStore;
        setPropertiesParams(propName);
        setPropertiesParams(propNames);
        int maxMemSize = this.maxInMemory;
        if (maxMemSize == 0) maxMemSize = sizeLimit;
        if (maxMemSize == 0) {
            memoryTable = new ConcurrentHashMap<Object, CacheLine<V>>();
        } else {
            memoryTable = new Builder<Object, CacheLine<V>>()
            .maximumWeightedCapacity(maxMemSize)
            .listener(this)
            .build();
        }
        if (this.useFileSystemStore) {
            // create the manager the first time it is needed
            jdbmMgr = fileManagers.get(fileStore);
            if (jdbmMgr == null) {
                Debug.logImportant("Creating file system cache store for cache with name: " + cacheName, module);
                try {
                    String ofbizHome = System.getProperty("ofbiz.home");
                    if (ofbizHome == null) {
                        Debug.logError("No ofbiz.home property set in environment", module);
                    } else {
                        jdbmMgr = new JdbmRecordManager(ofbizHome + "/" + fileStore);
                    }
                } catch (IOException e) {
                    Debug.logError(e, "Error creating file system cache store for cache with name: " + cacheName, module);
                }
                fileManagers.putIfAbsent(fileStore, jdbmMgr);
            }
            jdbmMgr = fileManagers.get(fileStore);
            if (jdbmMgr != null) {
                try {
                    this.fileTable = HTree.createInstance(jdbmMgr);
                    jdbmMgr.setNamedObject(cacheName, this.fileTable.getRecid());
                    jdbmMgr.commit();
                } catch (IOException e) {
                    Debug.logError(e, module);
                }
            }
        }
    }

    private static String getNextDefaultIndex(String cacheName) {
        AtomicInteger curInd = defaultIndices.get(cacheName);
        if (curInd == null) {
            defaultIndices.putIfAbsent(cacheName, new AtomicInteger(0));
            curInd = defaultIndices.get(cacheName);
        }
        int i = curInd.getAndIncrement();
        return i == 0 ? "" : Integer.toString(i);
    }

    public static String getPropertyParam(ResourceBundle res, String[] propNames, String parameter) {
        try {
            for (String propName: propNames) {
            if(res.containsKey(propName+ '.' + parameter)) {
                try {
                return res.getString(propName + '.' + parameter);
                } catch (MissingResourceException e) {}
            }
            }
            // don't need this, just return null
            //if (value == null) {
            //    throw new MissingResourceException("Can't find resource for bundle", res.getClass().getName(), Arrays.asList(propNames) + "." + parameter);
            //}
        } catch (Exception e) {
            Debug.logWarning(e, "Error getting " + parameter + " value from cache.properties file for propNames: " + propNames, module);
        }
        return null;
    }

    protected void setPropertiesParams(String cacheName) {
        setPropertiesParams(new String[] {cacheName});
    }

    public void setPropertiesParams(String[] propNames) {
        ResourceBundle res = ResourceBundle.getBundle("cache");

        if (res != null) {
            String value = getPropertyParam(res, propNames, "maxSize");
            if (UtilValidate.isNotEmpty(value)) {
                this.sizeLimit = Integer.parseInt(value);
            }
            value = getPropertyParam(res, propNames, "maxInMemory");
            if (UtilValidate.isNotEmpty(value)) {
                this.maxInMemory = Integer.parseInt(value);
            }
            value = getPropertyParam(res, propNames, "expireTime");
            if (UtilValidate.isNotEmpty(value)) {
                this.expireTimeNanos = TimeUnit.NANOSECONDS.convert(Long.parseLong(value), TimeUnit.MILLISECONDS);
            }
            value = getPropertyParam(res, propNames, "useSoftReference");
            if (value != null) {
                useSoftReference = "true".equals(value);
            }
            value = getPropertyParam(res, propNames, "useFileSystemStore");
            if (value != null) {
                useFileSystemStore = "true".equals(value);
            }
            value = getPropertyParam(res, new String[0], "cache.file.store");
            if (value != null) {
                fileStore = value;
            }
        }
    }

    private Object fromKey(Object key) {
        return key == null ? ObjectType.NULL : key;
    }

    @SuppressWarnings("unchecked")
    private K toKey(Object key) {
        return key == ObjectType.NULL ? null : (K) key;
    }

    private void addAllFileTableKeys(Set<Object> keys) throws IOException {
        FastIterator<Object> iter = fileTable.keys();
        Object key = null;
        while ((key = iter.next()) != null) {
            keys.add(key);
        }
    }

    public Object getCacheLineTable() {
        throw new UnsupportedOperationException();
    }

    public boolean isEmpty() {
        if (fileTable != null) {
            try {
                synchronized (this) {
                    return fileTable.keys().next() == null;
                }
            } catch (IOException e) {
                Debug.logError(e, module);
                return false;
            }
        } else {
            return memoryTable.isEmpty();
        }
    }

    /** Puts or loads the passed element into the cache
     * @param key The key for the element, used to reference it in the hastables and LRU linked list
     * @param value The value of the element
     */
    public V put(K key, V value) {
        return putInternal(key, value, expireTimeNanos);
    }

    public V putIfAbsent(K key, V value) {
        return putIfAbsentInternal(key, value, expireTimeNanos);
    }

    CacheLine<V> createSoftRefCacheLine(final Object key, V value, long loadTimeNanos, long expireTimeNanos) {
        return tryRegister(loadTimeNanos, new SoftRefCacheLine<V>(value, loadTimeNanos, expireTimeNanos) {
            @Override
            CacheLine<V> changeLine(boolean useSoftReference, long expireTimeNanos) {
                if (useSoftReference) {
                    if (differentExpireTime(expireTimeNanos)) {
                        return this;
                    } else {
                        return createSoftRefCacheLine(key, getValue(), loadTimeNanos, expireTimeNanos);
                    }
                } else {
                    return createHardRefCacheLine(key, getValue(), loadTimeNanos, expireTimeNanos);
                }
            }

            @Override
            void remove() {
                removeInternal(key, this);
            }
        });
    }

    CacheLine<V> createHardRefCacheLine(final Object key, V value, long loadTimeNanos, long expireTimeNanos) {
        return tryRegister(loadTimeNanos, new HardRefCacheLine<V>(value, loadTimeNanos, expireTimeNanos) {
            @Override
            CacheLine<V> changeLine(boolean useSoftReference, long expireTimeNanos) {
                if (useSoftReference) {
                    return createSoftRefCacheLine(key, getValue(), loadTimeNanos, expireTimeNanos);
                } else {
                    if (differentExpireTime(expireTimeNanos)) {
                        return this;
                    } else {
                        return createHardRefCacheLine(key, getValue(), loadTimeNanos, expireTimeNanos);
                    }
                }
            }

            @Override
            void remove() {
                removeInternal(key, this);
            }
        });
    }

    private CacheLine<V> tryRegister(long loadTimeNanos, CacheLine<V> line) {
        if (loadTimeNanos > 0) {
            ExecutionPool.addPulse(line);
        }
        return line;
    }

    private CacheLine<V> createCacheLine(K key, V value, long expireTimeNanos) {
        long loadTimeNanos = expireTimeNanos > 0 ? System.nanoTime() : 0;
        if (useSoftReference) {
            return createSoftRefCacheLine(key, value, loadTimeNanos, expireTimeNanos);
        } else {
            return createHardRefCacheLine(key, value, loadTimeNanos, expireTimeNanos);
        }
    }
    private V cancel(CacheLine<V> line) {
        // FIXME: this is a race condition, the item could expire
        // between the time it is replaced, and it is cancelled
        V oldValue = line.getValue();
        ExecutionPool.removePulse(line);
        line.cancel();
        return oldValue;
    }

    /** Puts or loads the passed element into the cache
     * @param key The key for the element, used to reference it in the hastables and LRU linked list
     * @param value The value of the element
     * @param expireTimeMillis how long to keep this key in the cache
     */
    public V put(K key, V value, long expireTimeMillis) {
        return putInternal(key, value, TimeUnit.NANOSECONDS.convert(expireTimeMillis, TimeUnit.MILLISECONDS));
    }

    public V putIfAbsent(K key, V value, long expireTimeMillis) {
        return putIfAbsentInternal(key, value, TimeUnit.NANOSECONDS.convert(expireTimeMillis, TimeUnit.MILLISECONDS));
    }

    V putInternal(K key, V value, long expireTimeNanos) {
        Object nulledKey = fromKey(key);
        CacheLine<V> oldCacheLine = memoryTable.put(nulledKey, createCacheLine(key, value, expireTimeNanos));
        V oldValue = oldCacheLine == null ? null : cancel(oldCacheLine);
        if (fileTable != null) {
            try {
                synchronized (this) {
                    if (oldValue == null) oldValue = fileTable.get(nulledKey);
                    fileTable.put(nulledKey, value);
                    jdbmMgr.commit();
                }
            } catch (IOException e) {
                Debug.logError(e, module);
            }
        }
        if (oldValue == null) {
            noteAddition(key, value);
            return null;
        } else {
            noteUpdate(key, value, oldValue);
            return oldValue;
        }
    }


}

Create WSDL in Apache obfiz | Create web services in Apache ofbiz | Apache ofbiz web services tutorial.

In my project there is requirement of web services integration with Microsoft BizTalk i.e. Through WSDL
Biztalk will communicate with Apache ofbiz Services.

Following below step to follow for exposed ofbiz service to as web Services

1. Create a Apache ofbiz Services as below
   package org.ofbiz.party.party;

import java.util.Map;

import javolution.util.FastMap;

import org.ofbiz.service.DispatchContext;

public class TestWebService {
   
   
      public static Map<String, Object> testWebSerice(DispatchContext ctx, Map<String, ? extends Object> context) {
         
          Map<String, Object> result = FastMap.newInstance();
        try
        {
          String username=(String) context.get("userName");
         
          if(username.equals("kumud"))
          {
              result.put("returnMsg", "Hi Kumud") ;
          }
          else
          {
              result.put("returnMsg", "You are not kumud") ;
          }
         
          return result;
         
      }
     
      catch(Exception ex)
      {
          ex.fillInStackTrace();
          result.put("returnMsg", "Hi Kumud") ;
          return result;
      }
}
}


2. Register service in services.xml file as below

 <service name="TestWebSerice" engine="java" auth="false"
            location="org.ofbiz.party.party.TestWebService" invoke="testWebSerice"  export="false">
        <description>Test Web Service  by kumud</description>
       
        <attribute name="userName" type="String" mode="IN" optional="false"/>
        <attribute name="returnMsg" type="String" mode="OUT" optional="false"/>
       
    </service>

3. For expose above service as Web Services by making  export="true" in above configuration

   <service name="TestWebSerice" engine="java" auth="false"
            location="org.ofbiz.party.party.TestWebService" invoke="testWebSerice"  export="true">
        <description>Test Web Service  by kumud</description>
       
        <attribute name="userName" type="String" mode="IN" optional="false"/>
        <attribute name="returnMsg" type="String" mode="OUT" optional="false"/>
       
    </service>
   
4. Now start server hit by url http://localhost:8080/webtools/control/SOAPService/TestWebSerice?wsdl in web browser
   there show WSDL file

difference between return; and return (xx); in Java | Java concept

java return keyword is one of the most frequently used keyword in java and also one of the concept, which most programmers say they are well aware of. But, I still meet programmers who say that they are little confused when they come down to return statement. Especially with respect to the method which has void as their return type. So, this post would help you in clearing those doubts and help you understand the clear picture of it... in the most simplest way.
We all know that Java return keyword is used to return the control back to calling method with a certain data-type which was promised during the method declaration. As many wanted to know if we can use just the return keyword(like this: return;) but not return any value(like this: return obj;), then I have to say yes to that. Se the below java programs which will help you understanding the difference better,

public class ReturnTypes {
    public static void main(String args[]){
        functionVoid();
       
functionType();
    }
   
    public static int
functionType(){
        return 1;
    }

    private static void
functionVoid() {
        System.out.println("1");
        if(true){
            return;
        }
        // return;  // This line if UnCommented, would result in Compilation error
        System.out.println("2");
    }
}

If you see in the method functionType() the return keyword is used to return an primitive int value, which is quiet normal and know to almost every java programmer.

But, If you see the method functionVoid() it in fact uses the controversial return keyword(just "return;") which doesn't return anything at-all. Yes this is absolutely valid to use it that way... Ahaaaa hold on, I am not yet done completely... and now imagine if you can use the same return type outside the scope of if statement, as It's already available in program but commented out... That will certainly thorough you an compilation error.

Now why so.. There is nothing wrong with the syntax of return; statement but the error is something different as the compiler is just complaining about "Unreachable Code"... 

By now you know that we can use both of these conventions and are very much valid. But you can ask yourself a question on why actually java needs a return; statement in the first, when we already know that the method is not meant to return any thing back to the calling function as we have declared it void...??

The answer is, you can treat return keyword as a form of goto statement. As it will directly move the control to the line where the method is called. And if you want to accomplish that in an void method then what other option you have to do that other than return; statement.

How string comparison works in java | Java interview question for experinced developer

explain "how String comparison works in Java?" is the most common question interviewers ask these days, and today this post is going to explain the answer in detail. String is one of Java's most admired feature and most used class in the whole Java for sure and I hope you all won't deny that fact. But, sadly most programmers fail to answer this question. Reason is either learners or programmers don't take String seriously or they just don't take Java seriously. So, let's start our learning.....

There are two thing into consideration when we talk about String comparison
1. "==" normal Java comparison operator
2. ".equals()" method of Object class

"=="  in Java is used to compare two values, when I say values... I mean, they can be of any primitive types or any object types. Now when our focus is specifically on String class, we take objects alone into consideration. So, what really happens when you compare any two objects using == operator..? It's simple.. They just compare the two objects and see if they both are pointing to same memory location. If Java found that both strings are pointing to same memory location, then it returns boolean true else if both objects are pointing to different memory locations then it returns boolean false. Now, at this point of time don't worry about the implementation as I will explain with example in detail.

".equals()" in Java on the other hand is actually used to compare the contents of two objects. When I say contents of the objects, I mean value within the string and not the memory location to which the objects point to. In this kind of comparison if Java finds that the value in two different string objects are same, they return boolean true else boolean false. As simple as that isn't it..! Just give this below example a try in your favorite editor,

public class StringTestOne {
    public static void main(String[] args) {
        String s1 = "AAA";
        String s2 = "AAA";
        String s3 = "BBB";
       
        if(s1==s2){
            System.out.println("s1 == s2 : TRUE");
        } else {
            System.out.println("s1 == s2 : FALSE");
        }
        if(s1.equals(s2)){
            System.out.println("s1.equals(s2) : TRUE");
        } else {
            System.out.println("s1.equals(s2) : FALSE");
        }
       
        if(s1==s3){
            System.out.println("s1 == s3 : TRUE");
        } else {
            System.out.println("s1 == s3 : FALSE");
        }
        if(s1.equals(s3)){
            System.out.println("s1.equals(s3) : TRUE");
        } else {
            System.out.println("s1.equals(s3) : FALSE");
        }
    }
}
now let us trace the program,

-first the fourth if statement : it is resulting FALSE as we know that .equals() method compares the content of two object i.e "AAA" and "BBB"(no human on earth can prove that "AAA" is equal to "BBB"),  thus it's very clear that it has returned FALSE.

-then the third if statement : it returns FALSE as again we know that == operator will compare the memory location to which the objects s1 and s3 refers to, but not the values . As of now it makes sense that Java would create a separate memory location for every object created on heap. Thus, even in this case Java would have created two different objects s1 and s3 which would be pointing to two different memory places in memory. So, expected result FALSE. hmmm  you are smart..!

-then the second if statement : again as expected the values of s1 and s2 are same and hence the .equals() method will and should return TRUE.

-Now comes the first if statement : Why on earth is it returning TRUE while there are two different object s1 and s2 into consideration. Yes I agree to the point that they both have same values, but Java needs to compare the object handles while using == operator... isn't it what wee have learned till now..? Yes very much true and that's the reason we have this post today. 

This TRUE thing in the first if statement is due to the concept of  String class being immutable. You might have certainly learned in you college that your professors saying String is immutable. That's the reason Strings are so special in Java and that's the reason why few programmers still feel String comparison a confusing task. Don't worry as it would be explained clearly now, 

Internally Java uses the concept of String pool in order to make use of strings  efficiently across Java. Thus, whenever you create new string objects in your program, it will first search that string pool to see if there is any existing string value which is already created having same value. And if Java thinks that an value already exists in pool, then it takes no permission from you(as string is already final and programmer has no control on it) to point your new string object to the same memory location to which earlier object was pointing to. If it thinks there is not such value in the pool, they make a new entry into the pool. Let's take the earlier examples,

String s1 = "AAA"; //This will first search the String pool to see if there is any such value already existing in the pool. Knowing that there is no such string value, it will add the value into Java's String pool for the first time and assigns the memory location to object handle s1.
String s2 = "AAA"; //This will also first search the String pool to see if there is any such entry in the pool with the value "AAA", As you it finds one such entry it will just make use of that memory location and assigns the same to string object s2 as well.

Which means that, even though we have two objects s1 and s2 created, Java has done some internal optimization to ensure that it doesn't waste memory unnecessarily and thus s1 and s2 will be pointing to the same memory location where the value "AAA" is actually stored. That is the reason why we got TRUE in the first if statement earlier. And that is the reason why string is called immutable.

Difference between left and outer joins in database | database interview question

 There are two kinds of OUTER joins in SQL, LEFT OUTER join and RIGHT OUTER join. Main difference between RIGHT OUTER join and LEFT OUTER join, as there name suggest, is inclusion of non matched rows. Sine INNER join only include matching rows, where value of joining column is same, in final result set, but OUTER join extends that functionality and also include unmatched rows in final result. LEFT outer join includes unmatched rows from table written on left of join predicate. On the other hand RIGHT OUTER join, along with all matching rows, includes unmatched rows from right side of table. In short result of LEFT outer join is INNER JOIN + unmatched rows from LEFT table and RIGHT OUTER join is INNER JOIN + unmatched rows from right hand side table. Similar to difference between INNER join and OUTER join, difference between LEFT and RIGHT OUTER JOIN can be better understand by a simple example, which we will see in next section. By the way joins are very popular in SQL interviews, and along with classic questions like finding second highest salary of employee, Inner join vs outer join or left outer join vs right outer join is commonly asked.


LEFT and RIGHT OUTER Join Example in SQL
LEFT vs RIGHT OUTER JOIN in SQL, MySQL databaseIn order to understand difference between LEFT and RIGHT outer join, we will use once again use classical Employee and Department relationship. In this example, both of these table are connected using dept_id, which means both have same set of data in that column, let's see data on these two table.

mysql> select * from employee;
+--------+----------+---------+--------+
| emp_id | emp_name | dept_id | salary |
+--------+----------+---------+--------+
|    103 | Jack     |       1 |   1400 |
|    104 | John     |       2 |   1450 |
|    108 | Alan     |       3 |   1150 |
|    107 | Ram      |    NULL |    600 |
+--------+----------+---------+--------+
4 rows in set (0.00 sec)

mysql> select * from department;
+---------+-----------+
| dept_id | dept_name |
+---------+-----------+
|       1 | Sales     |
|       2 | Finance   |
|       3 | Accounts  |
|       4 | Marketing |
+---------+-----------+
4 rows in set (0.00 sec)

If you look closely, there is one row in employee table which contains NULL, for which there is no entry in department table. Similarly department table contains a department (row) Marketing ,for which there is no employee in employee table. When we do a LEFT or RIGHT outer join it includes unmatched rows from left or right table. In this case LEFT OUTER JOIN should include employee with NULL as department and RIGHT OUTER JOIN should include Marketing department. Here is example of LEFT and RIGHT OUTER Join in MySQL database :

mysql> select e.emp_id, e.emp_name, d.dept_name from employee e LEFT JOIN department d on e.dept_id=d.dept_id;
+--------+----------+-----------+
| emp_id | emp_name | dept_name |
+--------+----------+-----------+
|    103 | Jack     | Sales     |
|    104 | John     | Finance   |
|    108 | Alan     | Accounts  |
|    107 | Ram      | NULL      |
+--------+----------+-----------+
4 rows in set (0.01 sec)

As I said unmatched rows, i.e. row with dept_id as NULL has included in final result and dept_name for that row is NULL, as there is no corresponding row for NULL dept_id in department table. But note that Marketing department is not included in this result. Now, let's see example of RIGHT OUTER JOIN in MySQL, this should include Marketing department but leave out employee with NULL dept_id.

mysql> select e.emp_id, e.emp_name, d.dept_name from employee e RIGHT JOIN department d on e.dept_id=d.dept_id;
+--------+----------+-----------+
| emp_id | emp_name | dept_name |
+--------+----------+-----------+
|    103 | Jack     | Sales     |
|    104 | John     | Finance   |
|    108 | Alan     | Accounts  |
|   NULL | NULL     | Marketing |
+--------+----------+-----------+
4 rows in set (0.00 sec)

As I said, final result set has Marketing department and emp_id, emp_name is NULL in that row because there is no employee with dept_id=4 in employee table.

Difference between LEFT and RIGHT OUTER JOIN in SQL
In short, following are some notable difference between LEFT and RIGHT outer join in SQL :

1) LEFT OUTER join includes unmatched rows from left table while RIGHT OUTER join includes unmatched rows from right side of table.

2) Result of LEFT OUTER join can be seen as INNER JOIN + unmatched rows of left able while result of RIGHT OUTER join is equal to INNER JOIN + unmatched rows from right side table.

3) In ANSI SQL, left outer join is written as LEFT JOIN while right outer join is written as RIGHT JOIN in select sql statements.

4) In Transact-SQL syntax left outer join is written as *= and right outer join is written as =*, Sybase database supports both syntax and you can write join queries in both ANSI and T-SQL syntax.

That's all on difference between LEFT and RIGHT OUTER JOIN in SQL. We have seen example of RIGHT and LEFT join in MySQL database but since we have used ANSI syntax of OUTER joins, it's for other databases e.g. Oracle, Sybase, SQL Server and PostgreSQL as well. JOIN is a one of the most important and common concept in SQL and you should be good enough to figure out which rows will be included as a result of JOIN statement before actually running that SELECT query against any table. Some time erroneous JOIN query can bring loads of data and potentially may hang your database so beware of it.

Producer-Consumer Problem in Java | Java Thread interview question for Experinced developer

The producer-consumer problem is one of the most frequently encountered problems when we attempt multi threaded programming. While not as challenging as some of the other problems in multi-threaded programming, an incorrect implementation of this problem can create a mess of your application. Produced items will be left unconsumed, starting items will be skipped, consumption depends on whether the production began earlier or later than consumption attempts etc. To add to this you might notice the anomalies long after it has actually happened and most importantly like almost all multi-threaded programs, this one is hard to debug and reproduce too.
So in this post I thought I would attempt to solve this problem in Java with the help of Java' awesome java.util.concurrent package and its classes.
First of all, let us see the characteristics of the producer-consumer problem:
  • Producer(s) produce items.
  • Consumer(s) consume the items produced by the producer(s).
  • Producer(s) finish production and let the consumers know that they are done.
Note that in this producer-consumer problem the producer is running on a different thread than the ones on consumer. This setup makes sense in two cases:
  • The steps to do the consumption of the item produced in independent and not dependent on other items.
  • The time to process the items is larger that the time to produce them.
The term "larger" in the second point is used a bit loosely. Consider the case where producer reads a line from a file and the "consumption and processing" is just to log the line in a special format back to a file then the use of a producer consumer problem solution can be considered a case of over-engineering a solution. However if for each of those lines the "consumption and processing" step is to make a HTTP GET/POST request to a web-server and then dump the result somewhere then we should opt for a producer-consumer solution. In this case I am assuming that all the data to do a GET/POST is available in the line (item) itself and we are not dependent on previous/next lines.
So let us first take a look at the characteristics of the producer-consumer problem solution that I have posted below:
  • There can be multiple producer.
  • There will be multiple consumers.
  • Once the production of new items is done the producer(s) will let the consumers know so that the consumer will exit after the last item is consumed and processed.
It is interesting to note that to solve this problem at a generic level we can address only the consumer side and not the producer side. This is because the production of items might be done at any time and there is very little that we can do in a generic way to control the production of items. We can, however control the consumer's behaviour while accepting items from producer(s). Having laid out the rules let us take a look at the consumer contract:


  1. package com.maximus.producerconsumer;  
  2.   
  3. public interface Consumer  
  4. {  
  5.  public boolean consume(Item j);  
  6.    
  7.  public void finishConsumption();  
  8. }   

Here the consumer can be shared between multiple producers of similar items; by similar items I mean producer that produces objects of type "Item". The definition if Item is as follows:


  1. package com.maximus.consumer;  
  2.   
  3. public interface Item  
  4. {  
  5.  public void process();  
  6. }  
Now we take a look at an implementation of the Consumer interface:

  1. package com.maximus.consumer;  
  2.   
  3. import java.util.LinkedList;  
  4. import java.util.List;  
  5. import java.util.concurrent.BlockingQueue;  
  6. import java.util.concurrent.ExecutorService;  
  7. import java.util.concurrent.Executors;  
  8. import java.util.concurrent.LinkedBlockingQueue;  
  9.   
  10. public class ConsumerImpl implements Consumer  
  11. {  
  12.  private BlockingQueue< Item > itemQueue =   
  13.   new LinkedBlockingQueue< Item >();  
  14.    
  15.  private ExecutorService executorService =   
  16.   Executors.newCachedThreadPool();  
  17.    
  18.  private List< ItemProcessor > jobList =   
  19.   new LinkedList< ItemProcessor >();  
  20.    
  21.  private volatile boolean shutdownCalled = false;  
  22.     
  23.  public ConsumerImpl(int poolSize)  
  24.  {  
  25.   for(int i = 0; i < poolSize; i++)  
  26.   {  
  27.    ItemProcessor jobThread =   
  28.     new ItemProcessor(itemQueue);  
  29.      
  30.    jobList.add(jobThread);  
  31.    executorService.submit(jobThread);  
  32.   }  
  33.  }  
  34.    
  35.  public boolean consume(Item j)  
  36.  {  
  37.   if(!shutdownCalled)  
  38.   {  
  39.    try  
  40.    {  
  41.     itemQueue.put(j);  
  42.    }  
  43.    catch(InterruptedException ie)  
  44.    {  
  45.     Thread.currentThread().interrupt();  
  46.     return false;  
  47.    }  
  48.    return true;  
  49.   }  
  50.   else  
  51.   {  
  52.    return false;  
  53.   }  
  54.  }  
  55.    
  56.  public void finishConsumption()  
  57.  {  
  58.   shutdownCalled = true;  
  59.     
  60.   for(ItemProcessor j : jobList)  
  61.   {  
  62.    j.cancelExecution();  
  63.   }  
  64.     
  65.   executorService.shutdown();  
  66.  }  
  67. }  

Now the only point of interest is the ItemProcessor that the consumer internally uses to process the incoming items. ItemProcessor is coded as follows:

  1. package com.maximus.consumer;  
  2.   
  3. import java.util.concurrent.BlockingQueue;  
  4. import java.util.concurrent.TimeUnit;  
  5.   
  6. public class ItemProcessor implements Runnable  
  7. {  
  8.  private BlockingQueue< Item> jobQueue;  
  9.    
  10.  private volatile boolean keepProcessing;  
  11.     
  12.  public ItemProcessor(BlockingQueue< Item > queue)  
  13.  {  
  14.   jobQueue = queue;  
  15.   keepProcessing = true;  
  16.  }  
  17.    
  18.  public void run()  
  19.  {  
  20.   while(keepProcessing || !jobQueue.isEmpty())  
  21.   {  
  22.    try  
  23.    {  
  24.     Item j = jobQueue.poll(10, TimeUnit.SECONDS);  
  25.       
  26.     if(j != null)  
  27.     {  
  28.      j.process();  
  29.     }  
  30.    }  
  31.    catch(InterruptedException ie)  
  32.    {  
  33.     Thread.currentThread().interrupt();  
  34.     return;  
  35.    }  
  36.   }  
  37.  }  
  38.    
  39.  public void cancelExecution()  
  40.  {  
  41.   this.keepProcessing = false;  
  42.  }  
  43. }   
The only challenge above is the condition in the while loop. The while loop is so written to support the continuation of the consumption of items even after the producer(s) have finished production and has notified the consumer that production is finished. The above while loop ensures that consumption of all the items is done before the threads exit.This will be the case when producers run faster that consumers.

The above consumer is thread-safe and can be shared multiple producers such that each producer may concurrently call consumer.consume() without bothering about synchronization and other multi-threading caveats. Producers just need to submit an implementation of the Item interface whose process() method will contain the logic of how the consumption will be done.

As a bonus for reading the post I put forward a test program that demonstrates how to use the above classes:



  1. package com.maximus.consumer;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.File;  
  5. import java.io.FileInputStream;  
  6. import java.io.InputStreamReader;  
  7.   
  8. public class Test  
  9. {  
  10.  public static void main(String[] args) throws Exception  
  11.         {  
  12.          Consumer consumer = new ConsumerImpl(10);  
  13.            
  14.          BufferedReader br =   
  15.           new BufferedReader(  
  16.           new InputStreamReader(  
  17.           new FileInputStream(  
  18.           new File(args[0]))));  
  19.            
  20.          String line = "";  
  21.            
  22.          while((line = br.readLine()) != null)  
  23.          {  
  24.           System.out.println(  
  25.            "Producer producing: " + line);  
  26.           consumer.consume(new PrintJob(line));  
  27.          }  
  28.            
  29.          consumer.finishConsumption();  
  30.         }  
  31. }  
  32.   
  33. class PrintJob implements Item  
  34. {  
  35.  private String line;  
  36.    
  37.  public PrintJob(String s)  
  38.  {  
  39.   line = s;  
  40.  }  
  41.    
  42.  public void process()  
  43.  {  
  44.   System.out.println(  
  45.    Thread.currentThread().getName() +   
  46.    " consuming :" + line);  
  47.  }  
  48. }  
The above consumer can be tweaked in a host of different ways to make it more flexible. We can define what the consumer will do when production is done. It may be tweaked to allow batch processing but I leave that to the user. Feel free to use it and twist it in whatever way you want.

Most common error : java.lang.UnsupportedClassVersionError: when is it thrown by a JVM & how to resolve it | Experinced level java interview question

UnsupportedClassVersionError is (evidently a descendant of the Error class in Java) which subclasses java.lang.ClassFormatError which in turn subclasses java.lang.LinkageError (which is a direct subclass of java.lang.Error). This error is thrown in the cases when the JVM (Java Virtual Machine) attempts to read a class file and finds that the major and minor version numbers in the particular class file are not supported. This happens in the cases when a higher version of Java Compiler is used to generate the class file than the JVM version which is used to execute that class file. Please do notice that this is not true vice-versa which means you can comfortably use a higher version of JVM to run a class file compiled using an earlier version of Java Compiler. Let’s understand why? We'll see below a valid Java program and subsequently an alert and a stack trace of the error which are thrown as a result of the above explained situation which causes UnsupportedClassVersionError. FYI: The output

public class TestFinally {

      public static void main(String[] args) {
             
            try{
                  try{
                        throw new NullPointerException ("e1");
                  }
                  finally{
                        System.out.println("e2");
                  }
            }
            catch(Exception e){
                        e.printStackTrace();
            }
      }
}



The error alert and the stack trace if the right JVM is not used



java.lang.UnsupportedClassVersionError: TestFinally (Unsupported major.minor version 49.0)
      at java.lang.ClassLoader.defineClass0(Native Method)
      at java.lang.ClassLoader.defineClass(ClassLoader.java:539)
      at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:123)
      at java.net.URLClassLoader.defineClass(URLClassLoader.java:251)
      at java.net.URLClassLoader.access$100(URLClassLoader.java:55)
      at java.net.URLClassLoader$1.run(URLClassLoader.java:194)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.net.URLClassLoader.findClass(URLClassLoader.java:187)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:289)
      at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:274)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:235)
      at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:302)


The minor and major version numbers are represented by the major_version and minor_version items respectively in a Java Class File structure. A combination of these two unsigned integers (both of length 2 bytes each) determines the particular version of the class file format. If a class file has major version number M and minor version number m, we denote the version of its class file format as M.m.

As per Java VM Spec, “A Java virtual machine implementation can support a class file format of version v if and only if v lies in some contiguous range Mi.0 v Mj.m. Only Sun can specify what range of versions a Java virtual machine implementation conforming to a certain release level of the Java platform may support.” For example: Implementations of version 1.2 of the Java 2 platform can support class file formats of versions in the range 45.0 through 46.0 inclusive.

Major version number of the class file formats of the popular Java versions are: J2SE 6.0 = 50, J2SE 5.0 = 49, JDK 1.4 = 48, JDK 1.3 = 47, JDK 1.2 = 46, JDK 1.1 = 45. Now you understand what in our example “major.minor version 49.0” signifies? It simply means we have used J2SE 5.0 for compiling the source code as the class file format is 49.0 where major_version is ‘49’ and minor_version is ‘0’.

Errors are never detected at compile time. This is quite easier to understand in this case – how can the compiler have the information at the time of compilation about which version of JVM would be used execute the compiled class file? It can't, right? So is the case with other errors as well. This is the reason why all the Errors are unchecked. The error alert also rightly shows that it's a JVM Launcher error.

How to resolve UnsupportedClassVersionError?


Whenever you encounter this error, do check if you’re using an earlier version of JVM to execute the class file than the corresponding version of compiler you used to compile the source code. The example shown here was compiled using Java 5.0 compiler, but when I tried to run using JVM 1.4, I got the above error. I just needed to switch either to a JVM version 5.0 or above OR needed to switch to a Java Compiler of JDK 1.4 or below (you of course need to make sure the source code is compatible with the corresponding version of the compiler otherwise you’ll start getting other compiler errors).

A higher JVM version doesn’t cause a problem in most of cases unless the class file format is quite old (and hence doesn’t lie in the supported range as specified by Sun for that particular JVM version ... as discussed above). But, it’s always a good practice to have both the Compiler and the JVM of the same version.

How to get name of a Class in Java from within a static method | Fresher level java interview Question

One of our regular visitors (Ranvijay) had asked it a couple of days back. Though, the code is pretty straightforward, I think it might help few others if posted as an article.

One of the many possible ways of finding the name of the class in Java from within a static method is by using a static nested class, which will have a public instance method returning the class name of the enclosing class (in our case the class where we have the particular static method).


public class GetClassNameInStaticMethod {

public static void main(String[] args) {
 // Calling a Static Method
 System.out.println("Current Class Name: " + getCurClassName());
}
public static String getCurClassName(){
 return (new CurClassNameGetter()).getClassName();
}

//Static Nested Class doing the trick
public static class CurClassNameGetter extends SecurityManager{
 public String getClassName(){
  return getClassContext()[1].getName();
 }
}
}

Output

Current Class Name: GetClassNameInStaticMethod

How to recover Shift + Deleted folders/emails in Outlook 2013 | Outlook email recovery

How to recover Shift + Deleted folders/emails in Microsoft Outlook?

Ever wondered if you can really recover something in Windows, which you have Shift + Deleted? Well, you can do that at least in few cases (if not in all) - one of them being recovering folders/emails which you accidentally Shift + Deleted in your Microsoft Outlook (running with MS Exchange Server). I managed to delete one important folder this weekend and only then I could realize the importance of this feature.


As you would certainly be aware that a plain 'Delete' moves the particular email to the 'Deleted Items' folder, from where you can easily restore them unless they get permanently deleted from that folder once one of the two situations occur - either the Deleted Items folder exceeds its quota of storage or the deleted emails become stale more than the allowed number of days (which I guess is 20/30 days) - whichever happens earlier.


But, it's interesting to know that even a Shift + Delete, does not really instantly delete all the items permanently - not at least in case of Microsoft Outlook. It's just that you probably can't see an option to recover them with your default settings. You need to add one registry entry and that's it. They will be visible to you then and you can recover the same way as you restore deleted items from the 'Deleted Items' folder. How many of these emails, folders, or emails within the folders you can recover, again depends upon the two parameters - storage quota and the age of the Shift + Deleted emails. So, you probably need to act fast whenever you manage to lose your folders/emails accidently. Find below the steps which are required for this recovery:-


Registry Entry:
if not already there (it's required to be added only once after the installation of the client application), you would be required to add the below entry in your Windows Registry. Below are the steps:-


  • Run -> regedit
  • expand HKEY_LOCAL_MACHINE
  • expand and go to SOFTWARE
  • expand and go to Microsoft
  • expand and go to Exchange
  • expand and go to Options
  • add the entry 'DumpsterAlwaysOn REG_DWORD 0x00000001 (1)'

Registry entry for recovering Shift+Deleted emails in Outlook
Microsoft Outlook -> Tools -> Recover Deleted Items...: If you want to recover a folder then simply select the corresponding mailbox and select 'Recover Deleted Items...', which will enable you select and recover the Shift + Deleted folders of that mailbox. If you want to recover emails then simply select the corresponding folder and do the same. It will show you all the emails of that folder available for you to be recovered (based on storage quota and age of the emails as discussed above). Please find below the sample screenshots for your reference:

Recover Deleted Items in Tools menu after registry entry
Recovering emails window where you can select which all to recover

IBM Websphere commerce server(WCS) interview question in Sapient date 30.04.2013 | IBM WCS Interview Question in MNC Software Company 2013

I have attended interview of Company Sapient last week for IBM WCS developer they have asked following interview question below

1. Explain Catalog data model in WCS
2. Difference between controller command and task command 
3. Write Hello world in WCS programming model
4. Explain Data bean type.
5. Jsp tag in WCS
6. Sales catalog master catalog
7. Define excepion in IBM WCS.
8. Explain control command mehods.
9. Explian order flow.
10. Can two main() method in java class
11. Difference between HashMap and HashTable.
12  implicit object in JSP.

Setting of Java classpath in Windows , Unix, Linux operating System.| What is Use java classpath

Classpath in Java is path to directory or list of directory which is used by 
ClassLoaders to find and load class in Java program. Classpath can be specified 
using CLASSPATH environment
 variable which is case insensitive, -cp or -classpath command line option or 
Class-Path attribute in manifest.mf file inside JAR file in Java.  In this Java 
tutorial we will learn What is Classpath in Java, how Java resolve classpath and 
How Classpath works in 
Java along side How to set classpath for Java in windows and UNIX environment. 
 I have experience in finance and insurance domain and Java is heavily used in this domain for writing sophisticated Equity, Fixed income trading applications. Most of these investment
 banks has written test as part of there core Java interview questions and
 I always find at least one question related to CLASSPATH in Java on those 
interviews. Java CLASSPATH is one of the most important concepts in Java, 
 but,  I must say mostly overlooked. 
This should be the first thing you should learn while writing Java programs 
because without correct understanding of Classpath in Java you can't understand 
how Java locates your class files. Also don't confuse Classpath with PATH in Java,
 which is another
 environment variable used to find java binaries located in JDK installation 
directory,
 also known as JAVA_HOME. Main difference between PATH and CLASSPATH is that 
former is used to locate Java commands while later is used to locate Java class
 files.

So let’s start with basic and then we will see some example and improvisation 
of Classpath in Java. In fact CLASSPATH is an environment variable which is used 
by Java Virtual Machine
 to locate user defined classes. As I said In this tutorial we will see How to 
setup 
classpath for java in windows and Linux , java -classpath example in different 
scenario and use of java -classpath or java -cp.

Setting Java Classpath in Windows

In order to set Classpath for Java in Windows (any version either Windows XP, 
 Windows 2000 or Windows 7) you need to specify value of environment variable 
CLASSPATH, name of this 
variable is not case sensitive and it doesn’t matter if name of your environment
 variable is Classpath, CLASSPATH or classpath in Java.

Here is Step by Step guide for setting Java Classpath in Windows:

Go to Environment variable window in Windows by pressing "Windows + Pause “-->
Advanced -->Environment variable " or you can go from right click on my computer
 than choosing properties
 and then Advanced and then Environment variable this will open Environment 
variable window in windows.
Now specify your environment variable CLASSPATH and put the value of your 
JAVA_HOME\lib and also include current directory by including (dot or period sign).
 Now to check the value of Java classpath in windows type "echo %CLASSPATH" in
 your DOS command prompt and it will show you the value of directory which 
are included in CLASSPATH.

You can also set classpath in windows by using DOS command like :

set CLASSPATH=%CLASSPATH%;JAVA_HOME\lib;
This way you can set classpath in Windows XP, windows 2000 or Windows 7 and 8,
 as they all come with command prompt.
Setting Java Classpath in UNIX or Linux

To set Classpath for Java In Linux you can simply export
 CLASSPATH="your classpath" from either your .bash_profile or 
.bashrc script which will run whenever you login into your 
Linux or Unix Machine. Now to check value of Java CLASSPATH in Linux type
 "echo ${CLASSPATH}" 
this will print value of Classpath in command prompt. By using export
 command you can set classpath for Java in Unix, Linux, Solaris, 
IBM AIX or any other UNIX operating system. 
I hope this example for setting classpath in Java will enable to set 
classpath by yourself 
let me know if you face any problem while setting up classpath in Java

Overriding Classpath in Java

You can override classpath in Java, defined by environment variable CLASSPATH by providing option "-cp" or "-classpath" while running Java program and this is the best way 
to have different classpath for different Java application running on same Unix or Windows machine,
 standard way to define classpath for Java application is creating start-up script for Java program and set classpath there as shown below :

CLASSPATH=/home/tester/classes
java -cp $CLASSPATH Test

By default Java CLASSPATH points to current directory denoted by "." and it will look for any class only in current directory.

Different example of using Classpath in Java

In case you have multiple directories defined in CLASSPATH variable,
 Java will look for a class starting from first directory and only look 
second directory in case it did not 
find the specified class in first directory. This is extremely useful
 feature of Classpath in
 java to understand and it’s very useful while debugging Java application or  patch release kind of stuff. Let’s see  java -classpath example

I have set my classpath environment variable as CLASSPATH=/home/tester/first:/home/tester/second. Now I have Test class of different version in both first and second directory.
 When I give a command "java Test" What will happen ? Which Test class would be picked? Since 
JVM search directory in the order they have listed in CLASSPATH variable it will first go to the "first" directory and if it finds Test.class over there it will not go to 
/home/tester/second directory. Now if you delete Test.class from /home/tester/first directory it
 will go to /home/tester/second directory and will pick  Test.class from there.

I have used this feature of Java Classpath to test my patch releases, we used to have a folder called "patch" listed as first element in Java CLASSPATH and any point of
 time we want to put any debug statement or want to test any bug we just modify Java source file ,
 compile it and generate class file and put that inside patch folder instead of creating JAR file and releasing whole new Java application. This is very handy if you are
 working in a large project where you don't have development environment setup in Windows and your
 project only runs on Unix server. This approach is much faster than remote debugging Java application in Eclipse

Its also worth noting that when you use the  java -jar command line option to run your Java program as an executable JAR, then the CLASSPATH environment variable will be
 ignored, and also the -cp and -classpath switches will be ignored. In this case you can set your Java
 classpath in the META-INF/MANIFEST.MF file by using the Class-Path attribute. In short Class-path attribute in manifest file overrides classpath specified by -cp, -classpath or CLASSPATH environment variable.

Now a common question if I have my CLASSPATH variable pointing to current directory "." and I have class called "Test" inside package "testing" and with below directory 
structure C:\project\testing\Test.class in my computer.

What will happen if I run command "java Test" from directory "C:\project\testing\"? will it run?
No it will not run it will give you :
Exception in thread "main" java.lang.NoClassDefFoundError: Test
Since name of the class is not Test, instead it’s testing.Test even though your classpath is set to current directory.

Now what will happen if I give command  java testing.Test from C:\project\testing\ it will again not run and give error?

Exception in thread "main" java.lang.NoClassDefFoundError: testing/Test

Why because now it looking for class called Test which is in package testing, starting from current directory "." but don't find it since there is no directory
 called "testing after this path "C:\project\testing\".

To run it successfully you need to go back to directory  C:\project and now run 
C:\project>java testing.Test  and It will run successfully because of Classpath issues i prefer to use Eclipse rather than running Java program from command prompt. 

Errors related to Classpath in Java

If you are working in Java you must have faced some errors and exception related to classpath in java, two most common issues related to java classpath is 
ClassNotFoundException and NoClassDefFoundError. I have seen that many Java developer tries to solve this 
error by trial and error; they just don’t look beyond the hood and try to understand what the reason for these java classpath related errors is. 
They often misunderstood that these two errors are same also. 

Here is the reason of these Java classpath errors: 

ClassNotFoundException is an Exception and will be thrown when Java program dynamically tries to load a Java class at Runtime and don’t find corresponding 
class file on classpath. Two keyword here “dynamically” and “runtime”. Classic example of these errors is 
whey you try to load JDBC driver by using Class.forname(“driver name”) and greeted with java.lang.ClassNotFoundException: com.mysql.jdbc.Driver. So this 
error essentially comes when Java try to load a class using forName() or by loadClass() method of ClassLoader. Key thing to note is that presence of that
 class on Java classpath is not checked on compile time. So even if those classes are not present on Java classpath your program will compile successfully and only fail when you try to run.

On the other hand NoClassDefFoundError is an Error and more critical than ClassNotFoundException which is an exception and recoverable. NoClassDefFoundError 
comes when a particular class was present in Java Classpath during compile time but not available during run-time. Classic example of this error is using log4j.jar 
for logging purpose and forgot to include log4j.jar on classpath in java during run-time. to read more about logging in Java see . Keyword here is,  class was present 
at compile time but not available on run-time.  This is normally occurring due to any method invocation on a particular class which is part of library and not available on classpath in Java.
 This is also asked as common interview questions as  
“What is difference between NoClassDefFoundError and ClassNotFoundException Exception in Java”   or
“When do you see NoClassDefFoundError and ClassNotFoundException Exception in Java”. By the way NoClassDefFoundError can also comes due to various other reason
 like static initializer failure or class not visible to Classloaders in J2EE environment. read 3 ways to resolve NoClassDefFoundError in Java for complete details.


Summary of CLASSPATH in Java

1.      Classpath in Java is an environment variable used by Java 
Virtual machine to locate or find  class files in Java during class loading.

2.      You can override value of Classpath in Java defined by 
environment variable CLASSPATH by providing JVM command line option –cp or –classpath while running your application.

3.      If two classes with same name exist in Java Classpath 
then the class which comes earlier in Classpath will be picked by Java Virtual Machine.

4.      By default CLASSPATH in Java points to current directory
 denoted by "." and it will look for any class only in current directory.

5.      When you use the -jar command line  option to run your program as an executable JAR, then the Java CLASSPATH environment variable will be ignored, 
and also the -cp and -classpath switches will be ignored and In this case you can set your java classpath in
 the META-INF/MANIFEST.MF file by using the Class-Path attribute.

6.      In Unix of Linux Java Classpath contains names of directory with colon “:” separated , On Windows Java Classpath will be  semi colon “;” separated
 while if you defined java classpath in Manifest file those will be space separated.

7.       You can check value of classpath in java inside your application by looking at following system property “java.class.path”  System.getProperty("java.class.path")

Class-Path attribute is used to contain classpath inside manifest file. Also make sure that your manifest file must end with a blank line (carriage return or new line)
 , here is an example of java classpath in manifest file.

Main-Class: com.classpathexample.Demo_Classpath
Class-Path: lib/tibco.jar lib/log4j.jar

8.       It’s also important to note that path specified in manifest file is 
not absolute instead they are relative from application jar’s path. 
For example in above if your application jar file is in C:\test
 directory you must need a lib directory inside test and tibco.jar
 and log4j.jar inside that.

9.       ClassNotFoundException is an Exception and will be thrown when Java
 program dynamically tries to load a particular Class at Runtime and don’t
 find that on Java classpath due to result of Class.forName() or 
loadClass() method invocation.

10. NoClassDefFoundError comes when a particular class was present in Java Classpath during compile time but not available during runtime on Classpath in Java.

I hope you find this Java Classpath tutorial useful , 
please let me know if you have any doubt or any question
 related to "How to set classpath for java" 
and I would be happy to answer :) keep learning. Your
 suggestions and comments are always welcome. 
 If you like to read UNIX command tips you may find  10 
tips of using find command in Linux ,  10 tips to increase
 speed on Unix command and  10 
basic networking Commands in Unix useful. That's all on What
 is Classpath in Java, How Classpath works in Java, How to set 
Classpath in Java on Windows 
and Linux and how to deal with Classpath issues in Java.