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.

Sunday, April 25, 2010

Taiwan Top Universities 2009

There’re are lots of ranking methodology and results on the Internet. But the list of Taiwan Top Universities I wrote here will be based on QS Top Universities. You can read their university ranking on their website. I’ll list 3-6 top Taiwan Universities grouped by various categories. The Taiwan universities will be ordered based on their ranking on each categories. This post is written by Gandhi Manalu [http://gandhim.wordpress.com].

  1. World University Rankings:

    1. National Taiwan University – NTU (95)

    2. National TSING HUA University (223)

    3. National CHENG KUNG University (281)

    4. National YANG MING University (306)

    5. National Taiwan University of Science And Technology – NTUST (351)

  2. World University Rankings – Natural Sciences:

    1. National Taiwan University – NTU (51)

    2. National TSING HUA University (150)

    3. National Taiwan University of Science And Technology – NTUST (254)

  3. World University Rankings – Engineering/Technology:

    1. National Taiwan University – NTU (47)

    2. National TSING HUA University (84)

    3. National Taiwan University of Science And Technology – NTUST (143)

    4. National CHENG KUNG University (160)

    5. National CHIAO TUNG University (166)

  4. Asian University Rankings:

    1. National Taiwan University – NTU (22)

    2. National TSING HUA University (40)

    3. National CHENG KUNG University (43)

    4. National YANG MING University (47)

    5. National SUN YAT-SEN University (71)

    6. National Taiwan University of Science And Technology – NTUST (72)

  5. Asian University Rankings – Life Sciences & Biomedicine Subject:

    1. National Taiwan University – NTU (10)

    2. National Taiwan University of Science And Technology – NTUST (42)

    3. National SUN YAT-SEN University (66)

    4. National TSING HUA University (67)

    5. National Central University (69)

  6. Asian University Rankings – Natural Sciences:

    1. National Taiwan University – NTU (10)

    2. National TSING HUA University (33)

    3. National Taiwan University of Science And Technology – NTUST (57)

    4. National Central University (60)

    5. National CHENG KUNG University (63)

  7. Asian University Rankings – IT and Engineering:

    1. National Taiwan University – NTU (16)

    2. National TSING HUA University (32)

    3. National Taiwan University of Science And Technology – NTUST (41)

    4. National CHENG KUNG University (49)

    5. National CHIAO TUNG University (52)

The number in the left is the university ranking in Taiwan on particular category whereas the number in brackets denotes the university ranking in each category.This post is written by Gandhi Manalu [http://gandhim.wordpress.com].

Wednesday, April 14, 2010

Yahoo Messenger 10 Profile Directory in Vista

My Yahoo Messenger 10 suddenly crashed, I didn’t know the cause, but it might be the side effect of uninstalling several programs from my computer.

I wanted to reinstall Yahoo Messenger 10, but first I need to backup my profile that includes my chat history, so that all my recorded chats could be restored later. Usually, I could find my Yahoo profile directory at Program Files installation directory, but I couldn’t find it.

After searching through directories in my computer, I found out that Yahoo Messenger 10 stores its user profile in:

X:\Users\<windows_username>\AppData\Local\VirtualStore\Program Files\Yahoo!\Messenger\Profiles\<yahoo_id>
(where windows_username is user’s windows id and yahoo_id is user’s yahoo id).

I hope this post will be helpful for those who have the same problems as me.