Archive for the 'Java' Category

Installing tomcat 6.0 on MediaTemple dedicated server

In the last post, I looked at how Java can be configured on MediaTemple CentOS server. This post will show you how you can install Tomcat on your server.

1. Download and extract tomcat 6.0 binary distribution. Get the URL of the tar.gz file from http://tomcat.apache.org/download-60.cgi. From the ssh console download using wget.

wget http://apache.deathculture.net/tomcat/tomcat-6/v6.0.14/bin/apache-tomcat-6.0.14.tar.gz

Now use the following commands to extract the files.
gunzip -d apache-tomcat-6.0.14.tar.gz
tar -xvf apache-tomcat-6.0.14.tar

2. set JAVA_HOME variable to point to your Java installation.For this you need to login as root. Create or edit a java.sh inside /etc/profile.d. Add the following line in java.sh.

export JAVA_HOME=/usr/java/jdk1.6.0_04

3. Start tomcat from bin folder. Login again to SSH using your non root user id.  The startup script for tomcat is in [tomcatdir]/bin. Change to bin and then execute,

./startup.sh

Voila! You should see the tomcat server running at www.yourdomain.com:8080/

To shutdown you can use ./shutdown.sh, but for some reason I am not able to execute it. So currently the only way to stop is to kill the Java process from SSH. Update: I got shutdown.sh working by adding -Xmx4m as JVM parameter in catalina.sh for “stop” block of code.

Now my next task is to get Struts2 sample application running on this…

Installing Java 1.6 (JDK6.0) on MediaTemple dedicated virtual server (dv)

I recently bought a dedicated virtual hosting service on MediaTemple. The idea was to use this server for all my future projects in J2EE and Ruby on Rails. For $50/month, this service is not exactly cheap, but I am yet to find an alternative VPS host (I am planning to evaluate SliceHost sometime in future)

By default, the server comes with Ruby 1.8.5, but not Java compiler. My first task was to install latest JDK (1.6) on the VPS server.

All dedicated virtual servers hosted by MediaTemple comes with a CentOS installation. You can check that by running the following command on SSH terminal.

cat /etc/redhat-release

I got CentOS release 4.5 (Final) on the console.

Now before you can install Java, you need to know the password of “root” user in your server. For this, create a new support request and ask MediaTemple to set your root password of your choice.

Now comes the second problem. How do you download Java binaries onto your server? One option is to download JDK on to your machine and then upload it to server. Since the file size is more than 60MB, I didn’t want to use this method.

What you need is the ability to download J2SE 1.6 rpm binaries directly to your dedicated server. The Linux command “wget” is the ideal tool for this. But what is the URL to download?

The problem with Java download is that there is no direct link available for download. In order to force license display and acceptance, Sun uses a hidden redirect to the actual download. You need to find this URL.

For MediaTemple installation, select Java download which is “jdk-6u4-linux-i586-rpm.bin” from the list at http://developers.sun.com/downloads/. Using Firefox, start the download. Immediately pause the download and then righ click on the “resume” link to get the actual download link.

Paste the URL in a text editor and then quote it by adding double quotes at the beginning and at the end of the URL.

Now from the SSH terminal, type the following.

wget <quoted url from text editor> -O j.bin

This will download the Java binary as j.bin in the current folder. Now change to super user mode by typing sudo su.

Make the j.bin executable for using the command chmod a+x j.bin and execute it by ./j.bin. Accept license terms and continue. You will get the Java installed on the MediaTemple DV server.

But you are not there yet! Following is the error I got when I ran “java -version” on the command prompt.

Error occurred during initialization of VM
Could not reserve enough space for object heap
*** glibc detected *** free(): invalid pointer: 0xb7e71280 ***
Aborted

On CentOS you need to do something more to get Java installed. You need to set a link to Java. Execute the following from the root privileges. Change jdk1.6.0_04 to your version of JDK download.

/usr/sbin/alternatives –install /usr/bin/java java /usr/java/jdk1.6.0_04/bin/java 1

After this I was able to get a sample Java program running on the MediaTemple dedicated server. Now the next step is to install Tomcat App server. That is for another day….

Quick Java Tips - List conversions

Here are some quick Java tips. This is based on my recent code reviews.

1. How to convert HashMap keys to an ArrayList?
new ArrayList(map.keySet());

2. How to convert an Array to an ArrayList?
new java.util.ArrayList(java.util.Arrays.asList(array));

3. How to create and initialize a List in one step?
java.util.List = java.util.Arrays.asList(new String[] {”1″, “2″});