IgniTech Logo

IgniTech Logo
Vinoth

Wednesday, March 4, 2009

How to Compile and Run Java Program in Linux using JDK

Normally most the linux distros comes with gcj, which is a Java compiler for gnome. Most of the java programmers prefer only jdk. So how to compile and run a java program in linux. Download the latest jdk from the sun website. (jdk-1_5_0_16-linux-i586.bin for a x86 system)

http://java.sun.com/javase/downloads/index_jdk5.jsp

And then to install jdk use the following

rosarioarun@ros-lap:~$ ./jdk-1_5_0_16-linux-i586.bin

This will extract the jdk and give an folder in current path as, jdk-1_5_0_16, this folder will have all bin, lib directories required by the javac compilation and execution.
All the contents inside this directory will be same as the conents when you install the jdk in the windows. You will have a question that, why not i use the extracted jdk folder in windows, in linux to compile the java program in linux.
Both the extracted jdk folders in windows and linux are not the same, the difference is the extracted contents in windows will have all exe file, where as in linux it will be binary files.
That is javac in windows will be javac.exe and in linux it will be javac.bin
Now to set the path proceed as below

rosarioarun@ros-lap:~$ PATH=$PATH:/home/rosarioarun/jdk1.5.0_16/bin/

this is just a single line script to set the path, you can view the PATH variable using echo

rosarioarun@ros-lap:~$ echo $PATH

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/home/rosarioarun/jdk1.5.0_16/bin/

Now you can compile the java programs using javac and run them using java commands.
Compiling: $javac filename.java
Executing: $java filename

--
vinoth

No comments:

Post a Comment