Installing JDK and Write Your First Java Program

The java development kit (JDK) is product of sun Microsystems used to develop java software and is an extended subset of a java software development kit (SDK).

Installation steps 



Download Java Package HERE 

Create a Directory

#mkdir -p /usr/java

Extract JDK.tar file to the Directory

#tar -xvf jdk-8u91-linux-x64.tar -C /usr/java



Now delete the old java and update new#cd /etc/alternatives/ 

# rm -f java javac keytool

Create Link using below command 

# ln -s /usr/java/jdk1.8.0_91/bin/java .

# ln -s /usr/java/jdk1.8.0_91/bin/javac .

# ln -s /usr/java/jdk1.8.0_91/bin/keytool .



Set the class path

# vi .bash_profile   or .bashrc

export  JAVA_HOME=:/usr/java/jdk1.8.0_91/bin

export PATH=$PATH:/usr/java/jdk1.8.0_91/bin
export CLASS=$CLASSPATH:/usr/java/jdk1.8.0_91/jre/lib
:wq!



Now Verify 

#source .bashrc

#echo $JAVA_HOME

/usr/java/jdk1.8.0_91

#java -version

java version "1.8.0_91"
Java(TM) SE Runtime Environment******

Writing a Java hello world program
Create a file with name  HelloWord.java and type the following content:
# vi HelloWord.java
class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello world!");
}
}

Compiling it
Now let’s  compile our first program in the HelloWorld.java  file using javac tool. Type the following command

#javac HelloWorld.java

Running java 
It’s now ready to run our first Java program. Type the following command:
#java HelloWorld

Note :If you get error like "Error: Could not find or load main class First"

make sure to set correct java home and classpath and in java code "class HelloWorld" the same  "HelloWorld"  should be the file name.


Installing JDK and Write Your First Java Program Installing JDK and Write Your First Java Program Reviewed by TecGeeks News on April 08, 2016 Rating: 5

No comments:

Get Your Business Online Now


Powered by Blogger.