Tuesday, 14 August 2012

How to check the processes in use from command prompt

Many a times, while configuring the web server, we may have to configure the connection port and server port. If the port is already in use then we get error.

The server may already be running in another process, or a system process may be using the port. To start this server you will need to stop the other process or change the port number(s).


To fix this, we need to find the running process in that particular port.
1. Type netstat -ao in the command window. This is will show the port and PID in use.
2. Check the task manager, for the respective PID. This shows the program blocking the port. You may then decide if it is better to stop the program or change the port number...

Monday, 6 August 2012

Java annotations

Java programming language has built in annotations which helps us to add  or put some metadata to our code. This metadata is to provide some information on the classes, methods, variables or packages.

Annotations , which may be applied to the code are as follows:

1.@override:

This annotation is applied to a function to see if its been over ridden correctly,to that of that of its parent class  function. A compiler error is thrown, if the method is not over ridden as that of its super class(not immediate though.any one of the super class).

For example.,

public class Baseclass{

public void test(){

System.out.println("do stuff");

}



}

class Derivedclass extends Baseclass

{

@Override

public void Test() //compiler error..
{
}
}

Here the compiler would expect the name of Test() of Derivedclass to match that of the test() in Baseclass.Code will compile if its changed to test().By this annotation , compiler ensures that we have over ridden the method correctly avoiding any typing errors.


2.@deprecated:

Some times there might be a function written with more powerful feature than the existing one. As most of the people might have used the older one, to give them time to accommodate new change as well as to avoid using it again and warn the developer that the function may be removed anytime in future, methods are deprecated.

@deprecated is a built in annotation used to let the developer know, they are using a deprecated method.

Example:
public class Annotatefunction {
public static void main(String[] args) {
Derivedclass dc = new Derivedclass();
String result = dc.test(); // test is shown as deprecated though    no error is thrown
}
}
class Derivedclass extends Annotatefunction
{
@Deprecated
public String test()
{
return "";

}
}

3. @SuppressWarnings :
Here is a simple example to show the usage of SupressedWarning..
As the name says, it is used to suppress any of the warning messages thrown by the compiler.

For example.,
public class Annotatefunction {
public static void main(String[] args) {
Derivedclass dc = new Derivedclass(); //IDE shows warning that value of the local variable is not used.
}
}
class Derivedclass extends Annotatefunction
{
@Deprecated
public String test()
{
System.out.println("hi");
return "true";
}
}



Introducing this annotation for the main method., the warning could be suppressed.

@SuppressWarnings("unused")
public static void main(String[] args) {
Derivedclass dc = new Derivedclass(); //no warning is thrown now
}

For the list of parameters used for suppresswarnings, please refer to the eclipse documentation.
at ECLIPSE document site.

For quicker reference, i have pasted the content from above website...

allDeadCodedead code including trivial if(DEBUG) check
allDeprecationdeprecation even inside deprecated code
allJavadocinvalid or missing javadoc
assertIdentifieroccurrence of assert used as identifier
boxingautoboxing conversion
charConcatwhen a char array is used in a string concatenation without being converted explicitly to a string
compareIdenticalcomparing identical expressions
conditionAssignpossible accidental boolean assignment
constructorNamemethod with constructor name
deadCodedead code excluding trivial if (DEBUG) check
dep-annmissing @Deprecated annotation
deprecationusage of deprecated type or member outside deprecated code
discourageduse of types matching a discouraged access rule
emptyBlockundocumented empty block
enumIdentifieroccurrence of enum used as identifier
enumSwitchincomplete enum switch
fallthroughpossible fall-through case
fieldHidingfield hiding another variable
finalBoundtype parameter with final bound
finallyfinally block not completing normally
forbiddenuse of types matching a forbidden access rule
hashCodemissing hashCode() method when overriding equals()
hidingmacro for fieldHiding, localHiding, typeHiding and maskedCatchBlock
indirectStaticindirect reference to static member
intfAnnotationannotation type used as super interface
intfNonInheritedinterface non-inherited method compatibility
intfRedundantfind redundant superinterfaces
javadocinvalid javadoc
localHidinglocal variable hiding another variable
maskedCatchBlockhidden catch block
nlsnon-nls string literals (lacking of tags //$NON-NLS-<n>)
noEffectAssignassignment with no effect
nullpotential missing or redundant null check
nullDereferencemissing null check
over-annmissing @Override annotation
paramAssignassignment to a parameter
pkgDefaultMethodattempt to override package-default method
rawusage a of raw type (instead of a parametrized type)
semicolonunnecessary semicolon or empty statement
serialmissing serialVersionUID
specialParamHidingconstructor or setter parameter hiding another field
static-accessmacro for indirectStatic and staticReceiver
staticReceiverif a non static receiver is used to get a static field or call a static method
superoverriding a method without making a super invocation
suppressenable @SuppressWarnings
syncOverridemissing synchronized in synchronized method override
syntheticAccesswhen performing synthetic access for innerclass
tasksenable support for tasks tags in source code
typeHidingtype parameter hiding another type
uncheckedunchecked type operation
unnecessaryElseunnecessary else clause
unqualifiedFieldunqualified reference to field
unusedmacro for unusedArgument, unusedImport, unusedLabel, unusedLocal, unusedPrivate and unusedThrown
unusedArgumentunused method argument
unusedImportunused import reference
unusedLabelunused label
unusedLocalunused local variable
unusedPrivateunused private member declaration
unusedThrownunused declared thrown exception
unusedTypeArgsunused type arguments for method
uselessTypeCheckunnecessary cast/instanceof operation
varargsCastvarargs argument need explicit cast
warningTokenunhandled warning token in @SuppressWarnings

4.@Retention:
We can build our own annotations like we declare an interface.
This is how it is done.

Example:

@interface MyFirstAnnotation
{
String method1();
String method2();
}


@Retention are meta annotations and this with its enumerated constants tells how long the annotation has to be retained.
It has three constants. 

1. RetentionPolicy.CLASS
2.  RetentionPolicy.SOURCe
3.  RetentionPolicy.RUNTIME


Default value is RetentionPolicy.CLASS. i.e. If there is no retention policy specified for the annotation then by default it applies the CLASS one.RetentionPolicy.CLASS will store the annotation in class file, but is not available in runtime for any external tool or api(like java refelection) to access.
RetentionPolicy.Runtime will store the annotation in class file and is available in runtime for accessing.
RetentionPolicy.Source will not store the annotation in class file.

Example of how to use the retention annotation on the above code snippet.

@Retention(RetentionPolicy.SOURCE)
@interface MyFirstAnnotation
{
String method1();
String method2();
}

@ symbol before interface says that it is an annotation.