Sunday, 21 December 2014

Important Eclipse Keyboard Shortcuts For Java Programmer


Here I am listing some basic and important eclipse keyboard shortcuts that every programmer should use while developing any application

The benefit of using shortcuts is it helps you to develop your application faster.

The list of shortcuts are as follows:

Ctrl+N                                                
Create New Project
Ctrl+Shift+R                                      
Open Resource (Java files,other files,folder,projects)
Ctrl+W                                                
Close Current Opened File.
Ctrl+Shift+W                                      
Close All Files
F5                                                        
Refresh


Ctrl+C                                                
Copy Some text or line.
Ctrl+V                                                
Paste text or line.
Ctrl+X                                                
Cut text or line
Ctrl+Z                                                
Undo Last Action
Ctrl+Y                                                
Redo Last Action
Ctrl+F                                                  
Open find and replace dialog to find any word or strings of words.
Ctrl+H                                                
Search Entire Workspace for file,words etc
Ctrl+Shift+O                                      
Organize all imports and remove unnecessary imports
Ctrl+A                                                
Select all text in class
Ctrl+I
Correct indention of selected text or of current line or whole class
Ctrl+Shift+F
Format all code in class
Ctrl+/
Comment / un-comment line
Ctrl+Shift+/
Add Block Comment around selection
Ctrl+Shift+\
Remove Block Comment
Ctrl+S                                                  
Save Current File.
Ctrl+Shift+S                                        
Save All Files. 



Important Debug shortcuts :

F11
Debug                                                                       
F5
Step Inside the function
F6
Next step(line by line)
F7
Step out of the function
F8
Skip to next breakpoint



Saturday, 20 December 2014

Clear command line console and display bold size of string in Java Command Line Application


To clean console of your command line application,use following code :


  /**  
   * This method clear the current screen  
   *   
   *   
   */  
   public static void clearScreen() {  
    System.out.print("\033[H\033[2J");  
    System.out.flush();  
   }  



To make bold size of any string,

  public void boldSizeOfString()  
   {  
       String strNormalSize = "\033[0;0m";  
       String strBoldSize = "\033[0;1m";  
         System.out.println (strNormalSize + "My Name is Anthony Gonsalves" + strBoldSize);  
   }  














How to create jar and run from command line


In order to create runnable jar,You need to do following steps in Eclipse:

1) Right Click on your project.Select Export Option and then Select Runnable Jar File Option.
























2) After Clicking Next, select the main class of your project in Launch configuration and Specify the path and name of jar file in Export destination.



3) Click Finish.

To Run Jar File,Follow the steps mentioned below:

1) Make Sure java path is set in Environment variable.
2) Open command Line
3) Go to path where Jar is generated. in this case : cd f:/aapps/
4) Run java -jar Test.jar


Detecting key events in Console application Using JLine


JLine is nothing but a java library for handling console inputs.It can detects various keys which System class can not detect.

For ex:you can not detect ESC key or ENTER key pressed in java.

JLine ConsoleReader object can detects special key using its readVirtualKey() function.
For more information about JLine.

You can download the jar from http://www.java2s.com/Code/Jar/j/Downloadjlinejar.htm.

For maven dependency refer this http://mvnrepository.com/artifact/jline/jline

Using Jline you can ROTATE the list using UP and DOWN arrow key.
For ex: If you want to rotate list of country names. using UP and DOWN arrow key

public String ArrowKeysForSelctingCountryName() throws IOException {  
           List<String> listCountry = new ArrayList<String>();  
           listCountry.add("India");  
           listCountry.add("Australia");  
           listCountry.add("UK");  
           int i = 0;  
           boolean isValueSelected = false;  
           String strCountryName = null;  
           ListIterator<String> itr = listCountry.listIterator();  
           int key;  
           ConsoleReader consoleReader = new ConsoleReader();  
           while ((key = consoleReader.readVirtualKey()) != 10) {  
                // to clear previous list name ,you can customise it according to you.  
                for (int clear = 0; clear < 100; clear++) {  
                     consoleReader.delete();  
                }  
                switch (key) {  
                // UP arrow key  
                case 65539:  
                     isValueSelected = true;  
                     for (; i < listCountry.size(); i++) {  
                          if (itr.hasNext()) {  
                               strCountryName = itr.next();  
                               consoleReader.putString(strCountryName);  
                               Collections.rotate(listCountry, i);  
                               consoleReader.moveCursor(-(strCountryName.length()));  
                               break;  
                          } else {  
                               while (itr.hasPrevious()) {  
                                    itr.previous();  
                               }  
                          }  
                     }  
                     if (i == listCountry.size() - 1) {  
                          while (itr.hasPrevious()) {  
                               itr.previous();  
                          }  
                     }  
                     break;  
                // Down Arrow Key  
                case 65540:  
                     isValueSelected = true;  
                     for (; i < listCountry.size(); i++) {  
                          if (itr.hasNext()) {  
                               strCountryName = itr.next();  
                               consoleReader.putString(strCountryName);  
                               Collections.rotate(listCountry, i);  
                               consoleReader.moveCursor(-(strCountryName.length()));  
                               break;  
                          } else {  
                               while (itr.hasPrevious()) {  
                                    itr.previous();  
                               }  
                          }  
                     }  
                     if (i == listCountry.size() - 1) {  
                          while (itr.hasPrevious()) {  
                               itr.previous();  
                          }  
                     }  
                     break;  
                }  
           }  
           if (key == 10 && strCountryName.length() > 0 && isValueSelected == true) {  
                return strCountryName;  
           }  
           return strCountryName;  
      }  


You can also implement AUTO-COMPLETE functionality using TAB key.
Here is the sample code : to implement auto completion of students names.

 public static void AutoCompleteSelectingStudentsName() throws IOException {  
         List<String> lStudentsName = new ArrayList<String>();  
         lStudentsName.add("Ramesh");  
         lStudentsName.add("John");  
         lStudentsName.add("Bob");  
         // convert into string array  
         String[] arrStudentsNames = lStudentsName.toArray(new String[lStudentsName.size()]);  
         ConsoleReader consoleReader= new ConsoleReader();  
         consoleReader.addCompletor(new SimpleCompletor(arrStudentsNames));  
        }  





Error :Arc dependent binaries in noarch package (CentOS 6.5)


This error mostly occurs when you try to create RPM and if binary files are present in project,then It gives this error.
This error most likely to occur in Linux distributions like CentOS,Ubuntu,OpenSuse.

In order to avoid this error,you should to add following line in .spec file which gets generated whenever you create rpm.


   %define _binaries_in_noarch_packages_terminate_build   0

To add this line in .spec file of your build,mention this declaration in pom.xml.

Sunday, 8 June 2014

Installing Apache Cordova using CLI (Command Line Interface)


Pre-requisites:
JDK 1.7.0
ADT Bundle
Apache Ant
NodeJS

Install JAVA:
http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html

Install ADT:
http://developer.android.com/sdk/index.html

Install APACHE ANT:
http://ant.apache.org/bindownload.cgi

Install NodeJS:
http://nodejs.org/

Environment Variables:

On windows, right click on My Computer -> Properties -> Advanced System Settings -> Under the Advanced Tab -> Environment Variables.

Set Environment Variables for Java, Ant,Android and NPM.

User Variables:

JAVA_HOME: C:\Program Files\Java\jdk1.7.0_60

PATH: C:\Users\HP\AppData\Roaming\npm;F:\Pushpender\adt-bundle-windows-x86_64-20140321\adt-bundle-windows-x86_64-20140321\sdk\tools;F:\Pushpender\adt-bundle-windows-x86_64-20140321\adt-bundle-windows-x86_64-20140321\sdk\platform-tools;C:\Program Files\nodejs\;

System Variables:
PATH:F:\Pushpender\apache-ant-1.9.4\bin\;

A restart might be required for the changes.

Open Command Prompt and follow the below instructions:

Cordova CLI Instruction:
npm install -g cordova
cordova create my-app
cd my-app
cordova platform add android
cordova build android

References:
http://phonegap.com/install/
http://docs.phonegap.com

Thursday, 1 August 2013

Best Practices to write good Java code


Good java code should be readable and self explanatory to others.
In order to write a good java code,following things are need to be taken care of-

1) Variable Name- should be meaningful(related to context) and should be short.
   avoid hyphen,underscore while declaring variable.
    For ex: int sum,float temperature,double averageSum.

2) If variable name is long,use camel casing. for ex:numberOfRows

3) Class Name- should be noun not verb.It should be descriptive. For ex:ReadMailbox

4) Interface Name -Define Interface names which represents adjective.For ex:Versionable

5) Method Name– Should represent Verb. For ex: getHashItem() , addProject()

6) Comments-Should be written in few words and specific to the topic.

    a)Use implementation comments inside the method  or describing variable names, method line instead of documentation/block comment.
ex-
// This code check for valid value of tax if () { }


   b)Use documentation block comment to describe method name or class.
ex-

 /** This method retrieves project list  
 **/  
 public List<String> retrieveProjectList() {  
 }  
7) The code should be properly indented and formatted so that it should be readable to programmers.