Thursday 17 April 2014

Static Import

Static import
Introduced in  Java 5
Import statement was enhanced to provide greater keystroke-reduction capabilities.
 Static import is use when you want to access Class static members.
Example:
File: First.java
import static staticImport.Second.*;

public class First
{
       public static void main(String[] args)
       {
              System.out.println(firstVar);
              printfirstVar();
       }

}

File : Second.java

public class Second
{
              final static int  firstVar= 34;
             
              static void printfirstVar()
              {
                     System.out.println(firstVar);
              }
}

Output:
34
34

Problem:
Ambiguity of static members:
If we import from two classes which use same static variables then compiler will in ambiguity to decide which variable class variable to use.


Example:
File:First.java

import static staticImport.Second.*;
import static staticImport.Third.*;

public class First
{
       public static void main(String[] args)
       {
              System.out.println(firstVar);         // Error since compile not able to decide which firstVar we want to use, either of Second.java or Third.java
             

printfirstVar();                   // Error: reason is same as above
       }

}

File: second.java
public class Second
{
       final static int  firstVar= 34;
             
       static void printfirstVar()
       {
              System.out.println(firstVar);
       }
}

File: Third.java
public class Third
{
       final static int  firstVar= 34;
      
       static void printfirstVar()
       {
              System.out.println(firstVar);
       }
}


Monday 14 April 2014

Java Serialization Example

A simple example of serialization concept in java having logging concept so that you can track object before and after serialization.


Class1 : Person.java

import java.io.Serializable;

public class Person implements Serializable
{
private static final long serialVersionUID = 1L;
String name;
int age;

Person(String name, int age)
{
this.name = name;
this.age=  age;
}

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}

public String toString()
{
return name + " " +age;
}
}

Class 2: First.java (Serialization part )

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.logging.FileHandler;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

public class First {

private static final Logger logger = Logger.getLogger(First.class.getName());
public static void main(String[] args)
{
SimpleFormatter simpleFormatter = new SimpleFormatter();
Person p =new Person("shubham", 24);
try
{
FileHandler log = new FileHandler("D:\\My\\java\\try\\src\\serialization\\log.txt", true);
log.setFormatter(simpleFormatter);
logger.addHandler(log);
logger.info("Object to be serialize   " + p.toString());
FileOutputStream fileOutputStream = new FileOutputStream("D:\\My\\java\\try\\src\\serialization\\abc.ser");
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
objectOutputStream.writeObject(p);
objectOutputStream.close();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}
}


Class 3: Second.java (Deserialization )

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.logging.FileHandler;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

public class Second {

private static final Logger logger = Logger.getLogger(Second.class.getName());
static SimpleFormatter simpleFormatter = new SimpleFormatter();

public static void main(String[] args)
{
try
{
FileHandler log = new FileHandler("D:\\My\\java\\try\\src\\serialization\\log.txt", true);
log.setFormatter(simpleFormatter);
logger.addHandler(log);

FileInputStream fileInputStream = new FileInputStream("abc.ser");
ObjectInputStream objectOutputStream = new ObjectInputStream(fileInputStream);
Person p = (Person) objectOutputStream.readObject();
objectOutputStream.close();
logger.info("Object deserialized is "+p.toString());
}
catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
}
}

Thursday 10 April 2014

Remote debugging using Tomcat with eclipse


Remote debugging using Tomcat with eclipse
The Java debugger has a client/server design so that it can be used to debug programs that run locally (on the same workstation as the debugger) or remotely (on another computer on the network).
Local debugging:
Local debugging is the simplest and most common kind of debugging.  After you have finished editing and building your Java program, you can launch the program on your workstation using the menu item on the workbench.  Launching the program in this way will establish a connection between the debugger client and the Java program that you are launching.  You may then use breakpoints, stepping, or expression evaluation to debug your program.
Remote debugging:
The client/server design of the Java debugger allows you to launch a Java program from computer on your network and debug it from the workstation running the platform.  This is particularly useful when you are developing a program for a device that cannot host the development platform.  It is also useful when debugging programs on dedicated machines such as web servers. 
Steps:
  1. Stop your tomcat if it is in running mode.
  2. Go to Tomcat/webapp/bin. You will find a file catalina.bat (windows)/ catalina.sh (UNIX and Linux).
  3. Catalina.sh have this entry:
JPDA_ADDRESS = 8080
This port number is use for remote debugging. 8080 is default port and you can change it if required.
Write this command on UNIX command interpreter: catalina.sh jpda start
You will see something like this “INFO: Server startup in 4955 ms”.
Now, go to eclipse
Click on debug pick list icon and you will menu like this:



Now select Debug Configurations…
You well get a new window like this:



Now right click on “Remote Java Application” and select “new”.
Debug configuration window now looks like this:



Select an entry for Name field.
Select desired project from Browse button.
Enter Host: (Host name of the machine where tomcat will run.) and valid port number.
Select Apply and click Debug.  
Now you can debug your application as you usually debug on local machine.

[Note: Remote debugging may be quite slow as compared to debugging on local machine]

Sources: http://help.eclipse.org/