The Coded One

programming, algorithms, discrete math, open-source

Archive for the ‘Java’ Category

Setting Up Tomcat on Ubuntu

with one comment

Prerequisites

Tomcat 6.0 requires JRE 5.0. Read the RELEASE-NOTES and the RUNNING.txt file in the distribution for more details.

Installation

1. Download binary core from apache site.

wget http://apache.tradebit.com/pub/tomcat/tomcat-6/v6.0.18/bin/apache-tomcat-6.0.18.tar.gz

2. Extract the tarball under /usr/local/

Note: Locally installed software must be placed within /usr/local rather than /usr unless it is being installed to replace or upgrade software in /usr.

tar xvzf apache-tomcat-6.0.18.tar.gz

3. Tomcat requires that you set the JAVA_HOME variable. If you haven’t done it yet, you can do so by adding the following line in your .bashrc

export JAVA_HOME=/usr/lib/jvm/java-6-sun

4. We can test if Tomcat is running by running the startup script then opening a browser and accessing http://localhost:8080

sudo /usr/local/tomcat/bin/startup.sh

Details on troubleshooting can be found on RUNNING.txt

Read the rest of this entry »

Written by Mark Basmayor

March 2, 2009 at 5:37 pm

Posted in Java, Ubuntu

Tagged with , , , ,

Setting Up MySQL/JDBC Driver on Ubuntu

with 5 comments

Assuming that you already have MySQL installed, the next step is to install the connector driver. You can do this easily on the CLI by using the following command:

sudo apt-get install libmysql-java

The next step is to make sure that the classpath is set. You can have this set automatically by adding this command to you bashrc file.

export CLASSPATH=$CLASSPATH:/usr/share/java/mysql-connector-java.jar

If you want to set this for all users, you should modify the /etc/environment instead.

For those using Eclipse, you can also do this by going through the following steps:

  1. Select Project Properties > Java Build Path
  2. Select Libries tab
  3. Click Add External Jars
  4. Choose the jar file, in this case mysql-connector-java.java

Once you’re done, you can test the connection using the following snippet:

import java.sql.Connection;
import java.sql.DriverManager;

class JDBCTest {

	private static final String url = "jdbc:mysql://localhost";

	private static final String user = "username";

	private static final String password = "password";

	public static void main(String args[]) {
		try {
			Connection con = DriverManager.getConnection(url, user, password);
			System.out.println("Success");

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

Written by Mark Basmayor

March 1, 2009 at 1:32 pm

Posted in Java, Ubuntu

Tagged with , , , ,

Writing Better Conditional Statements

leave a comment »

1. Use 1 == foo instead of foo == 1.  Srsly.

At a glance, you might think that the following statements are the same as dictated by logic and the property of commutativity.

if( foo == 1 ) {
...

and

if( 1 == foo ) {
...

Well logically yes, but in a pragmatic point of view, the second one is better because it makes the compiler barf an error whenever you’re in a zombie programming mode and you write = (which assigns the value to the variable instead of checking for equality) in lieu of ==.
I just can’t imagine how much time I spent debugging before, only to find out that I missed a single character.

2. Proper use of .equals

First of all, when comparing strings, always use .equals. The == operator compares references and not values.
Now, if you're checking for a "" or a null string, use the following form:

if( "".equals( foo ) ) {
...

This way, you do not get a Null Pointer Exception whenever the variable is null.

Written by Mark Basmayor

February 28, 2009 at 4:49 pm

Follow

Get every new post delivered to your Inbox.