Skip to content


Compiling Java Code

Java is a very popular language. Here is an introduction on how to compile and run Java programs. The first step is to download and install the latest Java Development Kit (JDK), which allows you to compile and run java code. Note that there is a Java Runtime Environment (JRE) which only allows you to run applications made in Java, you have to download the JDK which contains the development and runtime environments. You can download it from here.

The next step is to write a simple Java program. Using any plain text editor, write the following code:

class mainClass
{
    public static void main(String args[])
    {
    System.out.println("Hello there...");
    }
}

The filename has to be the same as the name of the class defined in the code. In this case, the class that I created is called mainClass. Also, the filename needs to have a .java extension. Therefore, the file has to be saved under the name of mainClass.java. Now you can compile the code using javac which is the Java compiler.

javac *.java

If you need to include some other folders with pre-compiled .class files or other libraries in .jar format, you need to use the -classpath argument. Note that for each .jar you need to specify the complete path. Each inclusion is separated with a semicolon (;). Remember to include the current directory by adding a single dot (.).

javac -classpath .;"c:\path\to\classFiles";"c:\path\to\lib1.jar" *.java

Now the code is ready to be run. Just call java and the name of the class that contains the Main function, which in this case is mainClass.

java mainClass

You can also pack your application into a Jar file, which basically is a compressed file containing the machine code you already compiled and a manifest file. The manifest file tells extra information about the application, in particular which one is the main class to execute. Let’s create this file. Open a text editor and write the following:


Main-Class: mainClass
Manifest-Version: 1.0

As you can see, you have to define which is the Main-Class that is going to be executed when the Jar file is run. Save this file as Manifest.txt. Although the second line itself is optional, it is important to add it (or even just a blank line) below the Main-Class definition because the Main-Class line needs to contain an end of line character in order to work properly. If you do not add an end of line character at the end of the Main-Class line you will receive the following error when you try to run your Jar file:

Failed to load Main-Class manifest attribute from main.jar

Jar is the application that allows you to pack your application. The c argument means that you are creating a Jar file. The f argument allows you to give a filename instead of sending the output to the console. The m argument means that you are telling the program where the manifest file is. Then, you need to provide the jar filename and the manifest filename. In this case, I used the main.jar filename but you can use any name as long as it has a .jar extension. The manifest filename can be anything you want, in this case I used Manifest.txt which was created previously. The last argument tells the program which files to include in the Jar package, in this case I simply used all the files that end in .class which is general enough for this introduction.

jar cfm main.jar Manifest.txt *.class

Now you have your application packed in a Jar file. If you want to run it, you only need to write this:

java -jar main.jar

Now you are able to compile and run Java programs.

class myfirstjavaprog
{
        public static void main(String args[])
        {
           System.out.println("Hello World!");
        }
}

Posted in Programming.


Installing OpenCV 2.0 in Ubuntu

OpenCV is an excellent library for Computer Vision. I have been using it for years and it helped me a lot during my master thesis.

OpenCV 1.0 can be easily installed in Ubuntu via the repositories. Unfortunately, the newer version of OpenCV, 2.0, which is full of new features is not yet available in the repositories of Ubuntu.

Here are the steps that I used to successfully install OpenCV 2.0 in Ubuntu 9.10. I have used this procedure for previous versions of Ubuntu as well with minor modifications (if any).

First, you need to install many dependencies, such as support for reading and writing jpg files, movies, etc… This step is very easy, you only need to write the following command in the Terminal


sudo apt-get install build-essential libgtk2.0-dev libavcodec-dev libavformat-dev libjpeg62-dev libtiff4-dev cmake libswscale-dev libjasper-dev

The next step is to get the OpenCV 2.0 code:


cd ~
wget http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/2.0/OpenCV-2.0.0.tar.bz2/download
tar -xvf OpenCV-2.0.0.tar.bz2
cd OpenCV-2.0.0

Now, you need to configure how you want to compile OpenCV 2.0. In my case I used /usr/local as the installation directory and I also activated the video support for reading and writing videos using OpenCV (via ffmpeg).


./configure --prefix=/usr/local --enable-apps --enable-shared --with-ffmpeg --with-gnu-ld --with-x --without-quicktime CXXFLAGS=-fno-strict-aliasing

Check that the above command produces no error and that in particular it reports ffmpeg as yes. If this is not the case you will not be able to read or write videos.

Now, you are ready to compile and install OpenCV 2.0:


make
sudo make install

Now you have to configure the library. First, open the opencv.conf file with the following code:


sudo gedit /etc/ld.so.conf.d/opencv.conf

Add the following line at the end of the file(it may be an empty file, that is ok) and then save it:


/usr/local/lib

Run the following code to configure the library:


sudo ldconfig

Now you have to open another file:


sudo gedit /etc/bash.bashrc

Add these two lines at the end of the file and save it:


PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH

Finally, open a new console, restart the computer or logout and then login again. OpenCV will not work correctly until you do this.

Now you have OpenCV 2.0 installed in your computer.

Let’s check some demos included in OpenCV:

cd ~
mkdir openCV_samples
cp /usr/local/share/opencv/samples/c/* openCV_samples
cd openCV_samples/
chmod +x build_all.sh
./build_all.sh

All the training data for object detection is stored in /usr/local/share/opencv/haarcascades. You need to tell OpenCV which training data to use. I will use one of the frontal face detectors available. Let’s find a face:

./facedetect --cascade="/usr/local/share/opencv/haarcascades/haarcascade_frontalface_alt.xml" --scale=1.5 lena.jpg

Note the scale parameter. It allows you to increase or decrease the size of the smallest object found in the image (faces in this case). Smaller numbers allows OpenCV to find smaller faces, which may lead to increasing the number of false detections. Also, the computation time needed gets larger when searching for smaller objects.

Now let’s see some background subtraction from a video. The original video shows a hand moving in front of some trees. OpenCV allows you to separate the foreground (hand) from the background (trees).

./bgfg_segm tree.avi

There are many other samples that you can try.

Posted in Computer Vision, Open Source, OpenCV.