rioastamal.net

Just things inside my head…

Archive for the ‘Java’ Category

Tutorial: How to Install Apache Ant on Ubuntu Linux

Posted by rio On October - 14 - 2008

Sorry if there are any spelling or grammatical errors, I’m learning English… :)

On my last article I’ve show you how to install Java Development Kit(JDK) on Ubuntu Linux. In this tutorial we will try to install Apache Ant or Ant in short. Before going more deeper, some of you may ask, What is ant?

Apache Ant is a software tool for automating software build processes. It is similar to make but is implemented using the Java language, requires the Java platform, and is best suited to building Java projects.

If you ever try building some application from source in Linux, I bet you have used build tool called make. Ant is very similar to it but Ant use XML based configuration for it’s build process. Ant is also used internally by NetBeans IDE to compile project. OK, enough talk let’s install Ant.

My Environment

  • My $HOME is located in /home/astadev
  • I put Ant package in $HOME/archive/a
  • I extract the package to $HOME/programs
  • My Ant version is 1.7.1

Pre-Installation
You must have Java installed first, go to my Java installation tutorial if you have not install Java yet.

Installation
Download the latest version of Ant from http://ant.apache.org. Saved to some location e.g: I save to $HOME/archive. First we need to extract the package.

$ tar -jxvf ~/archive/a/apache-ant-1.7.1-bin.tar.bz2 -C ~/programs
$ cd programs
$ ln -s apache-ant-1.7.1 ant

Note: if your ant package in .gz format use -zxvf instead.

The next step is to add Apache Ant directory to shell environment variables. So we need to edit .bashrc file located in our home directory.

$ gedit ~/.bashrc

Put this at the end of .bashrc file, after editing your file look something like this:

JAVA_HOME=/usr/local/java
JAVA_BIN=$JAVA_HOME/bin
ANT_HOME=/home/astadev/programs/ant
PATH=$PATH:$JAVA_HOME:$JAVA_BIN:$ANT_HOME/bin
export PATH

Save the file, and then open new bash session by pressing CTRL-Shift-T. Try to execute ant.

$ ant
Buildfile: build.xml does not exist!
Build failed

Error? No. Error above indicate that ant command is recognized by shell but it did not find build.xml file that needed to compile ant projects. So, it’s absolutely normal and the installation was successful.

Comment and feedback are welcome :).

References:
http://ant.apache.org/
http://en.wikipedia.org/wiki/Apache_Ant

bookmark bookmark bookmark bookmark bookmark bookmark

Tutorial: How to Install Java on Ubuntu Linux

Posted by rio On October - 2 - 2008

Sorry if there are any spelling or grammatical errors, I’m learning English… :)

By default ubuntu comes with pre-installed Java Runtime Environment called gij (The GNU Java bytecode interpreter). But gij interpreter known to be not compatible with some Java code. So, most of linux user update their JVM to Sun JVM.

Basically we have two methods to install Java/JDK on ubuntu. The first is using apt-get command that will install Java through ubuntu repository, and the second one is manually install Java using traditional method “unpack and run”. The first method is the most easiest way to install Java, but the package(Java version) maybe quite out of date. My recommendation is to use the second method since it provide you the latest JDK version. Let’s try the first method.

Install using apt-get

Open your terminal(Application - Accessories - Terminal) then type:

$ sudo apt-get install sun-java6-jdk

Note: We are using sun-java6-jdk not sun-java6-jre since I assume we are going to use it to develop Java programs not only for running the programs.

Follow the instruction to complete the instalataion. Done? yeah what do you expect more? we are in debian world :). Then we need to check whether the Sun JVM is properly installed or not by typing:

$ java -version

You should get information about your current JVM. The JVM should provided by Sun NOT gij anymore. Now let’s move to the second method.

Install using “unpack and run” method

In this method off course you need to download the JDK package from Sun’s Website. Go to http://java.sun.com/ and grab the latest JDK version. In this tutorial I use JDK version 1.6.0 Update 7. Download the file jdk-6u7-linux-1586.bin and save it to some location e.g: /home/user/jdk-6u7-linux-1586.bin.

Open the terminal to extract the package, move to the saved location first:

$ cd /home/user
$ sh jdk-6u7-linux-1586.bin

You will be asked “License Agreement” and type yes to Agree. After you complete those steps a folder named “jdk1.6.0_07″ created. Now, I don’t want to run jdk from my home folder, so I move it to /usr/local. Since /usr/local is owned by root we need sudo command.

$ sudo mv jdk1.6.0_07 /usr/local
$ sudo ln -s /usr/local/jdk1.6.0_07 /usr/local/java

The second command ln -s is used to make symbolic link. Now you can also access /usr/local/jdk1.6.0_07 with /usr/local/java.

We have successfully install the new JDK, but when type java -version the current JVM is still gij. So, how do I change this? We need to tell the shell that we want to use our JDK located in /usr/local/java not the gij, for that purpose we use update-alternatives command.

$ update-alternatives --list java
/usr/bin/gij-4.2

After running that command we knew that there is no alternative for java. So, we need to add our new JDK to the alternatives. Since we are modifying the system, sudo command is needed to do so.

$ sudo update-alternatives --install /usr/bin/java java /usr/local/java/bin/java 100
$ sudo update-alternatives --config java
There are 2 alternatives which provide `java'.
 
  Selection    Alternative
-----------------------------------------------
*         1    /usr/bin/gij-4.2
 +        2    /usr/local/java/bin/java
 
Press enter to keep the default[*], or type selection number: 2
$ java -version
java version "1.6.0_07"
Java(TM) SE Runtime Environment (build 1.6.0_07-b06)
Java HotSpot(TM) Client VM (build 10.0-b23, mixed mode, sharing)

For more information about update-alternatives see the manual page man(1) update-alternatives.

Now let’s test our new environment, for this purpose we will create simple Java program. Open your favorite text editor, e.g gedit (Application - Accessories - Text Editor) and try this following code.

1
2
3
4
5
public class Hello {
   public static void main(String[] args) {
      System.out.println("Hello World!") 
   }
}

Save it to some location, since this is only a test I would recommend to save it under /tmp directory. I name it Hello.java. Now go back to the terminal and compile the file using javac command.

$ cd /tmp
$ javac Hello.java
bash: javac: command not found

Oops…, what am I missing? No you don’t, the are few steps that we did not completed yet :). We need to add /usr/local/java in shell environment variables by editing .bashrc file located in our home directory.

$ gedit ~/.bashrc

Add the following lines into .bashrc (put these code at the end of file).

JAVA_HOME=/usr/local/java
JAVA_BIN=$JAVA_HOME/bin
PATH=$PATH:$JAVA_HOME:$JAVA_BIN
export PATH

Save the file and close the editor. Now we need to close our Terminal to take affect. Open the terminal again and try to compile Hello.java. All the things should work :).

$ cd /tmp
$ javac Hello.java
$ java Hello
Hello World!

Comment and feedback are welcome… :)

bookmark bookmark bookmark bookmark bookmark bookmark