Fix: Tomcat taking very long to serve first request on fresh install on Ubuntu 14.04

Yesterday, I was trying to setup Apache Tomcat 7 on Ubuntu 14.04 on a new DigitalOcean droplet. But it was taking VERY long (10-15+ minutes) to serve first request. After that, it started serving requests normally. If the Tomcat was restarted, it again took very long to serve the first request.

I tried large droplets with more memory. Also, I tried installing Tomcat from both the Ubuntu repository package and from official Tomcat website. But in every case, I faced the same problem.

Turns out, the solution is pretty simple. We just need to install a package named  haveged like this:

Now restart Tomcat and you will see that it starts serving requests immediately at normal speed.

Basically what happens is that the entropy pools run dry on our virtual server running in the cloud. As a result, Tomcat blocks on /dev/random waiting for random numbers to be generated. Haveged seeds our system’s random source (usually /dev/random) using differences in our processor’s time stamp counter (TSC) after executing a loop repeatedly. Please refer to this article on DigitalOcean for details of this process.

Linux: Check from command-line if your OS is 32 bit or 64 bit

Do you need to find out if your Linux installation is 32 bit or 64 bit? I usually need this information when manually installing a piece of software (e.g. Netbeans Profiler Remote Pack).

There are two alternative commands which produce identical output. You can use any one of these:

  1. arch
  2. uname -m

If any of these commands outputs  x86_64 then your Linux is 32 bit. On the other hand, if the output is  i686 or  i386 then you are running a 64 bit Linux distro.

For detailed discussion of these and some other useful, related commands, please checkout: http://www.howtogeek.com/198615/how-to-check-if-your-linux-system-is-32-bit-or-64-bit/

Programmatically Get Absolute Path of Java Executable in a Running Program

Do you want to get absolute path of the specific Java executable that was used to launch a program, from within that program itself? May be you want to launch another Java program P2 from a program P1 and you want to make sure that P2 is launched using the same version of Java that was used to launch P1?

It is possible. Java runtime makes available a system property  java.home which contains the path to its installation or root directory. Java executable is usually located (tested on Windows and Mac) inside  java.home in a folder named  bin . We can combine these two facts to get absolute path of the Java executable that was originally used to launch a program. Here is a code snippet that does exactly this:

You will need to import  java.io.File in your source code to compile it. After these couples of lines have executed,  javaExe will contain our required path. You can use it for whatever purpose you want. Here is a sample code snippet that uses this Java executable to launch another Java program in a separate JVM. Continue reading Programmatically Get Absolute Path of Java Executable in a Running Program

SOLUTION: No Java runtime present, requesting install – Mac OS X Yosemite

I was working on a simple java based desktop application today. It was packaged in an executable jar. One of its required features was to launch another executable jar in a separate instance of JVM. Here is my code dealing with this requirement:

This is equivalent of running following command on terminal.

This code was working fine on Windows and Ubuntu. But on Mac, it resulted in a dialogue like this: Continue reading SOLUTION: No Java runtime present, requesting install – Mac OS X Yosemite

Send Emails from PHP on Ubuntu localhost LAMP setup

In this article, I will walk you through the process of configuring your local, Ubuntu based development machine to send emails from within PHP and also from the command line terminal. In a nutshell, you need to install and configure a mail server on your local computer. The mail server that we are going to use is Postfix. So without any further delay, lets get started.

Open up the terminal and, first of all, enter the following command:

It will make available the “mail” command on your terminal and also will install all the programs that it depends on. Our desired mail server Postfix is one of them. During installation, accept the defaults as configuration options of Postfix when asked.

Once the installation is complete, type following command:

Continue reading Send Emails from PHP on Ubuntu localhost LAMP setup