Monday, April 30, 2012

How to run sonar for Maven based java project


To run sonar, either we can directly run using maven script or we can run sonar from eclipse plugins.
To run sonar report, first we have to install maven & sonar in our local machine.


  1. Download and extract Maven in your C drive from http://www.apache.org/dyn/closer.cgi/maven/binaries/apache-maven-3.0.4-bin.zip
  2. Install Jdk 
  3. Download and extract Sonar in your D drive from http://dist.sonar.codehaus.org/sonar-3.0.zip
  4. Now go to D:\sonar-3.0\bin\windows-x86-32 and double click on StartSonar.batNow sonar server will start locally on http://localhost:9000
  5.  Go to parent folder of your project, and run following command
    mvn clean compile install sonar:sonar as below
  6. After its completion, browse http://localhost:9000/ where you will see the project list with code quality report as below

How to Install Sonar in Eclipse plugins?

Installing Sonar Eclipse plugins for java


Name Sonar Eclipse
License LGPL v3
Authors Evgeny Mandrikov (SonarSource), Jérémie Lagarde
Latest version 2.3.0
Update site http://dist.sonar-ide.codehaus.org/eclipse/
Eclipse Marketplace http://marketplace.eclipse.org/content/sonar
JIRA Issue Tracker http://jira.codehaus.org/browse/SONARIDE/component/14315
Sources https://github.com/SonarSource/sonar-eclipse

Compatibility Matrix

Plugin 1.0.0 2.0.0 2.1.0 2.2.0+
Sonar 2.0.1+ 2.4+ 2.4+ 2.4+
Eclipse 3.5.x, 3.6.x 3.5.x, 3.6.x 3.5.x, 3.6.x 3.5.x, 3.6.x, 3.7.x
Mylyn (optional) 3.2.0+ and requires Sonar 2.8+ 3.2.0+ and requires Sonar 2.9+

Installation

To install this plugin in the Eclipse IDE:
  1. Select Help -> Install New Software. This should display the "Install" dialog.
  2. Paste the Update Site URL into the field named "Work with:" and press Enter. Pressing Enter should cause Eclipse to update list of available plugins and components.
  3. Choose the component listed under Sonar: "Sonar Integration for Eclipse (Required)".
  4. Click Next. Eclipse will then check to see if there are any issues which would prevent a successful installation.
  5. Click Finish to begin the installation process. Eclipse will then download and install the necessary components.
  6. Once the installation process is finished, Eclipse will ask you if you want to restart the IDE. It's strongly recommended that you restart IDE.

Empty GroupId error when trying to Associate with Sonar

How/Where to add GroupId and artifactId when project associating with sonar in eclipse.
First time when I started using Sonar (project quality tools) for my Java based web project, i google a lot & read articles about it. I started installing sonar in eclipse ide.
But unfortunately I got following error 'Empty GroupId error when trying to Associate with Sonar'.
Finally its solved by giving groupId and aftifactId. Actually because of lack of interactive GUI in eclipse most of the time people get confused.
As in below image, just double click on respective column and give groupID and artifactID same in pom.xml

how to solve Empty GroupId error when trying to associate with Sonar

Thursday, March 1, 2012

Pass by Value or reference in Java

 Pass by Value or reference in Java?    
 Answer :  Java Passes References By Value

How does Java pass arguments into a method?  The short answer is “by value,” but the details are subtle.  Recall that in C, C++, and other languages you can choose to pass either a “value” or to “pass a pointer to a value”.  The first option is called “pass-by-value.”  The second option is called “pass-by-reference.”  In Java, primitive variables are always passed by value.  In other words, a copy of the variable is passed into the method.  In other words, if you make a change to the variable within the method, then that change is not visible anywhere else.  For example, suppose we have

double d = 2.0;
changeMe(d);
System.out.println(d);       //prints 2.0

The printed value will be 2.0 no matter what happens inside the method changeMe.

public void changeMe(double d)
{
     //this has no effect on d outside of this method!
     d = 345.0;
}
On the other hand, Java objects appear to be passed by reference, but it’s a ruse.  All Java objects are pointers (we just don’t have to use the annoying pointer symbol * when we create them), so the pointer gets passed by value (oooh, subtle).  For example, suppose I create an object called Car that has methods called setSpeed() and getSpeed().

Car ferrari = new Car();
ferrari.setSpeed(65.0);
changeParameters(ferrari);
System.out.println(“The speed = “+ferrari.getSpeed());
So what gets printed out?  That depends on what happens inside the changeParameters method.  Suppose
public void changeParameters(Car c)
{
     //changes the car’s speed outside this method!
     c.setSpeed(200.0);
}
Then the code prints out 200.0.  Why?  Because the ferrari was a pointer to the object Car.  The ferrari got passed by value into the method.  So a copy of the pointer got passed into the method.  But a copy of a pointer still points to the same object, the ferrari!  So changes to the ferrari in the method are felt outside the method as well.

Here is one more subtlety.  Suppose we have
public void changeCar(Car c)
{
     Car chevy = new Car();
 
     //does not affect c outside this method
     c = chevy;
}
And now we call
Car ferrari = new Car();
changeCar(ferrari);
Is the original ferrari object now a chevy (outside of the method)?  No!  Inside of changeCar we changed the original pointer to be a new pointer.  But the original pointer was passed by value, so changing the pointer within the method has no impact on the original pointer outside of the method.

Phew!

For those who doubt J, try the following code.  Recall that arrays are pointers in Java.  First let’s show that either a pointer or a copy of the pointer must be passed into a method.  We’ll resolve which one in a minute.  (The next example shows that it is actually a copy.)

public class Example
{
public static void main(String[] args)
     {
          //”test” is a pointer.
int[] test = {1, 2, 3, 4};

//The original hex value of the pointer
System.out.println(test);

//Let’s pass test’s pointer into a method.
//The method changes some array values.

          Example e = new Example();
          e.modifyArray(test);
          
//This prints out “9 2 3 4”, not “1 2 3 4”

          for(int i=0; i<test.length; i++)
          {
               System.out.print(test[i]+”  “);
          }
     }
     public void modifyArray(int[] a)
     {
          a[0] = 9;

//Because “a” is a pointer to the array 
//called “test”, this changes the first 
//element of “test”.
}
} 


See?  We passed in a pointer to the array, and then used that pointer to modify an element in the array. (But it was really a copy of the pointer!  See below.)

The following code shows that a copy of the array’s pointer is passed into a method.

public static void main(String[] args)
     {
          //”test” is a pointer.
int[] test = {1, 2, 3, 4};
 
//The original hex value of the pointer
System.out.println(test);
 
//Let’s pass test’s pointer into a method.
//The method changes some array values.

          Example e = new Example();
          e.modifyArray(test);
          
//This prints out '9 2 3 4', not '1 2 3 4'

          for(int i=0; i<test.length; i++)
          {
               System.out.print(test[i]+”  “);
          }
     }
 

     public void modifyArray(int[] a)
     {
          a[0] = 9;
 
//Because “a” is a pointer to the array 
//called “test”, this changes the first 
//element of “test”.
}
}


If the actual pointer had been passed into the method, then “test” should have been changed to point to “b”.   That didn’t happen!

Tuesday, February 21, 2012

How to read input from user in java program

Here is the sample demo to get inputs from user in java program while running in command prompt.

I am providing you a java application that will communicate with the user at the command line and returns the user input.

I have prompt the user to enter the name by using System.out.print() method to keep the cursor on the same line. Then I have used the System.in object, along with the InputstreamReader and BufferedReader class in order to read the user input. The br.readline() method reads the name from command line.
After pressing the enter key, you will get the user input.

Here is the sample example java code:

import java.io.*;

public class ReadInputFromUser
{
    public static void main (String[] args) 
    {
       System.out.print("Enter your name and press Enter: ");
       BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
       String name = null;
       try
       {
         name = br.readLine();
       }
       catch (IOException e) 
       {
         System.out.println("Error!");
         System.exit(1);
       }
       System.out.println("Your name is " + name);
    }
}