Monday 6 October 2014

Exceptions with method overriding in java

In java methods can throw the different checked/unchecked exceptions, this seems to be simple as long as we are talking about the single methods in java but when we deal with method overriding in java the concept of exception which can be thrown by overriding or overridden method are bit tricky. The overriding method cannot throw the new/broader exception than the overridden method is throwing but this case is only true for checked exception and not true for unchecked/runtime exception.

If overriding method is throwing a checked exception which is new/broader than your overridden method exception then your code will not compile.

The combination of base class overridden method and derived class overriding method with respect to exceptions can be categorized into the below five cases as:

1.    Base class method throwing no exception and derived class method is throwing exception.
       •    In case of checked exception code will not compile
       •    In case of runtimeException code will work fine.

2.    Base class method throwing less newer/broader exception than derived class method is throwing.   
       Suppose base class overridden method is throwing ArithmeticException and derived class overriding
       method is throwing general Exception so then code will not compile.
      •    In case of checked exception code will not compile
      •    In case of runtimeException code will work fine.

3.    Both base and derived class overridden method throw same exception
       •    Code will compile and run perfectly in both cases of checked and unchecked exception.

4.    Base class overridden method throwing newer/broader exception than derived class overriding method.
       Suppose base class overridden method is throwing general Exception and derived class overriding
       method is throwing ArithmeticException so then code will compile and work fine.
       •     Code will compile and run perfectly in both cases of checked and unchecked exception.

5.    Base class overridden method is throwing exception but Derived class overriding method is not 
       throwing  exception.
      •    Code will compile and run perfectly in both cases of checked and unchecked exception.

Tuesday 30 September 2014

How to solve JSF issue view could not be restored

Log Error : - javax.servlet.ServletException: viewId:/mysettings/myinvoiceTemplates/BillingTemplate.html - View /mysettings/myinvoiceTemplates/BillingTemplate.html could not be restored.

Issue : - Sometimes when we open a JSF page in the application and then open some other page in new tab with right clicking on the existing page and work on new opened page for 2 or 3 minute. When we come back to the earlier page after 2 or 3 minutes and click on any button then we get this error in log that page could not be restored.

Solution : -  It can be solved with two ways as below:
1). By default JSF store the state of the page at server side. We can change this to client by changing context parameter “state_saving_method” in web.xml  as:
<context-param>
       <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
       <param-value>client</param-value>
</context-param>

After changing the method to client, the state of the page is saved in a input hidden field at client side with the page. This will solve the above problem.
Advantage :-
Lower the terrific to server because for page state container does not need to go to client side.
Disadvantage : - This can lead to more data download when we open the page because now page state is also stored at client side.

2). Add one more context parameter in Web.xml file. It will re-create/restore the expired view.

<context-param> 

      <param-name>com.sun.faces.enableRestoreView11Compatibility</param-name>   

      <param-value>true</param-value> 

</context-param>

How to close the popup window by pressing escape key button or clicking outside of the popup window with jQuery.

For closing the open popup window on the click of escape key or clciking outside the popup window, We need to create one jQuery method in every modal panel .xhtml file or any other .xhtml file which is used to show the popup window as:

<script type="text/javascript">
    jQuery(document).keyup(function (e){
            var clickedID = e.target.id;
            var unicode=e.keyCode? e.keyCode : e.charCode /*This will capture the key pressed event*/
                 if(unicode==27){//Key code for escape key is 27
                 Richfaces.hideModalPanel(#{modalPanelId});
                /*var win = window.open("","_self"); r
                 win.close();*/
                }
</script>


The above method will automatically be called whenever we press any key and if pressed key is escape key then it would close the opened popup window so it will work when we press theescape key.

To close popup window when we click on the outside of the popup window, we have to call “Richfaces. hideModalPanel(‘modelPanel Id’)” on “onmaskclick”  event of rich:modalPanel as:

onmaskclick="Richfaces.hideModalPanel('#{modalPanelId}')"

Monday 29 September 2014

How to compile .JRXML file and produce .jasper file with JAVA code

The Jasper Report design file specifies the layout and appearance of the report and they works best with the .JRXML extension files. We use iReport to deal with the jrxml reports but sometimes we need to compile JRXML file through our java code. We can do this with the help of net.sf.jasperreports package.
Change the path of the JRXML and jasper files in below code and run this code to compile any JRXML file and generate jasper report.

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;


public class compileJasper{

public static void main(String[] args)
    {
    //    below code will compile the jasper file which is located on path given below
        try {
    JasperCompileManager.compileReportToFile("PathOfTheJRXMLFile(which needs to be compiled)" ,"PathOfJasperFile(Where to save generated jasper file)");
        } catch (JRException e) {
           
        }
    }
}

What is the difference between JDK, JRE, JVM


This question is very common in all java interviews. Basically it checks the candidates knowledge of how java works. This question can be extended in further questions like java class loader and how class files are loaded in java etc. but for that firstly we should know that what these three terms actually means

JDK : -
JDK means Java Development Kit. JDK contains Java Run-time Environment (JRE) , Compilers and debuggers required to develop applets and applications.

JRE :-
JRE is subset of JDK, It contains Java Virtual Machine (JVM) along with Java Class Libraries which implement the Java application programming interface (API) form the Java Runtime Environment (JRE).

JVM : -
Java Compilers compile JAVA source code and create a .class file having the byte code. Java Virtual Machine(JVM) interpreters Java byte code and send necessary commands to underlying hardware or Just in time compiled to native machine code and execute it directly on the underlying hardware . If we have already compiled code of java (.class files) then we just only needs the JVM to interpreters .class file codes.

Monday 6 January 2014

How to skip grants in MySQLdatabase or start without password

If you wants to start MySql in 'skip-grant-tables' mode on Windows or without password.

Please follow the steps below
1). Stop the MySQL service through Administrator tools, Services.

2). Modify the my.ini configuration file (assuming default paths)
     C:\Program Files\MySQL\MySQL Server 5.5\my.ini

3). Change the contents as:
      In the SERVER SECTION, under [mysqld], add the following line:
     skip-grant-tables

    As:

    # SERVER SECTION
    # ----------------------------------------------------------------------
    #
    # The following options will be read by the MySQL Server. Make sure that
    # you have installed the server correctly (see above) so it reads this
    # file.
   #
   [mysqld]

   skip-grant-tables

4). Start the service again and you should be able to log into your database without a password.
share|improve this answer