Five Reasons to Adopt Linux, Today!

The popular open source news site LXer recently published a detailed article on the advantages of Linux, with the aim of dispelling unfair cynicism.

Whether you are a home user or a business owner, Linux is a great choice for an operating system. I entered the Linux world pretty recently, after spending a long time with various Microsoft Windows versions. Now, there’s no looking back!

I’ve identified the top 5 reasons to consider turning to Linux:

1) Cost of operating your computer.

Microsoft Windows offers various paid license plans to run your computer, or install copies on several machines in a network. When you turn to Linux, software that runs your computers is completely free!

This is a great advantage for both home users and businesses who are seeking to cut costs.

2) The spirit of sharing.

Linux is based on sharing and caring. Linux is often the force behind social initiatives that bring computing power to less privileged children and people in war ravaged areas.

When you adopt Linux, you are automatically enrolled into community service – pure material aims take a backseat.

3) Vibrant support communities.

There are extremely active support communities that help users of various distros troubleshoot their problems. The AskUbuntu forums which I’ve used for my Ubuntu system is just one of countless examples. Experienced users will patiently help you run your computer flawlessly, without asking even a cent for it.

If you’re new to Linux, you surely won’t feel lost.

Paid support is also available from companies specializing in it, especially at the enterprise level.

4) Security.

Linux provides the harshest environment for viruses to live. Open source software is collaboratively scrutinized by developers from around the world, which means that viruses will be busted well before they make it to your computers.

You do need anti-viruses on a Linux box, at times. These are chiefly meant for scanning Windows drives, or viruses that execute themselves with Wine!

5) Linux encourages you to be tech savvy.

What is more gratifying than knowing the what, where, when, how and why of the software that powers your computer?

If you’re an Apple user, you’ll typically head straight to the nearest Mac genius for help in times of distress. As a Windows user, you’ll probably hesitate to venture beyond the basic troubleshooting methods.

Linux users set a very different example. They try to get to the bottom of every problem themselves (if they ever come), get help from worldwide networks of users if they’re stuck, and ensure that they’ve straightened out issues with their operating systems.

Linux has retained the way the classical Unix operating system works. Most servers today run Unix-like operating systems – if you’re technically inclined and get accustomed to using Linux, you’re getting to know how operating systems worked in their original design.

Sounds cool?

Choose from an array of distros, and get ready to go the Linux way!

What has been the most compelling reason for you to adopt Linux and stay with it? Are you skeptical about using Linux? Use the comment form below and speak your mind!

Remotely Working Together on a Terminal Session in Linux

I often get a chance to work from home, and this has given me opportunities to look for ways to share sessions with my colleagues. Windows XP used to have the excellent NetMeeting tool, but I recently switched to Ubuntu and haven’t yet found an equivalent.

Yes, I’ve heard of VNC and used it a lot too, but I wasn’t happy with its remote desktop sharing performance. Ekiga is interoperable with NetMeeting calls, but doesn’t support screen sharing. Even Skype’s screen sharing does not seem to support remote screen controlling.

However, Linux’s power is at the command line, and this is true even in the case of remote sharing. So here comes screen to the rescue – this magical command allows you to flawlessly resume lost sessions and share them with multiple users at the same time.

Starting screen

Start screen using

screen -d -R session_tomcat

Replace session_tomcat with any other helpful name for the session you’re about to create.

Sharing your Session

Ask your friend to connect using (assuming they are logged in using the same user account):

screen -x session_tomcat

Now it’s simply magical. Multiple persons can type and work on the same terminal – it works best when you’re coordinating over the phone. Note that the dimensions of your terminal output will be the same for every user – to change it, press CtrlA and then capital F. This will make the screen output fit your current terminal size, and change it to that size for every connected user.

Detaching from a Screen Session

Important: To detach from the screen session so that you can resume later, simply close the window. If you type exit, you’ll end up terminating the screen session and the processes running within.

Why screen is better than nohup and tail

Here’s a comparison. Consider a hypothetical example in which you need to find a file that contains the word “cat” in its name. You know it will take long, so you run the following combination of commands:


$ nohup find / -name *cat* &
$ tail -f nohup.out

The nohup command is a request to not kill (nohup stands for “no hangup”) the process that you are just about to run in case you lose connection. The & at the end sends the process to the background, so that the prompt returns immediately. The tail -f follows the output of the command executed through nohup. If you do lose connection, simply run tail -f nohup.out again to see the running output.

To do the equivalent in a screen session, you’ll only need run the find command in an open screen session. If you need to reconnect after a lost connection, run screen -d -R <session_name> again, and you’ll be back in your session as though you never left it.

Far more neat, isn’t it? Of course, screen has much more to offer than being a simple replacement for nohup and tail -f. For example, it’s very easy to resume a CLI session running remotely – you’d simply reconnect without having to re-initialize.

Other Advanced Usages

CtrlA has a special meaning when running screen – it allows you to execute special screen commands.

To see the list of available commands, type CtrlA and then ?.

For example, one interesting feature of screen is its support for multiple windows. To create a window, type CtrlA and then c.

To switch to a window, use CtrlA and then the window number (from 0 to 9).

To see the list of available windows type CtrlA and then ".

The man page of screen reveals a great deal more to explore!

Which features of the screen command in Linux do you find the most useful?

Setting up the DB2 database on Ubuntu and testing JDBC connectivity

I got my DB2 database installation running on a Ubuntu 11.10 machine recently, and have documented the steps I carried out below.

Downloading DB2

The express edition is free, and can be downloaded from Download DB2 Express-C. You’ll need to register for an IBM id if you don’t already have one.

Download DB2 Express C Using HTTP

Extracting and installing

Use the command tar -zvxf db2exc_975_LNX_x86.tar.gz to extract the gzipped file. Run the installer using sudo ./db2setup, and choose “Install a Product” on the left menu. The installation process is quite straightforward after this point.

DB2 Setup Launchpad - Install a Product as root

Troubleshooting libaio.so.1 ‘not found’ when running db2setup

I faced the following problem when I ran the setup script:


$ sudo ./db2setup
ERROR: 
   The required library file libaio.so.1 is not found on the system. 
   Check the following web site for the up-to-date system requirements
   of IBM DB2 9.7
   http://www.ibm.com/software/data/db2/udb/sysreqs.html
   http://www.software.ibm.com/data/db2/linux/validate  
  Aborting the current installation ...
  Run installation with the option "-f sysreq" parameter to force the installation.

To fix it, I had to run sudo apt-get install libaio-dev to install the missing package.

Verifying the Installation

The following command verifies the db installation and configured instances:

sudo /opt/ibm/db2/V9.7/bin/db2val -a

Creating a database

Switch to the user account that is the owner of the instance (db2inst1 is the default).

$ su db2inst1

Switch to the bash shell if necessary:

$ bash

Under this user account, you can run DB2 commands as illustrated below. Note that the create database command takes time – it took several minutes on my machine.


$ db2 create database test
DB20000I  The CREATE DATABASE command completed successfully.
$ db2 connect to test

   Database Connection Information

 Database server        = DB2/LINUX 9.7.5
 SQL authorization ID   = DB2INST1
 Local database alias   = TEST

$ db2 "create table test.technonstop(id int, username varchar(200))"
DB20000I  The SQL command completed successfully.
$ db2 "INSERT INTO test.technonstop VALUES(1, 'abdullah')"
DB20000I  The SQL command completed successfully.

Troubleshooting

If you’re unable to run any db2 command, the db2 environment variables may not have been sourced. To do it, run the following command at the terminal, replacing db2inst1 with the instance owner.

. /home/db2inst1/sqllib/db2profile

Java Program to Test JDBC Connectivity

I use the following Java program to test to see if JDBC connectivity works from a Java program, after assigning appropriate values to the constants at the beginning:


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;


public class PrintSQLTable {

	final static String dbdriver = "com.ibm.db2.jcc.DB2Driver";
	final static String dburl = "jdbc:db2://localhost:50000/test";
	final static String dbuser = "db2inst1";
	final static String dbpassword = "myPassword";
	final static String dbtable = "test.technonstop";
	
	public static void main(String[] args) throws ClassNotFoundException,
			SQLException {
		Class.forName(dbdriver);
		Connection connection = DriverManager.getConnection(dburl, dbuser,
				dbpassword);
		Statement statement = connection.createStatement();
		ResultSet resultSet = statement
				.executeQuery("SELECT * FROM " + dbtable);

		StringBuilder tableContents = new StringBuilder("");
		ResultSetMetaData metaData = resultSet.getMetaData();
		int noOfColumns = metaData.getColumnCount();

		for (int i = 1; i <= noOfColumns; i++) {
			tableContents.append(metaData.getColumnName(i) + " \t ");
		}

		tableContents.append("\n");
		int sbLength = tableContents.length();
		for (int i = 0; i < sbLength; i++)
			tableContents.append("-");
		tableContents.append("\n");

		while (resultSet.next()) {
			for (int i = 1; i <= noOfColumns; i++) {
				tableContents.append(resultSet.getString(i) + " \t ");
			}
			tableContents.append("\n");
		}

		if (tableContents.length() == 0)
			tableContents.append("No data found");

		System.out.println(tableContents);
	}


}

Run the program with the db2jcc.jar file in the classpath, as shown below:


$ java -cp /opt/ibm/db2/V9.7/java/db2jcc.jar:. PrintSQLTable
ID 	 USERNAME 	 
-----------------
1 	 abdullah 	

List of Shortcuts for the Eclipse Java IDE

Here’s a cheat sheet of Eclipse shortcuts I’ve put together to help me work really fast with the IDE. The shortcuts have been categorized into two sections – one that most people know about, and another section that contains the lesser known ones. You should be able quickly scan through the first section, and learn any of the ones that you don’t currently know.

The second section might take longer, and you may want to bookmark this page for future reference.

Easy Shortcuts

Search in files

Use CtrlH to search in all files across the workspace or project.

Search Dialog in Eclipse Juno

Open Resource

CtrlShiftR opens a resource quickly – without the time it spends for indexing. This makes it especially fast when you’ve just opened a workspace, in comparison to the Open Type shortcut below.

Open Resource Dialog in Eclipse Juno

Open Type

Use CtrlShiftT to open a Java type.

Open Type Dialog in Eclipse Juno

While I prefer CtrlShiftR to search for files, CtrlShiftT works better if you want to search for Java classes or interfaces.

Open the Type Hierarchy

Use CtrlT to get a popup window with the type hierarchy for the type under your cursor.

Type hierarchy popup - Eclipse Juno

This is tremendously useful to see inherited types, sub-types, and implemented interfaces.

Press CtrlT again to toggle between super-types and sub-types.

Display members and inherited members

Pressing CtrlO displays the class members, pressing it again displays the inherited members too!

Eclipse class members popup

Focusing on the tooltip

Press F2 to get the tooltip for the item currently under the cursor.

F2 Tooltip Eclipse Juno

Correcting Indentation of Selected Text

Use CtrlI to correct indentation.

Debuggging

Use F5 to Step into, F6 to Step over, F7 to step out, and F8 to resume. See below for more shortcuts on debugging.

New File Wizard

Use CtrlN to start the new File Wizard.

New File Wizard - Eclipse Juno

Lesser Known Shortcuts

CtrlTab to Switch Between Windows

Go to General->Keys to see all shortcuts.

Setting Eclipse Juno shortcut for switching between editor windows

Change the binding of Next Editor and Previous Editor to use Tab instead of F6 – this makes it incredibly easy to switch between open editors. I often dislike changing keyboard shortcuts so that it’s easy to work on someone else’s machine too, but I couldn’t resist this one.

Cycle through tabs in the current view

Similar to the above, but still slightly different: Use CtrlPgUp and CtrlPgDn to switch between tabs in your current viewwithout showing you the complete list of available tabs.

Scroll without using the mouse

Use CtrlUp and CtrlDown to scroll, while keeping your cursor in the same position!

Move a line or lines

Use AltUp and AltDown to move selected lines, or the current line.

Jump to matching bracket

CtrlShiftP on a bracket, brace or parenthesis moves the cursor to its matching bracket, brace or parenthesis.

Progressively Select Blocks

Use ShiftAltUp / ShiftAltDown to progressive select blocks.

Find references

CtrlShiftG will find references to the item under the cursor.

Find declarations (Only C++/Javascript)

CtrlG finds declarations of the item under the cursor.

Open Declaration

F3 opens the declaration for the item under the cursor.

Find previous and next, based on selection

Use ShiftCtrlK and CtrlK to find the previous and next occurrences of the item under the cursor.

I had to set CtrlK for “Find next” in Preferences->General->Keys, but the shortcut for “Find previous” was there by default.

CamelCase Code Completion

If you have a class that has a VeryLongName, simply type VLN and press CtrlSpace to see VeryLongName as one of the suggestions.

Automatically Insert Braces and Semicolons at the correct position

This isn’t a shortcut – but a useful productivity boost. Setting semicolons and braces to be automatically inserted in the correct position allows you to type them in from anywhere but they actually appear at the end of the line. Escaping text in string literals is another useful option.

Eclipse Juno preferences - automatic insertion of braces and semicolons at correct position and escaping pasted strings

Quick Access Menu

Quickly access any menu, command, editor, etc by typing in Ctrl3 and then the initial letters of the item you want.

Eclipse Juno quick access menu

History

Use AltLeft and AltRight to navigate between your editor history items. This is equivalent to using the history icons on the toolbar.

Eclipse Juno history toolbar buttons

Quick Fix

Use Ctrl1 to get to the quick fix menu and quickly fix compile-time issues.

Eclipse Ctrl-1 quick fix menu

Debugging

Use CtrlR to run to the current line. Use CtrlShiftB to toggle a breakpoint. F11 debugs the last run program, while CtrlF11 runs it.

Refactoring

ShiftAltR renames an item and updates all references. Use ShiftAltL to extract code to a local variable, and ShiftAltM to move code to a method.

The Best of them all!

And finally, which one’s the greatest shortcut of all? Use CtrlShiftL to get a list of all available shortcuts!!

Eclipse Juno list of shortcuts

\