Saturday, November 24, 2007

When to use MultiActionController in Spring framework

Generally we have mapping file for dispatcher servlet to decide which controller to use as per URL mapping but if we have to take decision on basis of request parameters then MultiActionController can be used.

Monday, September 10, 2007

Reset System date and Time through Java API

How to reset the Windows system date and time through Java ?

Java API for reset the Windows System date & time
--------------------------------------------

However you can execute any Windows command by using the Runtime class. So you should be able to invoke the Time and Date command. This piece of code will be used to update the date and time using java.

try
{

String newDate = "01-06-04";
String newTime = "8:30";
Runtime.getRuntime().exec("CMD /c \"DATE " + newDate + "\"");
Runtime.getRuntime().exec("CMD /c \"TIME " + newTime + "\"");
}
catch(Exception e){
// Unable to change the date and time
}

Tuesday, February 13, 2007

Java Performance Tips

Using shift operator for faster division and multiplication

Multiplications
              12 * 2     = 12 << 1
              12 * 4     = 12 << 2
              12 * 8     = 12 << 3
              12 * 16   = 12 << 4
              12 * 32   = 12 << 5
              12 * 64   = 12 << 6
              12 * 128 = 12 << 7
              12 * 256 = 12 << 8
Divisions
             12 / 2     = 12 >> 1
             12 / 4     = 12 >> 2
             12 / 8     = 12 >> 3
             12 / 16   = 12 >> 4
             12 / 32   = 12 >> 5
             12 / 64   = 12 >> 6
             12 / 128 = 12 >> 7
             12 / 256 = 12 >> 8

Monday, February 12, 2007

Classpath

Setting the classpath

For example If I have the class
/home/user01/Test.java
/home/user01/f1/TestA .java
/home/user01/f2/TestB .java
/home/user01/f3/TestC .java

TestA {
     public void show() {
           System.out.println("Show from TestA");
     }


TestB {
     public void show() {
           System.out.println("Show from TestB");
     }
}


TestC {
     public void show() {
           System.out.println("Show from TestC");
     }
}

Test {
     public static void main(Stirng atr[]) {
               TestA ta = new TestA();
               TestB tb = new TestB();
               TestC tc = new TestC();
               ta.show();
               tb.show();
               tc.show();

     }
}

If each class reside in different folder we need to set the classpath for all the file, then only the file got compile

[user01@localhost user01] java Test.java
the above command will lead you with error messages

Test.java4: cannot find symbol
symbol : class TestB
location: class TestB
...................

To Compile the above class
javac -classpath /home/user01/f1:/home/user01/f2:/home/user01/f3 Test.java
To Execute the above class
java -classpath /home/user01/f1:/home/user01/f2:/home/user01/f3 Test