Saturday, January 7, 2012

Integrating Eclipse/ IBM RAD and Java Decompiler(JAD)

  1. Download JAD from http://www.varaneckas.com/jad and unzip the file to C:\jad folder.
  2. Download jadclipse jar file from http://sourceforge.net/projects/jadclipse/
  3. Copy the  jadclipse jar file to the eclipse plugins folder
    Eg : If eclipse home dir is C:\eclipse then copy jar file to C:\eclipse\plugins.
    For RAD, eclipse home dir will be something like C:\Program Files(x86)\IBM\SDP and copy th jar file to C:\Program Files(x86)\IBM\SDP\plugins
  4. Restart Eclipse/RAD.
  5. Open Eclipse/RAD. Navigate to Window > Preferences > General > Editors > File Associations -> Associate *.class file type to JadClipse Class File Viewer and make JadClipse Class File Viewer as default.(If you are not able to see the JadClipse Class File Viewer under Associated Editors, then restart eclipse with clean option to pickup the plugin)
  6.  Window > Preferences > Java > JadClipse - > Set Path to Decompiler to C:\jad\jad.exe.
  7. Window > Preferences > Java > JadClipse > Debug - > Check Output original line numbers as comments.
  8. Restart Eclipse/RAD.

Saturday, December 3, 2011

Fibonacci

package au.com.d2klry;

public class Fibonacci {

    public static void main(String[] args) {
        long prevValue = 1;
        long prevPrevValue = 0;
        long currentValue = 0;
        for (int i = 0; i < 20; i++) {

            prevPrevValue = prevValue;
            prevValue = currentValue;

            System.out.format("%d\t", currentValue);
            currentValue = prevValue + prevPrevValue;
        }
    }
}


Monday, November 7, 2011

Iterate over HashMap

package au.com.d2klry.map;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

public class HashMapUtil {

    public static void main( String[] args ) {
        HashMapUtil hashMapUtilObj = new HashMapUtil();
        Map<string, string=""> map = hashMapUtilObj.getHashMap();
        Set<string> keySet = map.keySet();
            for(String key : keySet){
                System.out.println("(Key,Value) -> ("+key+","+map.get(key)+")");
            }
  
    }
 
    public Map<string, string=""> getHashMap(){
        Map<string, string=""> map = new HashMap<string, string="">();
        int maxSize = 10;
  
        for (int i = 0; i < maxSize; i++) {
            map.put( "KEY"+(i+1), UUID.randomUUID().toString() );
        }
        return map;
    }
}


Saturday, November 5, 2011

Split a string on commas and whitespaces

package au.com.d2klry.string;

public class Tokenizer {

    public static void main(String args[]){
        String fullNameWithCommaNSpace = "Lastname,  FirstName";
        String fullNameWithComma = "Lastname,FirstName";
        String fullNameWithSpace = "Lastname FirstName";

        String regExp = "[,\\s]+";
    
        for (String token : fullNameWithCommaNSpace.split(regExp))
            System.out.println("FullNameWithCommaNSpace Token : " + token);

        for (String token : fullNameWithComma.split(regExp))
            System.out.println("FullNameWithComma Token : " + token);

        for (String token : fullNameWithSpace.split(regExp))
            System.out.println("FullNameWithSpace Token : " + token);
    }

}

Monday, October 31, 2011

Read and Write the properties file.

package au.com.d2klry.properties;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;

public class ReadPropertiesFile {

    public static void main(String[] args) {
        //First section is creating the properties file
        String fileName = "C:\\temp\\java\\properties.txt";
        String newFileName = "C:\\temp\\java\\newPropertiesFile.txt";
        try {
            File propertiesFile = new File(fileName);
            if(!propertiesFile.exists()){
                propertiesFile.createNewFile();
            }
            BufferedWriter writer = new BufferedWriter(new FileWriter(propertiesFile));
            writer.write("database_name=DB_TEST");
            writer.newLine();
            writer.write("database_host=localhost");
            writer.newLine();
            writer.write("database_port=50000");
            writer.newLine();
            writer.write("#database_username=scott");//Add a comment to properties file
            writer.newLine();
            writer.write("#database_password=tiger");
            writer.flush();
            writer.close();
            //End of creating properties file
       
            //Second part is reading the properties from the file, printing the properties to the console
            //and writing properties to the new file
            Properties properties = new Properties();
            properties.load(new FileInputStream(fileName));
            System.out.println("database_name -- > " + properties.getProperty("database_name"));
            System.out.println("database_host -- > " + properties.getProperty("database_host"));
            System.out.println("database_port -- > " + properties.getProperty("database_port"));
            //Property value will be null as it is not defined in the original properties file
            System.out.println("database_username -- > " + properties.getProperty("database_username"));
           
            properties.store(new FileOutputStream(newFileName), "File created by d2klry");
           
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Writing data to a new file

package au.com.d2klry.file;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class WriteData {
    public static void main(String args[]){
        try {
            File file = new File("C:\\temp\\java\\WriteData.txt");
            if(!file.exists())
                file.createNewFile();
           
            BufferedWriter writer = new BufferedWriter(new FileWriter(file));
            writer.write("First Line");
            writer.newLine();//Add a new line
            writer.write("Second Line");
            writer.newLine(); //Add a new line
            writer.write("\tThird Line");//Add a tab before the third line content
            writer.flush();
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Creating a new file using java

package au.com.d2klry.file;

import java.io.File;
import java.io.IOException;

public class CreateFile {
    public static void main(String args[]){
        try {
            File file = new File("C:\\temp\\java\\CreateFile.java");
            if(!file.exists()){
                file.createNewFile();
                System.out.println("Successfully created a new file");
            }
            else{
                System.out.println("File already exists!!!");
            }
           
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}