How to read webpage source code through Java?

We might be seeing "view page source" option in all web browsers. Just by right click and by selecting "view page source" it will give us the complete client side source code of that particular page. So how we can get this done by using Java code? For this we need to use URLConnection class which establish the connection to the server and next by using InputStream class we can read complete page content as bytes. 

Next by iterating InputStream instance (byte by byte) we can get the complete page source as bytes and we can store it in a file for our reference. Lets see simple example to read web page source code through Java.


import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

public class URLReader {
 
 public static void main(String[] args) {
  try{
   URL url = new URL("http://docs.oracle.com/javase/6/docs/api/java/net/URLConnection.html");
   URLConnection urlCon = url.openConnection();
   InputStream is = urlCon.getInputStream();
   
   File myFile = new File("C://URLConnection.html");
   if(!(myFile.exists())){ 
             myFile.createNewFile();
   }
   
   FileWriter fWrite = new FileWriter(myFile, true);  
         BufferedWriter bWrite = new BufferedWriter(fWrite); 
   int i=0;
   while((i=is.read()) != -1){
       bWrite.write((char)i); 
   }

   bWrite.close();
   System.out.println("Web page reading completed...");
   
  }catch (Exception e) {
   e.printStackTrace();
  } 
  
 }
}


OUTPUT:
read webpage source code through Java

read webpage source code through Java



wait(), notify(), notifyAll() in Java

 
When we come to multi-threading environment then we need of some communication between each thread like some thread has to perform task after other thread completes its task. In other way some thread has to communicate to other thread that you can takeover the Object monitor from waiting state. Basically we can say its a inter thread communication. For this we can use these methods very effectively and it does the same work for us. 

All these methods are used in Java Multi-threading and used to communicate between each threads under same Object. Just by name itself we can assume that wait() makes the thread to wait or halt for some time, notify() and notifiAll() used to indicate other threads. Lets see each one by briefly and what is the use in multi-threading. 

wait()
Makes current thread to wait and release the lock which acquired on the Object and waits for the other thread to call notify() or notifyAll() method. At the time of waiting other thread will does his process and communicate to waiting threading by calling notify or notifyAll method. One notify or notifyAll method called waiting thread will acquire the lock and it continues the process. There are 2 types of wait() methods in Java and they are 

  • wait() - Waits until other thread calls notify or notifyAll method. This method throws InterruptedException
  • wait(long milliseconds) - Waits up-to given milliseconds and it starts the process. This method also throws InterruptedException.

notify()
Once notify() called it makes waiting thread to acquire the Object and it starts the process. Suppose if more threads are waiting for the Object to acquire then any one thread will be acquire the Object monitor and the choice is arbitrary. 

notifyAll()
Once notifyAll() method called it wake-up all threads that are waiting the Objects monitor. So the difference between notify() and notifyAll() is simple that once notify() method called only one thread will wake-up and by notifyAll() method all waiting threads will go to runnable state. 

Next lets see simple example for using wait() and notify() with perfect example of amount deposit and withdraw from bank. Suppose when we withdrawing the amount if the balance is insufficient then the thread has to wait for deposit thread and then we need to withdraw the amount. For this we will be using wait() in withdraw thread and notify() in deposit thread.


public class BankTrans implements Runnable {
 
 private int amount = 0;
 
 @Override
 public synchronized void run() {
  if(Thread.currentThread().getName().equals("withdraw")){
   System.out.println("Entering to withdraw amount");
   //Trying to withdraw 5000
   if(this.amount >= 5000){
    this.amount -= 5000;
   }else{
    try {
     System.out.println("Insufficient balance and waiting for deposit....");
     wait();
     this.amount -= 5000; 
     System.out.println("Amount withdraw completed successfully :)");
    } catch (InterruptedException e) {
     e.printStackTrace();
    }
   }
  }else{
   //Depositing 10000 in user account;
   System.out.println("Entering to deposit amount");
   this.amount += 10000;
   System.out.println("Amount deposited and notifying....");
   notify();
  }
 }
 
 public static void main(String[] args) {
  BankTrans obj = new BankTrans();
  Thread withDraw = new Thread(obj, "withdraw");
  Thread deposit = new Thread(obj, "deposit");
  withDraw.start();
  deposit.start();
 }
}


OUTPUT:


Entering to withdraw amount
Insufficient balance and waiting for deposit....
Entering to deposit amount
Amount deposited and notifying....
Amount withdraw completed successfully :)





What is Marker Interface in Java?

Marker interfaces are special interfaces in Java. Marker interface will not have any methods or member variables. Just declared with interface name and also called as Tag interface. 

Then what is the use of Marker interface in Java?
Normally interface used to tell compiler about the behavior of the class. Used to tag the implementation of class based on their purpose and behavior. Just used to "mark" the java class which support a certain capability.

Alternate for Marker or Tag interfaces?
Java 1.5 came up with Annotations as an alternate solution for Marker interfaces. Advantage of using annotations are just it makes loose coupling instead of implementing marker interfaces. 
By using Annotations we can easily specify special behavior to the class but it can't act as the super type of the class what we can achieve by Marker interface. 

Is there any Marker interfaces provided by Java?
Below are the list of marker interfaces provided by Java

  • Cloneable
  • Serializable
  • EventListener
  • RandomAccess
  • SingleThreadModel
  • Remote

Advantage of using Marker interface?
Apart from using Java built-in Marker interfaces, we can also create our own Marker interfaces for our application specific needs like classify code, logically to divide the code which does its own process at run-time. For example when we go for API or framework development Marker interface plays the important role like we have in Spring, Struts etc.,

Lets see simple example of using Marker interface in Java


public interface MyMarker {
 // Marker interface without any methods or variables 
}



public class MyMarkerTest implements MyMarker {
 
 public static void main(String[] args) {
  
  MyMarkerTest obj = new MyMarkerTest();
  
  if (obj instanceof MyMarker) {
   System.out.println("obj is an instance of MyMarker");
  } else {
   System.out.println("obj is NOT an instance of MyMarker");
  }
 }
}


OUTPUT:


obj is an instance of MyMarker




What is Covariant return types in Java?

While overriding the methods in subclass name, arguments, and return type must me same as super class method until before Java 1.5. Overriding method is said to be invariant with respect to argument types and return type of super class. Suppose if we changing any argument types then its not actually overriding but its method overloading. Complete method signature must be as same as super class method for overriding till before Java 1.5.

This has been relaxed from Java 1.5, where subclass method can have different return type but only condition is subclass can return type must be a sub-type of super-class return type. Lets see simple example for Covariant return types in Java and how it works,


public class ClassA {
 
 public ClassA getInstance(){
  return new ClassA();
 }
 
 public void sayHello(){
  System.out.println("Hello Class A ");
 }
}



public class ClassB extends ClassA{
 
 @Override
 public ClassB getInstance() {
  return new ClassB();
 }
 
 @Override
 public void sayHello() {
  System.out.println("Hello Class B ");
 }
 
 public static void main(String[] args) {
  ClassA obj = new ClassB().getInstance();
  obj.sayHello();
 }
}


OUTPUT:


Hello Class B 



If we seen in above example we are overriding getInstance() method from super class ClassA. But the return type is different from super class method where we are returning subclass instance here. Even though we are overriding return types are different in both the methods. This is called as Covariant return types in Java.

By this we can remember that from Java 1.5 overriding method can different return type with single condition ie., subclass can return type must be a sub-type of super-class return type.






How to convert list to read-only list in Java?

 
By using Collections class we can convert List to read-only List. This can done by using Collections.unmodifiableList(list) function from Collections class. By this we can't add or remove any element from the List. Not only List we can make other Collection Objects like Map, Set also to read-only as like below.

Collections.unmodifiableMap(map);
Collections.unmodifiableSet(set);
Collections.unmodifiableSortedMap(sortedMap);
Collections.unmodifiableSortedSet(sortedSet);

Lets see small example how to convert list to read-only list.


import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ReadOnlyList {

 public static void main(String[] args) {
  
  List<String> list = new ArrayList<String>();
  list.add("java");
  list.add("discover");
  list.add("threads");
  list.add("oops");
  list.add("servlet");
  
  System.out.println("LIST : "+list);
  
  // Removing "oops" from from list
  list.remove(3);
  
  System.out.println("LIST : "+list);
    
  // Making list to read-only
  list = Collections.unmodifiableList(list);

  // trying to removing "threds" from list
  list.remove(2);
    
  System.out.println("LIST : "+list);
 }
}


OUTPUT:


LIST : [java, discover, threads, oops, servlet]
LIST : [java, discover, threads, servlet]
Exception in thread "main" java.lang.UnsupportedOperationException


If we seen in above program we have added 5 values in list and we have removed 1 value "oops" from the list. Next we have converted list to read-only list by using unmodifiableList() method. Later when we try to remove "discover" value it throws UnsupportedOperationException because of read-only list. Even we can't add any value to this list. 




Daemon Thread in Java

 
Basically in Java there are 2 types of threads and they are Daemon Thread and non-Daemon Thread (User threads). Daemon threads are nothing but service provider for other threads to perform their task under same process like Garbage collection etc., Any Java thread can be a daemon thread and daemon threads life depends on user threads. It will be alive until all other threads are running and JVM will terminate daemon thread automatically once all other threads died. Some of the points to be remembered about daemon thread are 

  • Daemon threads are low priority threads. 
  • Daemon threads life depends on all other user threads (non-daemon threads) in same process.
  • Finally it provides only service to other user threads and nothing more than that.  
  • To specify the thread as daemon thread, just call the setDaemon method with the argument "true"  like  ( Thread.setDaemon(true) ).
  • Setting daemon should be done before starting the Thread, else it will throw IllegalThreadStateException exception. 
  • JVM will automatically exit the process once all user threads are completed and it won't wait for any Daemon thread to complete, no matter how many Daemon threads are running. 

Lets see small example with creating Daemon Thread and user threads under same process and lets see how its work.


public class DaemonThreadTest extends Thread{
 
 String daemonFlag = "";
 
 public DaemonThreadTest() { }
 
 public DaemonThreadTest(String daemonFlag) {
  this.daemonFlag = daemonFlag;
 }
 
 public void run() {
  if(daemonFlag.equals("daemon")){
   while(true){    
    try {
     System.out.println("Daemon Thread Running !!!");
     Thread.sleep(500);
    } catch (InterruptedException e) {
     e.printStackTrace();
    }
   }
  }else{
   for(int i=0;i<5;i++){
    try {
     System.out.println("User Thread Running !");
     Thread.sleep(500);
    } catch (InterruptedException e) {
     e.printStackTrace();
    }
   }
  }
 }
 
 public static void main(String args[]){
    
  DaemonThreadTest daemonThread = new DaemonThreadTest("daemon");
  DaemonThreadTest userThread1 = new DaemonThreadTest();
  DaemonThreadTest userThread2 = new DaemonThreadTest();
  
  // Setting Daemon thread;
  daemonThread.setDaemon(true);
  
  daemonThread.start();
  userThread1.start();
  userThread2.start();
 }
}


OUTPUT:


Daemon Thread Running !!!
User Thread Running !
User Thread Running !
Daemon Thread Running !!!
User Thread Running !
User Thread Running !
Daemon Thread Running !!!
User Thread Running !
User Thread Running !
Daemon Thread Running !!!
User Thread Running !
User Thread Running !
Daemon Thread Running !!!
User Thread Running !
User Thread Running !
Daemon Thread Running !!!

In above sample program we can see Daemon Thread we have set in infinite loop and user threads under looping for 5 times. One all other user thread complete Daemon Thread has be terminated automatically by JVM and process get completed. No more Daemon Thread is running. 

Executing Unix command through Java


In this tutorial we will see about executing Unix command through Java program and printing the output as same as Unix shell command printing. We can achieve by using Process class in Java. 

Process class provides the methods for performing 
standard input from the given process, 
performing standard output to the process, 
waiting for the process to complete its task, 
checking exit status of the process and 
destroying the process (normally its killing the process).

Constructor in Process class:
Process()

Method in Process class:
abstract InputStream getInputStream()
Used to get the standard input stream of sub-process.

abstract OutputStream getOutputStream()
Used to get the standard output stream of the sub-process.

abstract InputStream getErrorStream()
Used to get the standard error stream of the sub-process.

abstract void destroy()
Used to kill the sub-process.

abstract int exitValue()
Returns the exit value for the sub-process.

abstract int waitFor()
Makes current thread to wait, if necessary, until the process represented by this Process object has terminated.


Now lets see about how to execute Unix command using Process class in Java with simple example of listing folder and files from the current Folder. Second output will show you Error Stream result when we try to unzip a file.


import java.io.InputStream;

public class UnixCommandThroughJava {

 public static void main (String args[]) {
     
  String unixCommand = "ls -lh";
     InputStream in = null;
     
     try {
         StringBuilder commandResult = new StringBuilder();
         
         Process p = Runtime.getRuntime().exec(unixCommand, null);
         
         int returnVal = p.waitFor();
         
         if (returnVal == 0){ // no error           
             in = p.getInputStream();
         }else{     // If any error occurred 
             in = p.getErrorStream();
         }
         
         int readInt;
         while ((readInt = in.read()) != -1){
             commandResult.append((char)readInt);                
         }
         
         System.out.println("COMMAND : " + unixCommand + "\n\nOUTPUT : \n" + commandResult.toString());
          
         in.close();
     
     } catch (Exception e) {
         System.out.println("An exception occured while executing command " + unixCommand);
         e.printStackTrace();
     }
 }
}


OUTPUT:


Executing Unix command through Java


Executing Unix command through Java





Heap and Stack in Java

 

We are discussing few interview questions from our earlier tutorials and its a follow-up with same interview questions in Java. Questions are 

How memory managed in java? 
What is Heap and Stack memory? 
What will be stored in Heap and Stack?
Determine the size of Heap and Stack?
Stack vs Heap Pros and Cons?
When to use Heap and Stack?

This is one of the important questions which asked in most of the Java interviews and later interviewer may jump into asking question in Operating System(OS) level memory management also. Lets see above questions one by one and finally its a simple programs where we all can discuss about the memory allocated for it in Java.

How memory managed in Java?
Stack and Heap are the memories allocated by OS to JVM and they both are stored in the computer's RAM (Random Access Memory). 

What is Heap and Stack memory?
Stack:
It's a special region of computer's memory that stores temporary variables created by each functions. Stack uses "FILO" (First In Last Out) data structure, where its managed and optimized by CPU closely.

Heap:
Heap is a region of computer's memory that is not managed automatically by programmer, and is not as tightly managed by the CPU. It is a more free-floating region of memory.


What will be stored in Heap and Stack memory?
Stack:
Methods and the local variables are stored in stack. Also it is to be remembered that the variable references primitive or object references will be stored in Stack.  

Heap:
Objects and its instance variable are stored in Heap. In Java 6 due to "escape analysis" optimization, sometimes objects will be stores in Stack also.


Determine the size of Heap and Stack?
Stack:
Stack is usually pre-allocated and limited in size, because by definition it must be contiguous memory. Also depends on the language, compiler, operating system and architecture. 

Heap:
In general Heap will be dynamically allocated and constantly changing in size. 


Stack vs Heap Pros and Cons?
Stack:
Very fast in access.
Explicitly can't de-allocate variables allocated in Stack.
Memory fragmentation will not happen in Stack, since its managed efficiently by CPU.
Stack size is limited depending on OS.
If Stack runs out of memory, then this leeds to stack overflow could cause program to crash.

Heap:
Slower in access compared to Stack.
No limited on memory size which will be dynamically allocated. 
Heap could cause fragmentation problem, which occurs when memory on the heap is being stored as non-contiguous blocks. 

When to use Heap and Stack?
Its better to choice Stack when we use less memory or small amount of data in usage. Suppose if we don't know about the size or application uses large amount of memory like dynamic array or collections then we need to go for Heap. 


Lets discuss:


public class MyProgram {
 
 private static int value = 10;
 private String str = new String("Java Discover");
 private String name = "Heap and Stack";
 
 public void sayHello(){
  System.out.println("Hello, Java Discover !!!");
 }
 
 public static void main(String[] args) {
  
  String country = "India";
  
  MyProgram obj1;
  
  MyProgram obj2 = new MyProgram();
  obj2.sayHello();
 }
}



Lets comment on the memory usage on above program as what will be stored in Stack and Heap?





Actual use of interface in Java


We may come across with this interview question as "What is the actual use of interface in Java?". By this we may be thinking of, its an alternate for multiple interface which Java doesn't have and we can implement multiple interfaces in a Java class etc.,. Apart from these answers if we see in Object Oriented interface allows to use classes in different hierarchies by Polymorphism. Lets see simple example as how its works and interface used here



public interface Animal { 
 
 public int runningSpeed();
}

public class Horse implements Animal{

 @Override
 public int runningSpeed() {
  return 55;
 }
}

public class Zebra implements Animal{

 @Override
 public int runningSpeed() {
  return 40;
 }
}

public class AnimalsSpeed {
 
 public static void main(String[] args) {
  
  Animal a1 = new Horse();
  Animal a2 = new Zebra();
  
  System.out.println("Horse Speed "+a1.runningSpeed() + " mph");
  System.out.println("Zebra Speed "+a2.runningSpeed() + " mph");
  
 }
}

OUTPUT:

Horse Speed 55 mph
Zebra Speed 40 mph


If we see above example any number of classes, across class hierarchies could implement Animal interface in their own specific way. But still be used by some caller in a uniform way and by perspective of caller, it's just a Animal interface as we can see in AnimalsSpeed class. 

       Animal a1 = new Horse();
Animal a2 = new Zebra();

Welcome your comments for more use of interface in Java.




Using log4j API Logger


In each and every applications we will be seen logger to record the different types of logs like error, information, debug etc., In this tutorial we will see how to use Logger log4j API in our application and its usage. 



There are mainly 3 components like loggers, appenders and layouts. All these 3 components work together to enable developers to log messages according to message type and level and to control at run-time as how these messages are formatted and where they are stored. All these appender or logger settings will be made in the file called log4j.properties file. 

The advantage of using logger over System.out.print is to disable certain log statements while allowing others to print unhindered. This capability assumes that the logging space, that is, the space of all possible logging statements, is categorized according to some developer-chosen criteria. Loggers are named entities. Logger names are case-sensitive and they follow the hierarchical naming rule ie., "A logger is said to be an ancestor of another logger if its name followed by a dot is a prefix of the descendant logger name. A logger is said to be a parent of a child logger if there are no ancestors between itself and the descendant logger." as example given below


log4j.rootCategory=DEBUG, CONSOLE, TESTING 

log4j.logger.org.apache.commons.validator=DEBUG, CONSOLE

log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Threshold=WARN
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=- %-5p %m%n

log4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.File=/user/local/logFile.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.Threshold=DEBUG
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%-4r [%15.15t] %-5p %c %x - %m%n

log4j.appender.TESTING=org.apache.log4j.RollingFileAppender
log4j.appender.TESTING.File=/user/local/application.log
log4j.appender.TESTING.Append=true
log4j.appender.TESTING.MaxFileSize=1024KB
log4j.appender.TESTING.MaxBackupIndex=10
log4j.appender.TESTING.layout=org.apache.log4j.PatternLayout
log4j.appender.TESTING.layout.ConversionPattern=[%d{EEE MMM d HH:mm:ss yyyy}] [%-5p] %c{1}.java(): %m%n


Set of possible logger levels are:
TRACE,
DEBUG,
INFO,
WARN,
ERROR and
FATAL

For more appenders and how to inclide in our project we can get it from the following link - http://logging.apache.org/log4j/1.2/manual.html

Now lets see simple example for how to start with the log4j API in our application with simple example to redirect all logs to a specified log file. For that first we need log4j.jar file (can use any latest version). I have used log4j-1.2.13.jar and we need to create log4j.properties file under classes directory.


log4j.rootLogger=INFO, file
 
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=D:\\logs\\application.log
log4j.appender.file.MaxFileSize=1MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n



import java.util.Date;
import org.apache.log4j.Logger;

public class LogTest {

 static Logger logger = Logger.getLogger(LogTest.class.getName());
 
 public static void main(String[] args) {
  logger.info("Application starts .....................");
  
  long stime = new Date().getTime();
  
  try{
   logger.info("Process Started !!!");
   Thread.sleep(5300);
   logger.info("Process completed !!!");
  }catch (Exception e) {
   logger.error("Exception Occured : "+e.getMessage());
  }
  
  long etime = new Date().getTime();
  float diff = (float)(etime - stime)/1000;
  logger.info("Total time to complete ::: "+ diff +" second(s)");
  
  logger.info("Application Ends .......................");
 } 
}


OUTPUT:


log4j API Logger