Wednesday, August 27, 2008

Adsense, Adsense...

More than twenty days have gone since August 1st. I am very glad that the Adsense in this blog give me more than $5. Although it is not much, it is the best result since I used Adsense. Adsense gave me dreams before, now I hope the dreams will come true.

Up to now, I have got 12 clicks from this blog in August. A strange thing is that all the clicks are from the right sidebar instead of the big rectangle below every article. I thought the big rectangle would give me more income. But it seems that I was wrong.

I know to get more income, I should to get more traffic. I think the value of a blog not only depends on its traffic but also its contents. Providing useful information is the most important thing. So I should put my heart into writing better contents. Several days ago I posted a thread on DigitalPoint that said I wanted to earn $10/month with Adsense. I believe it is not just a dream any more.

Tuesday, August 19, 2008

New Site

My new blog site: Programming Note has launched! I choose WordPress as the blog program, since WordPress is written by PHP and there are many plugins to extends its functions. Also, I have added AdSense to that site. I hope it can give me more income.

As its title. Most topics on Programming Note are about programming in the future, since I like programming and like to write something about it. With plugins, WordPress is fit for pasting code. This is much better than Blogger. However, it doesn't mean that this blog is dead. I will update this blog in the future.

Sunday, August 17, 2008

Is a Domain Name Important?

One of my friends bought some domain names. He said that owning a domain was very important. One of his domain names is hcn.name and he made a Chinese weblog on blog.hcn.name. However, I couldn't understand how important a proper domain name is and why he was so interested in it. I think domain names are just used to make up URLs. Many people don't want to try to remember your domain name. I think they like bookmarks or Google better. For me, if I want to find something, Google is enough. If I find a good website, I will add it to my bookmarks. So I seldom remember a website's domain name.

Some people advised me to buy a domain name for this blog. But I don't know what and how much a domain name can give me. I mean, owning a domain name, not web space. Traffic, pagerank, or Adsense don't seem to have to do with a domain name. Therefore, I still use this sub domain of blogspot.com.

Is it really important that owning a domain name? Or if you think it would be better to buy a domain name for this blog, what domains names are the best? Thank you for leaving your comments.

Wednesday, August 13, 2008

Gedit as an Editor for Programmers

Gedit is a lightweight and powerful text editor. It is included in Ubuntu Linux. Gedit supports plugins, so you can add some plugins to extend your editor. With plugins, Gedit is not only a simple text editor.
As a programmer's editor, it should show line numbers, enable automatic indentation, etc. Open "Edit" -> "Preferences". There are for tabs in the dialog. You'd better go to "View" tab and check "Display line numbers" and "Highlight matching bracket". Next, check "Enable automatic indentation" in the "Editor" tab.

"Plugins" tab is used for enabling and disabling Gedit's plugins. Open "Plugins" tab, then you can find the installed plugins. So please check which plugins you have installed firstly. If what you need has not been installed, please visit http://live.gnome.org/Gedit/Plugins. To install a plugin is very easy. Just extract the files to ~/.gnome2/gedit/plugins/ then restart Gedit. I think the following plugins are useful for programming:
  • Code comment: It adds "Comment code" and "Uncomment code" to the "Edit" menu. It can help you comment and uncomment code quickly.
  • Color Picker: If you enable this plugin, "Pick color..." will be added to the "Tools" menu. It opens gcolor2 to help you to choose color. If you don't have "gcolor2", you need to run "sudo apt-get install gcolor2" to install it first..
  • Embedded Terminal: It adds a terminal frame in the bottom pane. You can use "Ctrl+F9" to toggle it.
  • File Browser Pane: It adds a simple file browser into the left pane.
  • Indent Lines: After turn on this plugins, you will find "Indent" and "Unindent" options in the "Edit" menu. It can increase or decrease indentation quickly.
  • Split View 2: "Toggle Split View" will be added to the "View" menu. You can split the document frame and see more that one file at the same time.
  • Symbol Browser: It is added to left pane. When you open a source code file, it can show the classes and functions, etc. It is very useful for programming.
  • Tag List: Some common tags for HTML, XML, etc.
Gedit plugins can be written in C or Python. If you want to write your own plugins, read C plugin howto and Python plugin howto.

Sunday, August 10, 2008

Some Problems of Tomcat on Ubuntu 8.10

I found that Tomcat 5.5 package of Ubuntu Linux had some serious problems.

Normal users can't control Tomcat server. If you want to start/restart/shutdown Tomcat, you must use "sudo". For developers, restarting Tomcat is very frequent, therefore, typing "sudo" again and again becomes a dirty work.

Problems about MySQL connection. The default bootstrap of Tomcat is /etc/init.d/tomcat5.5 and it starts Tomcat server automatically. But the serious problem is: Tomcat con't connect to MySQL database! I don't know why. However, if I start Tomcat with /usr/share/tomcat5.5/bin/startup.sh, Tomcat can connect to MySQL. Why the two methods to start Tomcat are different?

Class loading problems. I wrote my first servlet that can connect MySQL with JDBC and found some strange problems. I create three files:
  • Database.java: the database class, which encapsulates some database operations;
  • DBTestServlet.java: the servlet class that calls Database class to operate database.
All files (class file, jar file, web.xml, etc) are located in proper directory. But Tomcat said it can't find the JDBC driver class (Database class has a method that calls DriverManager.getConnection() to get a database connection), although I had put mysql-connector-java-5.1.6-bin.jar in WEB-INF/lib. But if I used JDBC API in DBTestServlet class directly, no problem happened. However, the stranger is: if I started Tomcat from root account (I need to enable root accout and use "su" to switch to root, see Enable and Disable "root" on Ubuntu Linux) instead of using "sudo", no problem happened, even if I used Database class. That's very strange.

I don't know why Ubuntu's Tomcat package has so many problems. Perhaps the best solution is to download Tomcat from http://tomcat.apache.org/ and extract it to a directory instead of using "sudo apt-get install tomcat5.5".

Tuesday, August 5, 2008

Unix Searching Tool -- find

The command "find" exists on almost all Unix. It is used for searching files. "find" can help you find files with specified attributes. In many conditions, "find" can work with other programmes well. Here I will take some examples to show the power of "find".

Suppose you have a directory named "shop" which contains many PHP files, but it contains some sub-directories, so you cannot use "ls *.php" to show all the PHP files. "find" can solve the problem:
find shop -name '*.php'
"find" searches all the sub-directores recursively and find all files whose names match the pattern "*.php" and print their paths. The argument "-name" means matching the filename.

If you need to calculate how many PHP files there are in the "shop" directory, just need use "wc" command to count the number of lines that "find" produced.
find shop -name '*.php' | wc -l
It proves that Unix "pipe" is very powerful and useful.

Beside matches file name, find can accept other conditions, for example, last modified time. In my computer, Tomcat (a JSP/Servlet container with a web server) generates log files on /var/log/tomcat5.5/. If I want to show the log files which are modified more than 2 days ago, I can use "-mtime" argument:
find /var/log/tomcat5.5/ -mtime +2
"-mtime" means "last modified time" and "+2" means "more than 2 days". Furthermore, I want to delete all the Tomcat log files which is generated more than 2 days ago. I can use "xargs" to combine "find" with "rm":
find /var/log/tomcat5.5/ -mtime +2 -print | xargs rm -f
"find" also supports logical operators. For the example above, if I need find the log files whose last modified time is more than 5 days or less than 3 days, I can use "find" as below:
find /var/log/tomcat5.5/ -mtime +5 -or -mtime -3.
"find" supports three logical operators: -not, -and, -or.

Certainly, the functions of "find" are too many to talk about. All the information about "find" can be finded by "man find", although it is not easy to read.

Sunday, August 3, 2008

Making Executable JAR

As a Java programmer, you probably need to make JAR file, a compressed file which contains all necessary .class files, like tar (a widely used file format in Unix). For some reasons, you may want to make executable JAR file, which can run by "java -jar filename.jar" or just double click it on Windows. Here are the steps about how to make an executable JAR.

Suppose these are the source code:
src/com/wjb/goose.java
src/com/wjb/dog.java
src/com/wjb/zoo.java
Each source file contains only one class (except inner class) and belongs to the package "com.wjb".

For convenience, you'd better put the .class file into another directory, for example, classes. Here, directory classes and src are in the save level. The best way to do it is using "-d" argument. Open the terminal (or "command line prompt" on Windows), typing these commands:
cd src # Enter source code directory
javac -d ../classes com/wjb/goose.java
javac -d ../classes com/wjb/dog.java
javac -d ../classes com/wjb/zoo.java
-d ../classes means put the .class file into ../classes directory and build proper sub-directories according to the package name. For this example, it will make classes/com/wjb and all the class file will be put there.

Now suppose the Zoo class has the main method, i.e. it is the starting point of the whole programs. If you want to make executable JAR, you must specify the class that has the main method. So you need a special file: manifest.txt.

Create a text file named manifest.txt and save it in "classes" directory. This file has only one line:
Main-Class: com.wjb.Zoo
Note that there is a blank character after the colon ":" and you must use its full class name (containing package name).

Next, go to "classes" directory and execute this command:
jar -cmvf manifest.txt zoo.jar com
If everything is OK, the zoo.jar will be created. It is executable. You can use "java -jar zoo.jar" to test it or just double click its icon if you are using Windows.

In addition, the simple usage of "jar" to create a JAR file is:
jar -cmvf <manifest.txt> <filename.jar> <input files>

Here, "<input files>" are files or directories. You can specify more than one file or directory if you want to add many files into JAR. If you need the complete reference, please read the jar manual.