Wednesday, August 6, 2008

installing java and running a simple program

Hello all! I downloaded the jdk package from SUN's site and installed it offline. It's just simple.

offline istallation
---------------------

If you download the .bin file from Sun's site, then you can install it by:
sudo ./j2sdk-1_4_2_16-linux-i586.bin

online istallation
---------------------
sudo aptitude install sun-java6-jdk


Now, you have to set the path variables for JAVA:
export JAVA_HOME=/mnt/F/ubuntu/j2sdk1.4.2_16
export PATH=$JAVA_HOME/bin:$PATH

Now, switch to the directory /usr/bin :
cd /usr/bin

Now execute these commands sequentially:
sudo rm ./java
sudo rm ./java_vm
sudo rm ./javaws

sudo ln -s /mnt/F/ubuntu/j2sdk1.4.2_16/bin/java ./java
sudo ln -s /mnt/F/ubuntu/j2sdk1.4.2_16/bin/java_vm ./java_vm
sudo ln -s /mnt/F/ubuntu/j2sdk1.4.2_16/bin/javaws ./javaws
sudo ln -s /mnt/F/ubuntu/j2sdk1.4.2_16/bin/javac ./


You are done! Now you can run any java program from any directory you like.


Compiling your first java program

---------------------------------

You may write a sample code like this:
//First Java Program
class Main
{
public static void main (String[] args)
{
System.out.println ("Hello Ubuntu!");
}
}

Compile it:
javac Main.java

Run it:
java Main

you should get the output "Hello Ubuntu!"

installing and running gcc / g++ compiler in ubuntu


New to ubuntu? just stumbled upon installing gcc compiler and running a sample program? It's simple!

open up a terminal and write the command:

sudo aptitude install build-essential

It will automatically install gcc/g++ compiler into your system. Also you can do the same thing from the Synaptic package manager. Open the Synaptic package manager, search for a package "gcc" and then you will get the list of available packages. Just select g++ from there and install. Done !

To test it, write a simple C code like this and name it test.c:

#include
int main()
{
printf("Hello ubuntu!");
return 0;
}

to compile it:
g++ -lm -o test.o test.c

here -lm indicates that the compiler may invoke the math library. test.o is the name of the object file.

finally, you can get the output by executing the object file:
sudo ./test.o

Here you can find details of the build-essential package:
http://packages.ubuntu.com/hardy/build-essential