Monday, February 13, 2012

How to configure Tomcat 6.0 Virtual Directory

Suppose a virtual directory http://localhost:8080/gandhi will be created on Tomcat 6.0, the virtual directory base directory is in /home/gandhi/tomcat-home. The steps to create the virtual directory are as follows:

  1. Create a file named gandhi.xml in $CATALINA_HOME/conf/Catalina/localhost/
  2. Type the following configuration:
    gandhi" docBase="/home/gandhi/tomcat-home" debug="0" privileged="true">
Now restart the tomcat, and the virtual directory will be up and running.

-gandhi

Friday, February 3, 2012

Runtime.getRuntime().exec with Output Redirection

Runtime.getRuntime().exec is a method used in Java to execute internal command of the operating system. The use of the method is quite straightforward, for example the code to display a content of a current directory (the "ls" command) is shown as follow:

1:  import java.io.*;  
2:  public class TestExec {  
3:    public static void main(String[] args) {  
4:      try {  
5:        Process p = Runtime.getRuntime().exec("ls");  
6:        BufferedReader in = new BufferedReader(  
7:                  new InputStreamReader(p.getInputStream()));  
8:        String line = null;  
9:        while ((line = in.readLine()) != null) {  
10:          System.out.println(line);  
11:        }  
12:      } catch (IOException e) {  
13:        e.printStackTrace();  
14:      }  
15:    }  
16:  }  

However, the above code will not works to execute more "complex" command, for example when piping or output redirection is involved. For example, the command "cat * > total" will not be executed. Therefore, for the piping or output redirection command to be involved, the above code will be changed as follow:

1:  import java.io.*;  
2:  public class TestExec {  
3:    public static void main(String[] args) {  
4:      try {  
5:        String[] complexCommand = {"/bin/bash", "-c", "cat * > total"};  
6:        Process p = Runtime.getRuntime().exec(complexCommand);  
7:        BufferedReader in = new BufferedReader(  
8:                  new InputStreamReader(p.getInputStream()));  
9:        String line = null;  
10:        while ((line = in.readLine()) != null) {  
11:          System.out.println(line);  
12:        }  
13:      } catch (IOException e) {  
14:        e.printStackTrace();  
15:      }  
16:    }  
17:  }  

I hope this piece of code could be useful for you :)

-gandhi

Thursday, February 2, 2012

How to Sort HashMap by Values in Java

Recently I had a task that requires me to work with HashMap. One of the task is to sort the content of a HashMap by its values. I use a HashMap to store a user-defined Object. Therefore, I need to make the user-defined Object "sortable" by implementing Comparable interface first. In this example, the user-defined object is Summary.

This is how I sort a HashMap:


    public HashMap sortSummary(HashMap listOfSummary) {
        HashMap sortedListOfSummary = new LinkedHashMap();

        ArrayList keys = new ArrayList();
        keys.addAll(listOfSummary.values());
        Collections.sort(keys, Collections.reverseOrder());
        int size = keys.size();
        for (int i = 0; i < size; i++) {
            Summary summary = (Summary) keys.get(i);
            sortedListOfSummary.put(summary.getUrl(), summary);
        }

        return sortedListOfSummary;
    }

I've seen several codes in the Internet that use TreeMap to sort a HashMap, so this code could be another alternative in dealing with such kind of task.

PS: I don't know how to format my code in Blogger. If anyone now how to do it, please let me know.

Wednesday, January 18, 2012

How to Extract .DZ Files

DZ extension is not a very popular file extension, it's usually used to compress a dictionary file (hence the name, Dictionary Zip).

Today I got one file of this type and wondered how to extract the dictionary inside. Default file extractor on my Ubuntu machine could recognize the content of the file, but apparently it failed to extract the dictionary. After a moment of googling, I found a way on how to extract a file from a DZ file:

dictzip –decompress dictionary.dict.dz

And the dictionary inside the DZ will be extracted without leaving the original DZ file.