Archive for May, 2009

Install OpenOffice 3.1 on Ubuntu

These are the steps to install OpenOffice 3.1 on Ubuntu.
First of all download OpenOffice 3.1 from the official web site. When you’re done, follow these steps:

1. Remove previous version

sudo apt-get remove openoffice*.*

2. Remove settings folder
This will remove ALL your OpenOffice settings

rm ~/.openoffice.org -rf

3. Expand the archive

tar -xvzf OOo_3.1.0_LinuxIntel_install_en-US_deb.tar.gz

4. Setup

cd OOO310_m11_native_packed-4_en-US.9399/DEBS/
sudo dpkg -i *.deb

5. Setup desktop integration

cd desktop-integration
sudo dpkg -i openoffice.org3.1-debian-menus_3.1-9393_all.deb

Java tail

A java implementation of the Unix tail command. This is a small customization of the java tail class created by Luigi Viggiano. I’ve just added some controls and added the update time parameter.

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
 
/**
 * Java implementation of the Unix tail command
 * 
 * @param args[0] File name
 * @param args[1] Update time (seconds). Optional. Default value is 1 second
 * 
 * @author Luigi Viggiano (original author) http://it.newinstance.it/2005/11/19/listening-changes-on-a-text-file-unix-tail-implementation-with-java/
 * @author Alessandro Melandri (modified by)
 * */
public class Tail {
 
  static long sleepTime = 1000;
 
  public static void main(String[] args) throws IOException {
 
    if (args.length > 0){
 
      if (args.length > 1)
        sleepTime = Long.parseLong(args[1]) * 1000;
 
      BufferedReader input = new BufferedReader(new FileReader(args[0]));
      String currentLine = null;
 
      while (true) {
 
        if ((currentLine = input.readLine()) != null) {
          System.out.println(currentLine);
          continue;
        }
 
        try {
          Thread.sleep(sleepTime);
        } catch (InterruptedException e) {
          Thread.currentThread().interrupt();
          break;
        }
 
      }
      input.close();
 
    } else {
      System.out.println("Missing parameter!\nUsage: java JavaTail fileName [updateTime (Seconds. default to 1 second)]");
    }
  }
}

Today links

VMWare and Linux

If you install VMware on linux you may find that some keys (up/down keys, home key, etc…) don’t work in the guest operating system.

You need to create a config file inside the .vmware folder in your home directory and paste this text info the file:

xkeymap.keycode.108 = 0x138 # Alt_R
xkeymap.keycode.106 = 0x135 # KP_Divide
xkeymap.keycode.104 = 0x11c # KP_Enter
xkeymap.keycode.111 = 0x148 # Up
xkeymap.keycode.116 = 0x150 # Down
xkeymap.keycode.113 = 0x14b # Left
xkeymap.keycode.114 = 0x14d # Right
xkeymap.keycode.105 = 0x11d # Control_R
xkeymap.keycode.118 = 0x152 # Insert
xkeymap.keycode.119 = 0x153 # Delete
xkeymap.keycode.110 = 0x147 # Home
xkeymap.keycode.115 = 0x14f # End
xkeymap.keycode.112 = 0x149 # Prior
xkeymap.keycode.117 = 0x151 # Next
xkeymap.keycode.78 = 0x46 # Scroll_Lock
xkeymap.keycode.127 = 0x100 # Pause
xkeymap.keycode.133 = 0x15b # Meta_L
xkeymap.keycode.134 = 0x15c # Meta_R
xkeymap.keycode.135 = 0x15d # Menu

Add maxlength functionality to textarea

The textarea field doesn’t have a maxlength attribute like text fields, so you need Javascript to get the same functionality:

function getMaxLength(textarea,maxlength){
 
  if (textarea.value.length > maxlength)
    textarea.value = textarea.value.substring(0,maxlength);
 
  updateCounter(textarea.value.length,maxlength);
}
 
function updateCounter(currentCount,maxlength){
  document.getElementById('counter').innerHTML = currentCount + "/" + maxlength;
}

Now modify the textarea like this and you are done:

<form>
  <textarea onkeyup="return getMaxLength(this,500)" cols="60" rows="10"></textarea>
<div id="counter">0/500</div>
</form>

Setup a LAMP server on Ubuntu

These are the steps to install an Apache-MySQL-PHP server on an Ubuntu desktop machine.

# Install Apache 2
sudo apt-get install apache2
 
#Install PHP5
sudo apt-get install php5
 
#Install MySQL
sudo apt-get install mysql-server
 
#Install MySQL for Apache
sudo apt-get install libapache2-mod-auth-mysql
 
#Install PHP MySQL Support
sudo apt-get install libapache2-mod-php5
sudo apt-get install php5-mysql
 
#Restart Apache
sudo /etc/init.d/apache2 restart

Managing Apache

Web files must be placed in ”/var/www/”.

sudo /etc/init.d/apache2 start
sudo /etc/init.d/apache2 stop
sudo /etc/init.d/apache2 restart

Managing MySql

sudo /etc/init.d/mysqld start
sudo /etc/init.d/mysqld stop
sudo /etc/init.d/mysqld restart

Install Sun JDK on Linux

I’ve tested this procedure on Fedora 10 and Ubuntu 9.04 but It should work on other distributions too.

First of all download the latest JDK package from the SUN page: be sure to download the bin file and not the rpm.

From now on you’ll need to run commands using ”sudo”.

Move the package to /opt/ and make it executable.

cd /path/to/download/folder
mv jdk-versionnumber-linux-i586.bin /opt/
cd /opt/
chmod +x jdk-versionnumber-linux-i586.bin

Now start the installation and follow the onscreen instructions

./jdk-versionnumber-linux-i586.bin

When the installation is done you’ll need to set the JAVA_HOME enviroment variable and add java executable to the system pah.

Open the file /etc/profile and add the following lines

JAVA_HOME="/opt/jdk_versionnumber"
export JAVA_HOME
 
PATH=$PATH:/opt/jdk_versionnumber/bin/
 
export PATH

Now add a symbolic link for the java command

ln -s /opt/jdk_versionnumber/bin/java /usr/bin/java

Log off and log in back and you’re done.

Get the WEB-INF folder path

This is a simple example on how to get the WEB-INF directory path in a J2EE web application.

public class MyClassName{
 
  private static final String WEBINF = "WEB-INF";
 
  public String getWebInfPath(){
 
    String filePath = "";
 
    URL url = MyClassName.class.getResource("MyClassName.class");
    String className = url.getFile();
 
    filePath = className.substring(0,className.indexOf(WEBINF) + WEBINF.length());
    return filePath;
  }
 
}

Connect to MySql using Java

This is a simple class that I usually use to connect to a MySQL database. Obviously the right MySQL JDBC driver must be in your classpath.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
 
public class ConnectionManager {
 
    // Connection data -- START
 
    static final String driver   = "com.mysql.jdbc.my_driver_version";
    static final String dbserver = "mysql.server.name";
    static final String dbport   = "mysql.database.port";
    static final String dbname   = "mysql.database.name";
    static final String dbuser   = "mysql.database.username";
    static final String dbpass   = "mysql.database.password";
 
    // Connection data -- END
 
    /**
     * Opens a connection to the database
     *
     * @return Returns a Connection object
     */
    public static Connection getConnection(){
 
        Connection con = null;
 
        try {
 
            Class.forName(driver).newInstance();
            String conString = "jdbc:mysql://"+dbserver+":"+dbport+"/"+dbname;
            con = DriverManager.getConnection(conString,dbuser, dbpass);
 
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
 
        return con;
    }
 
    public static void close(Object object){
 
        if (object != null){
 
            try {
 
                if (object instanceof Connection)
                    ((Connection)object).close();
 
                else if (object instanceof Statement)
                    ((Statement)object).close();
 
                else if (object instanceof PreparedStatement)
                    ((PreparedStatement)object).close();
 
                else if (object instanceof ResultSet)
                    ((ResultSet)object).close();
 
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                object = null;
            }
        }
    }
}