Wednesday, June 30, 2010

Our Enggineering college is in 33rd Rank in all over India

My Engg college is in 33rd Rank in India in 2010:

http://www.outlookindia.com/article.aspx?265887

Monday, June 28, 2010

Creating and Executing a Jar file using Command Prompt in Java

Dear reader,

I am writing an article today about creation and execution of Jar file in Java.
A jar file is nothing but a zipped file having Java classes and other files (may be xml, properties, manifest file etc).

Creation of Jar is quite easier however sometimes we face problem while executing a jar which has a main class
and if that requires some supporting Jar files too. By the way, if you know these things, you can ignore this 
article. But I do hope, please go through once and comment if find something better..

//Creating Jar and Running Jar file from Command Prompt
1) First Test your program without creating jar file, whether it is running or not.
//For this, first set classpath in Windows Operating System, we assume our package starts from "D:\smpp_new>" directory:
Directory structure:  D:\smpp_new>com\......\*.java

    Command_Prompt> set classpath=;%classpath%;.

//Compiling and creating packages with Classes in one command
    D:\smpp_new>javac -d . com\logica\smpp\test\SMPPConnector.java

//Running your main class, We assume Main class is "SMPPConnector.java" which is in below package
    D:\smpp_new>java com/logica/smpp/test/SMPPConnector


//If it runs, then fine.....we will go ahead creating JAR and running it....

2) Now create a Folder in parallel to your Starting package.. here in "D:\smpp_new>" as "META-INF". Then create a file "MANIFEST.MF" inside this META-INF folder, now your directory structure will be:
E.g:  D:\smpp_new>com\......\*.java
    D:\smpp_new>META-INF\MANIFEST.MF

//Contents of MANIFEST.MF file:
    Manifest-Version: 1.2
    Main-Class: com.logica.smpp.test.SMPPConnector
    Created-By: 1.5 (Sun Microsystems Inc.)

    //Remember, after writing this, your cursor should be at the end of the line "Created-By: 1.5 (Sun Microsystems Inc.)", 
    //Not below this line. Manifest jar should not have "/n" type of things.

    //In case your jar is dependent on some classpath, which still needs at runtime, please mention it in MANIFEST FILE like below:
    Manifest-Version: 1.2
    Class-Path: ./DecryptionUtilityLib/commons-codec-1.3.jar ./DecryptionUtilityLib/coreUtil.jar
    Created-By: 1.5 (Sun Microsystems Inc.)
    Main-Class: com.ewp.services.DecryptData

//Now create Jar file:
    D:\smpp_new>jar cfm smppconnector.jar META-INF\MANIFEST.MF com\*

//Run your Jar file:
    D:\smpp_new>java -jar smppconnector.jar

Friday, June 25, 2010

Thread safety and Synchronization at Class level in Java

Thread safety and Synchronization at Class level in Java

Can someone throw some light on the behaviour of class/object level monitor in java? Does one takes 
precedence over other? What happens when, class has both synchronized instance methods and synchronized static methods? 
Does calling thread has to acquire both class/object locks?

Answer:
    Those locks are not related, and each needs to be obtained regardless of the other. Namely, if you have:
    
    class Foo{
        static synchronized void staticMethod(){}
        synchronized void instanceMethod(){}
    }

    And then an instance: Foo f=new Foo();
    
    Then you have 2 unrelated monitors (one for class Foo, one for instance f ). Any threads attempting to invoke 
    staticMethod() will need to gain access to the (single) class monitor, and nothing else. Any threads calling 
    f.instanceMethod() will need to gain access to "f" monitor, and nothing else.
    
    If you need any access hierarchy, you'll need to do it programatically, which may look as follows (however, beware 
    - such nested locks pose some dangers of deadlocks unless used with care):
    
    synchronized(Foo.class){
        synchronized(f){
            // my code
        }
    } 

    -------------------------------------------------------------
    A synchronized method acquires a lock before it executes. For a class (static) method, the lock associated with the 
    Class object for the method's class is used. For an instance method, the lock associated with this (the object for 
    which the method was invoked) is used.

    class Test {
        int count;
        synchronized void bump() { 
            count++; 
        }
        static int classCount;
        static synchronized void classBump() {
            classCount++;
        }
    }

    It has exactly the same effect as:
    -------------------------
    class BumpTest {
        int count;
        void bump() {
            synchronized (this) {
                count++;
            }
        }
        static int classCount;
        static void classBump() {
            try {
                synchronized (Class.forName("BumpTest")) {
                    classCount++;
                }
            } catch (ClassNotFoundException e) {
            ...
            }
        }
    }
    -------------------------

    I assume that since we are dealing with 2 different locks then Class lock will lock all static synch methods and 
    'this' lock will lock all synch instance methods.
----------------------------------------------------------
Actually we should follow a standard policy here called "Don't Wake The Zombies", basically, rather than wake up a 
5 year old thread. However, if You ask only one synchronized static method can be called at a time. The answer is yes, 
for a given Class, only one synchronized static method can be called at a time. Synchronized static methods of different 
Classes, however, can be called at the same time, because the lock is on different instances of the Class object.

-------------------------------------------END------------------------------------------

Wednesday, June 23, 2010

Applying Character code in java

Setting Character coding in Java for web applications:

HttpServletRequest request=null;
String charEncoding = "UTF-8"; //Can be taken from some configuration files too.

if(request.getCharacterEncoding() == null)
request.setCharacterEncoding(charEncoding);

Thursday, June 17, 2010

Read a properties file inside jar file

Loading a Properties file into a Jar and running Main class from Jar:
//Issue: I have created my jar file. The main class get some configuration value from a *.properties. This file is contained into jar archive,
but when I run my application, I receive this error: "FileNotFoundException". Here properties file is a member of Jar file only.
//Package structure like:
com/*.java
smpp/test.propeties

Jar File->com/*.class
->smpp/test.properties


//build.xml, for creating jar file


//Java Code, loading properties file in java code
//Main class, main method
InputStream is;
Properties myProp=new Properties();
try {
is = getClass().getResourceAsStream("/smpp/EIF.properties");
myProp.load(is);
}
System.out.println("Size of file :"+is.available());
is.close();

//Here FileInputStream will not work as that is usable only for plain files, not for members inside a jar.
(It works in eclipse, because eclipse loads from the plain files, not from your jar file.)

//Running Jar:
java -jar HelloTest.jar

Wednesday, June 16, 2010

Java Decompiler software

Link for a good Java Decompiler software:
Software name: JD-GUI

http://www.softpedia.com/progDownload/JD-GUI-Download-92540.html