IgniTech Logo

IgniTech Logo
Vinoth

Thursday, March 19, 2009

Installing Java for Linux HOWTO

This tutorial explains how to uninstall and install the following on the Linux operating system:

  • Java 2 Platform, Standard Edition (J2SE), SDK 1.5.x
  • Java Web Start
  • Java Plug-in for most HTML browsers

If you are struggling with the installation instructions from Sun Microsystems, and your head is filled with more questions than answers concerning the finer points of Java installation on Linux, then this tutorial is for you.

Installing Java on Linux is almost easy, so don't worry that the tutorial is a bit thorough. The text covering the actual J2SE installation is just a few pages long, and you should be up and running Java in no time. Really! Most of the text is there just to keep you out of harms way if you get yourself into trouble.

Note: The tutorial has been tested successfully on the Red Hat Linux 7.2, 7.3, 8.0, and 9 operating system for SDK 1.4.0, SDK 1.4.1, SDK 1.4.2 and SDK 1.5.0.

The tutorial has also been found helpful for users of the Mandrake Linux 8.2 and 9.1 operating system.

Linux commands

If you find yourself stuck on those Linux commands, just take a look at The Linux Cookbook: Tips and Techniques for Everyday Use.

Warning: Do not be surprised if folder and file names, usually based on version numbers, have slightly different names on your installation than in the tutorial. New versions of Linux, J2SE and HTML browsers are released continuously, and I'm only human... Typos based on version numbers are the most reported typo error I get. Keep that in mind if something goes wrong with your installation.

Concept: Use the tab key to complete your typing when you are typing directory or file names, at least when you see the '*' character that the tutorial uses to replace version numbers. You should never copy and paste directly from the examples. The reason for this is that directory names may be different on your Linux version. /usr/java/j + tab may result in Linux writing /usr/java/j2sdk1.8.3/ or whatever.

About the Software

What is jdkgcj?

jdkgcj was created by Ark Linux: Easy Desktop Linux to make the gcj (gcj is a front end to gcc) commands more similar to the usual Java commands. An amazingly awful idea, considering that you don't even need jdkgcj in order to use gcj!! This software tend to create problems, so it is described here.

Why is Java not bundled with Red Hat Linux?

The JVM will not ever be included in Red Hat Linux because the JVM is not open source.
See: Bugzilla.

These days, it may seem strange that Red Hat does not come with Java already included. Actually, Red Hat Linux sort of comes with Java installed. It's called gcjand is part of the GNU Compiler Collection (GCC). gcj cunningly uses another program called jdkgcj in order to confuse everyone. gcj does not include a JVM and also lacks a few Java packages like AWT and Swing, so you basically still have a problem and you still need to install J2SE.

Why is J2SE with NetBeans IDE Cobundle installation not explained?

The tutorial does not describe installation of the NetBeans IDE Cobundle for a number of reasons:

  • Only developers needs NetBeans, and most users should avoid installing the cobundle.
  • J2SE and NetBeans are separate products. You may prefer to install a competing product like eclipse instead of NetBeans.
  • J2SE is installed in the NetBeans folder when installing the cobundle, and the J2SE installation is removed when uninstalling the cobundle.
  • It is much harder to reinstall J2SE or NetBeans independently if you first install the cobundle.
  • It makes the tutorial unnecessarily complicated if yet another installation should be described.

Uninstalling the Software

Before you start the installation, you should always find out if there are any programs that should be uninstalled first. Even if you think that your Linux box is clean, software like jdkgcj could play a trick on you.

jdkgcj uninstallation

Is jdkgcj installed?

To find out if jdkgcj is installed, open a terminal window and type:

[user]$ java -version 

If you read something similar to the following message, then jdkgcj has been installed on your computer, and you will no longer be able to compile and run Java programs normally. You should strongly consider removing it.

java version "1.3.1" jdkgcj 0.2.3 (http://www.arklinux.org/projects/jdkgcj) gcj (GCC) 3.2.2 20030222 (Red Hat Linux 3.2.2-5) Copyright (C) 2002 Free Software Foundation, Inc. This is free software; see the source for copying conditions.  There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 

Uninstalling jdkgcj

Open a terminal window, log in as the super-user and type:

[root]# rpm -e jdkgcj 

The jdkgcj package should now be removed, and if J2SE is installed correctly, all Java commands should now work perfectly.

Java Plug-in uninstallation

Uninstalling Java Plug-in for Mozilla browsers

If you are going to reinstall J2SE, you don't need to uninstall Java Plug-in. Just use the -f option for ln when creating a new symbolic link. In the unlikely case that you want to completely remove Java from your Linux box, the procedure of removing the symbolic link is described below.

Open a terminal window, log in as the super-user and type:

[root]# rm /usr/lib/mozilla/plugins/libjavaplugin_oji.so rm: remove symbolic link `/usr/lib/mozilla/plugins/libjavaplugin_oji.so'? y 

J2SE uninstallation

RPM in self-extracting file uninstallation

Note: If you have RPM on your Linux box, you should first find out if Java is already installed using RPM. If Java is not installed using RPM, you should skip reading.

Opening a terminal window. Try to find the j2sdk package by typing:

[user]$ rpm -q j2sdk 

Optionally, show all RPM packages by typing:

[user]$ rpm -qa 

If RPM reports a package similar to j2sdk-*-fcs, or jdk-* then Java is installed with RPM.

Note: Normally, you do not need to uninstall J2SE with RPM, because RPM is able to uninstall the old J2SE when installing a new version! You may skip reading, unless you want to remove J2SE permanently.

Warning: If you want to uninstall the NetBeans IDE, it must be uninstalled before you uninstall J2SE, because the InstallShield Wizard uninstaller is a Java application!

In order to uninstall J2SE using RPM, open a terminal window, log in as the super-user and type:

[root]# rpm -e j2sdk-*-fcs 

Self-extracting file uninstallation

This part of the tutorial has not been updated for a while...

Find out if Java is installed in some folder. Common catalogues are /usr/java/j2sdk* or /opt/j2sdk_nb/j2sdk*/bin/java. When you have located the folder, you may remove the folder.

Warning: You should be certain that Java is not already installed using RPM before removing the folder!

java.sh file

You may delete the java.sh file in /etc/profile.d/ if you want to remove J2SE permanently.

Installing the Software

J2SE installation

If you need to compile Java source code, and most of us do sooner or later, you should install the SDK. Don't bother installing the JRE, because it's included in the SDK anyway.

Note: If you download JRE instead of SDK, you will only be able to run Java applets and applications. SDK contains JRE and also essential tools to program in Java.

You may need to compile Java applications now and then even if you are not a programmer, because some applications are distributed as source only and must be compiled. If you plan on just using Java applets, you may go ahead and install JRE. Installing JRE is identical to SDK installation, except for slightly different filenames and missing development tools & APIs.

Note: There are two installation files for Linux, and you need only one of them. What file you want to download depends on your prefered installation method:

  • RPM in self-extracting file, using the Red Hat Package Manager (RPM)
  • Self-extracting file, doing everything yourself

Warning: The SDK file names changes frequently! You should always start your search for the latest files at http://java.sun.com/

RPM in self-extracting file installation

Note: RPM in self-extracting file is the recommended installation method in this tutorial. If your Linux has RPM installed and if you want to install and maintain Java in a similar way as other RPM packages, you should follow these instructions. Red Hat Linux and Mandrake Linux usually have RPM. Otherwise follow the instructions for self-extracting file.

To continue the Java installation, download the Linux RPM in self-extracting file called jdk-*-linux-i586-rpm.bin (for example jdk-1_5_0-linux-i586-rpm.bin.)

Open a terminal window and give the file execute permission:

[user]$ chmod +x jdk-*-linux-i586-rpm.bin 

File access permissions should now be:

[user]$ ls -l -rwx--x--x    1 user     user     44566787 oct  5 18:20 jdk-*-linux-i586-rpm. 

You should also check that the download file size is the same as printed on the download page.

Log in as the super-user and execute the file:

[root]# ./jdk-*-linux-i586-rpm.bin 

Read the "Sun Microsystems, Inc. Binary Code License Agreement". (As if... It's about 1 MB of lawyer mumble jumbo basically telling you that you just agreed to sell your soul to Sun Microsystems, Microsoft, SCO, Kodak and who else... Why do I love the BSD and GPL licenses??) Now, just press space repeatedly for quick scrolling, and type yes. You now have two files:

[root]$ ls jdk-*-linux-i586.rpm   jdk-*-linux-i586-rpm.bin 

Check the installation by printing the product version:

[root]$ /usr/java/jdk1.5.0/bin/java -version java version "1.5.0" Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-b64) Java HotSpot(TM) Client VM (build 1.5.0-b64, mixed mode, sharing) 

Self-extracting file installation

This part of the tutorial has not been updated for a while...

Note: If your Linux has not RPM installed, you should follow these instructions.

Download the self-extracting file called j2sdk-*-linux-i586.bin.

Open a terminal window, and type:

[user]$ chmod +x j2sdk-*-linux-i586.bin 

Log in as the super-user:

[root]# mkdir /usr/java [root]# mv j2sdk-*-linux-i586.bin /usr/java/ [root]# cd /usr/java/ [root]# ./j2sdk-*-linux-i586.bin [root]# rm j2sdk-*-linux-i586.bin 

Check the installation by printing the product version:

[root]# /usr/java/j2sdk*/bin/java -version 

java.sh file

This part of the tutorial describes how to put Java in your path. If you need to run Java from a terminal window without entering the complete path to the java andjavac commands each time, you must complete this step.

Note: You may still run NetBeans IDE even without the path.

Create and edit the java.sh file in /etc/profile.d/ with a simple text editor like gedit or Pico so that java and javac commands works for all users:

Warning: Be careful when editing /etc/profile.d/java.sh. You may not be able to run Linux afterwards (for instance, if you forget to type $ in $PATH). In this case, use the Red Hat Linux CD-ROM with boot: linux rescue.

[root]# gedit /etc/profile.d/java.sh 
export PATH=$PATH:/usr/java/jdk*/bin/ 

When you save the file, the file access permissions will look like:

ls -l -rw-r--r--    1 root     root           44 feb  6 22:11 java.sh 

You should now change the file access permission like this:

[root]# chmod +x java.sh [root]# ls -l -rwxr-xr-x    1 root     root           44 feb  6 22:11 java.sh 

Concept: You may wonder how this would ever work after you log out and back in again, without an explicit command to load the java.sh script. It works because the file /etc/profile executes all scripts in the /etc/profile.d folder.

Note: No other environment variables (such as JDK_HOMEJAVA_HOME or CLASSPATH) are required to run the SDK. However, some applicationsmay require their own environment variables. Check the installation instructions for those applications.

Note: Theoretically, you could edit /etc/profile instead of /etc/profile.d/java.sh, but the latter is easier to maintain. The export NPX_PLUGIN_PATHline is only needed for the Netscape browser Java plug-in. You should not change .bash_profile instead of /etc/profile.d/java.sh, because it will only affect one user.

Save the file and then exit from super-user. Update the NPX_PLUGIN_PATH and PATH environment variables by typing:

  [user]$ source /etc/profile.d/java.sh 

Note: This should work because you changed file access permissions earlier. Alternatively you may log out and then in again.

You should check the path and installation from a terminal window:

[user]$ java -version java version "*" Java(TM) 2 Runtime Environment, Standard Edition (build *) Java HotSpot(TM) Client VM (build *, mixed mode, sharing) 

Your path and installation is OK, if java prints the product version. If nothing happens, see what your PATH environment variable is set to:

[user]$ echo $PATH 

File System Preferences

If you try to run a Swing application with Java 1.4.1 or newer, you will get an error similar to this:

[user]$ java MySwingApp 21.feb.2003 20:22:34 java.util.prefs.FileSystemPreferences$3 run WARNING: Could not create system preferences directory. System preferences are unusable. 

Running a trusted Swing application once as root will create the preferences, and you will never see the message again:

[user]$ su Password:  [root]# java MySwingApp 21.feb.2003 20:32:47 java.util.prefs.FileSystemPreferences$3 run INFO: Created system preferences directory in /etc/.java/.systemPrefs 

Java Web Start

The J2SE installation also installs Java Web Start. You may start the Java Web Start from a terminal window:

[user]$ /usr/java/jdk*/jre/javaws/javaws 

Create Java Web Start Launcher

  1. Right click the desktop
  2. Click New Launcher
  3. Type in the Name text field: Java Web Start
  4. Type in the Command text field: "/usr/java/jdk*/jre/javaws/javaws"
  5. Click Icon and select: /usr/java/jdk*/demo/jfc/Metalworks/HelpFiles/javalogo.gif

If you have created the launcher already and just need to edit it, just drag it into your text editor, or open the file: .gnome-desktop/Java Web Start.desktop

Note: Unfortunately, the original Java Web Start Launcher logo has been removed from /usr/java/jdk*/jre/javaws/

Java Plug-in installation

This part explains how to install Java plug-in for most new HTML browsers, including the Netscape 7.1 browser and the Mozilla 1.4 browser. If you have an older browser, you should read "Java Plug-in installation for older browsers".

Note: For new browsers based on gcc 3.2 one should explicitly choose the plug-in version compiled with gcc32 in the ns610-gcc32 folder. The plug-in in the ns610 folder won't function. The symbolic link created is placed in the folder similar to /usr/*/browser_name/plugins/

Warning: Note that you must create the symbolic link! It doesn't work just trying to copy libjavaplugin_oji.so to the right spot.

Installing for the Netscape 7.1 browser

Open a terminal window, login as the super-user, and make a symbolic link:

[root]# ln -sf /usr/java/j2sdk*/jre/plugin/i386/ns610-gcc32/libjavaplugin_oji.so /usr/local/netscape/plugins/ 

Note: This is a very long one line command! If something looks like return, it should be typed as space.

Installing for the Mozilla 1.4 browser

Open a terminal window, login as the super-user, and make a symbolic link:

[root]# ln -sf  /usr/java/jdk*/jre/plugin/i386/ns7-gcc29/libjavaplugin_oji.so /usr/lib/mozilla-1.4.2/plugins/ 

Note: This is a very long one line command! If something looks like return, it should be typed as space.

Make sure that the Java plug-in is enabled. Click Edit > Preferences... > Advanced > Enable Java.

Check the Mozilla installation by clicking: Help > About Plug-ins. Mozilla should show a message similar to: Java(TM) Plug-in *

Note: Remember to restart Mozilla before checking the Mozilla installation!

What now?

That's it!! Hopefully you now have Java installed on your Linux box, and you may wonder what to do next. Follow the link if you want to compile and run a Java program, or perhaps get started learning Java programming!
See: Sharing Java

Frequently used commands

Open a Terminal Window

To open a terminal window, click Main Menu > System Tools > Terminal.

Note: If you have already opened a terminal window, you don't have to open another one.

Log in as Super-User

To log in as the super-user, type:

[user]$ su Password:  

Note: If you are already logged in as the super-user, you don't have to log in again.

Note: You may log out as super-user by typing Ctrl+D.

Search for Files

Click Main Menu > Search for Files...

Remove Folders

The tutorial does not explain how you should remove folders, because it is very easy to cause a lot of damage.

Warning: The rm -Rf command may delete your entire hard disk if you do anything wrong. You may feel safer by using a graphical file manager. Make backup of important files before you remove any folders.

Troubleshooting

Can't print the product version

Description: Java gives an error message when I type:

[user]$ java --version 

Solution: The option --version contains a typo! You should write -version. Always read the error messages carefully. Usually, it's not too hard to find out what Java complains about:

Unrecognized option: --version Could not create the Java virtual machine. 

Can't unpack files

Description: The file j2sdk-*-linux-i586-rpm.bin does not unpack correctly.

Solution: You may have download problems. Check the file size with ls -l and compare with the file size written on the download page.

Can't run Java applications

Description: Walter 4 told me that a Java application will not run, because there are two or more versions of the package 'j2sdk' installed in your system, and that the application can't decide which version to use.

Solution: The problem could be that you have installed two versions of Java SDK. Try to uninstall the old package with the rpm -e command.

See: www.rpm.org homepage - RPM Package Manager

Can't run new Java version

Description: You may only run an old Java version, and not the new version you just installed.

Solution: The problem could be the jdkgcj package.

See: This tutorial how to deal with the problem.

Description: You may still only run an old Java version, and not the new version you just installed.

Solution: See answer above. If you need both versions, you should probably start Java by specifying the whole part, such as /usr/java/j2sdk*/bin/java. If you find a better solution, please send me an e-mail.

/usr/java/j2sdk*/bin is in the path twice

Description: /usr/java/j2sdk*/bin is in the path twice.

Solution: I've heard that "GNOME" or "X Window" or something runs /etc/profile even if it has been run already. If you have a better explanation or know how to get rid of the problem, please send me an e-mail.

File System Preferences Error

Description: If you try to run a Swing (GUI) application with Java 1.4.1 or newer, you will get an error.

Solution: The first time you run a Swing application, run it from super-user.

FUD Errors

Description: The network is the computer, and because Java is a network language it won't run on Linux because of firewall problems.

Solution: Don't panic! Try to overcome your personal Fear, Uncertainty, and Doubt (FUD), and the problem is going to magically disappear in a puff of smoke!! This works most of the time, because a whole lot of problems simply does not exist at all, or turns out to be much simpler problems when you just relax a little.

The Far Side of Java Installation

This chapter describes things that you would not normally do, but if you really want to do something unusual anyway, this is how to do it...!! Hey, what do I know??

/etc/profile

If you, for some reason, decided to edit /etc/profile instead of /etc/profile.d/java.sh, follow this procedure:

Warning: Be careful when changing /etc/profile. You may not be able to run Linux afterwards (for instance, if you forget to type $ in $PATH). In this case, use the Red Hat Linux CD-ROM with boot: linux rescue.

Edit profile with the gedit editor so that the Netscape browser Java plug-in and java and javac commands works for all users:

[root]# gedit /etc/profile 
PATH="$PATH:/usr/java/j2sdk*/bin/" export NPX_PLUGIN_PATH=/usr/java/j2sdk*/jre/plugin/i386/ns4/ 

Put the above lines in front of:

export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE INPUTRC 

The code should now look like:

PATH="$PATH:/usr/java/j2sdk*/bin/" export NPX_PLUGIN_PATH=/usr/java/j2sdk*/jre/plugin/i386/ns4/ export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE INPUTRC 

Red Hat Linux 8.0 Note

Editing the profile under Red Hat 8.0 requires a different approach. If you are forced under torture to use Java with Red Hat Linux 8.0, this is how to do it:

pathmunge /usr/java/j2sdk*/bin after 

Put the above line before:

pathmunge /usr/X11R6/bin after 

The code should now look like:

pathmunge /usr/java/j2sdk*/bin after pathmunge /usr/X11R6/bin after 

Finishing touches:

export NPX_PLUGIN_PATH=/usr/java/j2sdk*/jre/plugin/i386/ns4/ 

Put the above lines in front of:

export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE INPUTRC 

The code should now look like:

export NPX_PLUGIN_PATH=/usr/java/j2sdk*/jre/plugin/i386/ns4/ export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE INPUTRC 

Java Plug-in installation for older browsers

This part of the tutorial describes how to install the Java plug-in for older browsers.

Installing for Netscape 4.x browsers

This is how to install the Java plug-in for older Netscape 4.x browsers. Open a terminal window, login as the super-user, and edit the java.sh file in the/etc/profile.d/ folder:

[root]# gedit /etc/profile.d/java.sh 

The file should look similar to the two lines below:

export NPX_PLUGIN_PATH=/usr/java/j2sdk*/jre/plugin/i386/ns4/ export PATH=$PATH:/usr/java/j2sdk*/bin/ 

Warning: Be careful about typos when editing /etc/profile.d/java.sh. You may not be able to run Linux afterwards (for instance, if you forget to type $in $PATH). In this case, use the Red Hat Linux CD-ROM with boot: linux rescue and fix the typo.

Make sure that the Java plug-in is enabled. Click Edit.Preferences... > Enable Java > Enable Java Plugin.

Installing for Mozilla 1.2.x browsers

This is how to install the Java plug-in for older Mozilla 1.2.x browsers. Open a terminal window, login as the super-user, and make a symbolic link:

[root]# ln -sf /usr/java/j2sdk*/jre/plugin/i386/ns610/libjavaplugin_oji.so /usr/lib/mozilla/plugins/ 

Note: This is a very long one line command! If something looks like return, it should be typed as space.

Note: You should explicitly choose the plug-in in the ns610 folder for older browsers, and not the plug-in in the ns610-gcc32 folder.

Note: The Mozilla folder/directory is named "mozilla-1.0.1" under Red Hat 8.0. However, it is called mozilla under Red Hat 7.2 and 9!

Make sure that the Java plug-in is enabled. Click Edit > Preferences... > Advanced > Enable Java.

Check the Mozilla installation by clicking: Help > About Plug-ins. Mozilla should show a message similar to: Java(TM) Plug-in *

Glossary

Alpha Release

An alpha release is an incomplete program that includes most major features. An alpha release is used for fixing bugs and bad features.

API (Application Programming Interface)

The Java API is a library of Java packages each containing related classes.

Beta Release

A beta release is a complete program, but not a stable or finished product.

JavaScript

Scripting language from Netscape, formerly known as LiveScript. JavaScript and Java are different languages. Netscape renamed LiveScript to JavaScript in order to confuse developers.

J2 (Java 2 Platform)

Sun Microsystems cunningly coined the 1.2.2 and later releases as the Java 2 Platform in order to confuse everyone. See: http://java.sun.com/products/archive/

J2SE with NetBeans IDE Cobundle

Also known as J2SE with Sun ONE Studio, Community Edition.

JDK (Java Development Kit)

Also known as SDK. Sun Microsystems cunningly renamed JDK to SDK in order to confuse everyone.

JRE (Java Runtime Environment)

The JRE is included in the SDK.

RC (Release Candidate)

A Release Candidate is very close to the launch version, and is meant to be a complete version in its final form. A Release Candidate is used for widespread testing, just in case there are any remaining bugs.

SDK (Software Development Kit)

Formerly known as JDK. The SDK contains development tools & APIs and the JRE.

SE (Standard Edition)

JRE or SDK

No comments:

Post a Comment