Thursday, November 21, 2013

Setting up your Linux Environment (Ubuntu) for JAVA Development


This tutorial is for starters. although this may also help professionals. I am using Ubuntu 12.04 LTS and I thought of creating a tutorial on how to setup your Linux environment for java development.

Installing JDK 6


  • download jdk from here
  • create folder jvm in /usr/local
  • move the downloaded jdk to /usr/local/jvm
  • change the permission of jdk -> sudo chmod 700 jdk-6u38-linux-i586-rpm.bin
  • extract jdk using command -> ./jdk-6u38-linux-i586-rpm.bin
  • create a link for the jdk -> ln -s jdk-6u38 java
  • add the jdk in your enviromnent PATH. I usually add it in .bashrc
          you can add the following in your .bashrc:

          export JAVA_HOME=/usr/local/jvm/java
          export JDK_HOME=$JAVA_HOME
          export PATH=$PATH:$JAVA_HOME/bin

Installing JDK 7

Just execute the following commands to install jdk7
sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update
sudo apt-get install oracle-java7-installer

Installing Subversion

sudo apt-get install subversion

Installing Git

sudo apt-get install git-core

Installing Maven

  • Go to Apache Maven website and follow the instructions
  • place maven in /usr/local/apache-maven
  • if you have your own settings.xml, create .m2 folder in your home directory and place your settings.xml
  • add maven in your environment path
    

Tuesday, October 2, 2012

Programmer Tip: #1 Constants First

Based on my experience, programmers always commit this mistake in using constants in the code. I always see this problem even in large projects made by experienced programmers. So I decided to choose this topic for my first blog. Check this code.

public static final String CONSTANT_VALUE = "Value";
    
public static boolean functn(String compare) {
    if(compare.equals(CONSTANT_VALUE)) {
        return true;
    }
    return false;
}

Did you notice something wrong with this code? Well, Yes you're right. What if a null value is passed to the functn method? This code will produce a NullPointerException. So to avoid getting a NullPointerException some programmers will check if the passed value is null or add some try-catch to handle the exception like this

public static final String CONSTANT_VALUE = "Value";
    
public static boolean functn(String compare) {
    if(compare != null 
       && compare.equals(CONSTANT_VALUE)) {
        return true;
    }
    return false;
}

public static boolean functnWithTryCatch(String compare) {
    try {
        if(compare.equals(CONSTANT_VALUE)) {
            return true;
        }
        return false;
    } catch (NullPointerException e) {
        return false;
    }
}

Note: declare your constant variables as "static"

The right way of doing it is just to put the constant variable first and make the "compare" the parameter in your operation. This is because the constant variable always has a value and will never turn to null. Even if the passed parameter is null, this will not produce any NullPointerException and behave properly. See example below

public static final String CONSTANT_VALUE = "Value";
    
public static boolean functn(String compare) {
    if(CONSTANT_VALUE.equals(compare)) {
        return true;
    }
    return false;
}

Notice that the code is now: if(CONSTANT_VALUE.equals(compare))

Hope you learned something. Sorry if the design and the words used is not that good. Busy eh :p I'll improve this page pag di na ako busy. Thanks ;)