Set up Java in CentOS 7

I have long been interested in Android application development, but I have not studied Java before. I was told that I can run Java on Windows using NetBeans, but now I would like to get this done on Linux, as in Linux world I can use my favorite VI, and not bothered with bulky IDE.

By default CentOS 7 has some version of Jave installed. I tried it, and while I was able to use javac to create class file, I was not lucky in running java. So I decided to switch to Sun Java.

1. Remove the default openjdk.

I am not sure if they can live happily with the Sun Java, so I decided to remove them. Note they are dependented by icedtea and liboffice, but I am not using them, so there is no trouble for me to remove all “*java*”.

Use yum to find out which java packages are currently installed. Below are what are installed on my machine.

# yum list installed | grep java

Then remove them using “yum erase”. You might want to double check if removing them will harm/brick your machine.

2. Get the lasted Java jdk from here.

3. Install..

# rpm -ivh jdk-8u45-linux-x64.rpm

4. Now we can do some short test. First we can check “java -version” and “javac”.

5. Before working on any projects, I would recommend configuring these environment variables. By default, Java is installed under /usr/java. You can also use “which java” to find out your installation directory.

JAVA_HOME=/usr/java/jdk1.8.0_45
JRE_HOME=/usr/java/jdk1.8.0_45/jre
PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin
CLASSPATH=".:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib"

6. Now let’s start with a simple test. Copy the following code as “Test.java”.

class Test {
  public static void main(String [] args) {
    System.out.println("My first java program");
  }
}

Then run these commands, and hopefully it will give you the following output. If so, Java is successfully installed.

$ javac Test.java
$ java Test
My first java program

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s