How to reset the root password on Linux

Today, i had a big hassle earlier on by accidentally changed my linux password policy and set the expiration for all the password a date before the current time :) That proves again Monday Morning is always not the most effective day during the week. LOL

But i managed to reset the password following this video, very good one.

Put it here in case you guys make the stupid mistake as i did today :)

No Comments

Java Exception study guide

Checked Exception

ParseException

eg. DataFormat.parse(String source) : Date, if the source is not good format string like 12/01/2010, it throws this.

ClassNotFoundException

  • The forName method in class Class.
  • The findSystemClass method in class ClassLoader .
  • The loadClass method in class ClassLoader.

InterruptedException thrown when a thread is waiting, sleeping or pausing.

eg. Thread.sleep()/wait()/join()  yield() is not throwing any exception

Runtime Exception

NoSerialzableException

eg. when one object gets serialized, its attributes are not serialized or its collaborator is not serializable

IllegalThreadStateException start the same thread more than once

IllegalMonitorStateException wait(),notify(),notifyAll() is not in the synchronized context

ClassCastException

eg1.

Object o =  new String(“jay”);

Integer i = (Integer)o;

eg.2 adding object of class type not comparable to TreeSet or TreeMap, It fails and throws CCE when it tries to cast the type to Comparable and sort the elements added.

note: this is exactly where it passes the compile time. During the runtime, ref type points to the object it can’t. ClassCastException gets thrown.

ArrayIndexOutOfBoundsException

IllegalArgumentException when method receives an argument formatted differently

IllegalStateException when

eg. matcher.group() is called while there is no match. So that’s why matcher.group() should always be called in side while(matcher.find())

NumberFormatException NFE

eg. Integer i = new Interger(“messy”);

NullPointException

ClassNotDefException Thrown when the JVM can’t find a class it needs. classpath issue or missing a class file.

ArrayStoreException thrown when the object doesn’t fit in the array type.

eg. B extends A, C extends B

A[] a = new C[1];

a[0] = new B(); pass Compile, throw ASE at runtime. cause B IS NOT an A.

No Comments

Jave Regular Expression study guide

Metacharacters:

\d : A digit

\w : A word char

\s : A white space char

[] : match one of the char contained in the square bracket

Quantifiers:

* Zero or more occurrences

? Zero or one occurrence

+ 1 or more occurrence

The Predefined Dot:

. any char

NOTE:

conversion type is used for formatting NOT for regex.

eg. printf(), format() …

bcdfs

b-boolean, c-char, d-integer, f-float,s-string

escape sequence:

\n = line feed

\b = backspace

\t = tab

,

No Comments

Java Assertion Study note

In SCJP exam, You are expected to know appropriate and inappropriate assertions

appropriate:

1. Do check private method

2. check code never should be reached

inappropriate:

1. check public method

2. check command-line arguments

3. assertion check will bring side affects

eg.assert a=a+1 // a’s value will be varied depending on whether assertion is enabled or not.

,

No Comments

Java Interface Study note

1. All methods are implicitly public abstract.

2.All variables are implicitly final static constants.

3.Interface CAN NOT have static method.

WHY : Java allows mult-interface implementation, what the child should inherit if two interfaces have the same static method?

4. casting a class to interface is always legal in Java during compile.

eg.

for A class, you can cast it to an interface, this rule apply to instanceof as well.

eg1.Runnable r = (Runnable)new A();

note:

but runtime if A has no relationship with Runnable, will throw Runtime Exception(ClassCastExcetption)

if A is a final class and has no relationship with Runnable, it will fail compile.

eg2.A a = (A)r; // this is legal as well.

eg3.$Left instaceof $Right, Left can be any objects,but no interface(fail the build), right can be interfaces

note:

again, compile will fail when $left is an ojbect of a final class, $right is an interface having no relationship with $left.


,

No Comments

Java Thread Study note

1.A new Thread gets started means a new stack gets kicked off apart from the main stack

2. InterruptedException(checked exception) will be thrown when a thread is waiting, sleeping and pausing for long time. (sleep(), wait() and join())

note:

yield() is not throwing this exception.

3.A thread can’t be started again once it’s started. Trying to start a thread more than once will throw IllegalThreadStateException(Runtime Exception)

4.wait(),notify(),notifyAll() are inherited from Object class. They have to be surrounded by the synchorized context. Otherwise it throws IllegalThreadMonitorException(Runtime Excpetion).

eg. sychronized(a) {

….

a.wait()

….

}

5. only thread can be started, not runnable.

6.Thread constructor can take  Runnable or String(name of the thread).

Test Tips:

before you go any further, check the following!!!

Compile

1. start() – check if it is against Runnable – compile error

2. wait(),join(),sleep() – InterruptedException is handled or not – compile error

3.Thread.sleep() is ok, sleep() it self can be only called in the subclass of Thread. so class implements Runnable has to call Thread.sleep() not sleep() directly.

Runtime

1. wait(),notify(),notifyAll() needs to be in synchronized context – runtime exception IllegalMonitorStateException

2. the same thread gets called start() twice – runtime exception – IllegalThreadStateException

No Comments

Java Inner Class Study note

Just quick sum-up what we should be careful during the SCJP exam when you see inner class questions.

CAN

1. inner class can be defined in method, in class, as arugments as well.

2.Method local inner class can only have modifier abstract and final.

CAN NOT

1. inner class CAN NOT have static methods. Neither static variable unless they are public static final constants

WHY

Because an inner class is implicitly associated with an instance of it’s outer class, it cannot define any static methods itself. Since a static nested class cannot refer directly to instance variables or methods defined in its enclosing class, it can use them only through an object reference, it’s safe to declare static methods in a static nested class.

2. method local inner class CAN NOT access method local variables unless they are Marked as final.

WHY

Simply because, the method local inner class is like an local variable. When the method completes, the object might still be passed by reference on the heap refered and used by some other code. The method local variable just can’t live as long as the method-local inner class object .

3.static inner class (this is not actually considered as regular inner class) CAN NOT have access to outer class’s instance members (non-static methods + instance variables)

4.static inner class can not extend a non-static inner class(Just burn this in mind)

WHY

Just like static method can’t directly access instace method, it has to be via object of the class. Static class can’t have the access to instance members.

4.inner class can be private+abstract, but CAN NOT be final+abstract.

WHY

it’s easy to tell , rite? hehe, just think about it.


,

No Comments

How to mount remote windows drive to linux

To remotely mount your windows driver to linux:

eg. mount d drive,

mount -t cifs -o username=${user},password=${password} //${IP}/d$ /mnt/windowsdriver

t: file system tile (cifs or nfs, ntfs)

o:options

,

No Comments

SCJP – scenarios you won’t get java compiled

  • Arrays.length VS String.length()

Most of the test tricks you by eg. args.length(), it will fail the compile. So don’t bother to figure out what results will be after execute. It won’t compile.

  • Ambiguous

This is a trick one, we will hit more ambiguous compile error in this article. One class implements/extends more than one parents. The parents have a same variable, the child has no idea which one gets inherited from what parent. Compile is confused and has no idea what to do. It gives you compile error(ambiguous).

eg. class A{int a;}

Inteface B{int a;}

FAIL: class C extends A implements B{ compiler has no idea which a to inherit}

eg.public void test(int… i) VS public void test(Integer… i)

both in the same class, when we make a call like test(4), compile fails as ambiguous call is made

  • instanceof

instanceof takes object on the left and interface/class on the right.So primitive will fail instanceof

eg. FAIL: int i=1;

i instanceof Integer is not legal, compile error.

  • binarySearch takes(List,key)

eg. Collection c = new ArrayList();

c.add(“shengjie”);

…..

Collections.binarySearch(c, “shengjie”); // compile error, c needs to have a explicit downcasting (List)c

  • Assignment

List <– ArrayList  OK – Polymorphism

Integer <– int OK – Auto-boxing

Integer[] <– int[] FAIL -  type is fixed when array initialised

  • passing param to method without explicit downcasting if downcasting is needed

  • Calendar.getInstance(Locale), SCJP test always tries to pass a date or smth else to Calendar.getInstance(Date) Compile Fail

  • When Overriding – It’s Public String toString(){} NOT Public void toString(){}

  • Catch Exception Sequence

sub-exception has to be caught before the super-exception, so the below will have compile error

eg.try{

}catch(RuntimeException){

}catch(IllegalStateException){ // this can be thrown by calling matcher.group() when matcher.find() is not true

}

  • Private Inner Class can’t be called/accessed by other classes

unlike the private fields can be accessed by getter/setter by other classes, inner class can’t be accessed by other classes. If the code snippet is trying to access the private inner class object, it fails the compile

  • Casting a class to anther class type which is not in the same hierarchy tree

  • static method tries to access Instance Member/Static class tries to access Instance Member

Answer to WHY is pretty simply, because static goes with class other than instance. So…bear it in mind. Static class can be thought as static member of the enclosing class.

  • Final field is declared and no initialization

public final int i; Compile Error

public final int i=0; OK

  • Arrays.reverse() doesn’t exist

Arrays : sort() binarySearch()

Collections: sort() binarySearch() reverse() reverseOrder()

Don’t let the test play you like a fool!!!

  • wait(),notify(),notifyAll() throws InterruptedException which is not handled

either catch it or throw it, doing nothing will fail the compile

  • Having unreachable code

eg.

public void test() throws Exception{

throw new RuntimeException();

throw new anotherException(); // this is ok, i am not sure why, just burn into your mind

int i = 0; // this never gets reached, so it fails the compile, tricky, yeah :) LOL

}

,

No Comments

J2EE – JNDI

To understand JNDI, the only thing you need to learn is naming.

Very simply example,

DNS is a naming convention which is used to map the domain to IP. Such as www.test.com – 9.161.142.75.

Another difficult thing to understand is LDAP (Lightweight Directory Access Protocol). most likely you will see it from the product where you have database storing all the users and you also have a LDAP server mapping all ther users as well (very popular usage).

Thus the LDAP name fn=Shengjie Min, p=SiChuan, c=CN names an LDAP entry fn=Shengjie Min, relative to the entry P=SiChuan,which in turn, is relative to C= CN.

So , back to the JNDI topic. JNDI allows us to look up any java object or LDAP directory based on the context. Hmm… not following..

Ok, one step by another,

1. Bindings

The association of a name with an object is called a binding. For example, a file name is bound to a file.

The DNS contains bindings that map machine names to IP addresses. An LDAP name is bound to an LDAP entry.

2. Context

A context is a set of name-to-object bindings

Examples are better than anything :) That’s why i like them.

eg. you have a few database source(JDBC), let’s say they are a few databases. The context would be those databases entries. And to find one of the specific data source out of them, you will need to do a look up among those entries(context).

Given a code snippet to help out:

Context initCtx = new InitialContext(); // actually, to create a brand new initialContext, which can take hashmap as argument, you construct the hashmap to contain name-value pair. Just think of it as defining the scope of the look up

Context envCtx = (Context) initCtx.lookup(“java:comp/env”);

DataSource ds = (DataSource) envCtx.lookup(“jdbc/raDataSource”);

No Comments