This Blog is about learning Java™ Programming. If you're interested in Java™, you’re welcome to post your comments freely!
Sharing Java Home | Table of Contents | Search | Useful links
Tuesday, June 14, 2005
Java can now be installed from software repositories in many Linux distributions. Fedora, etc. now usually ship with OpenJDK, so you could just use your favorite package manager to install it. See: OpenJDK Installation. You may wish to install the Sun JDK instead as outlined in this document. I no longer have the time to update this HOWTO, but this page will still be here for reference, and will not be deleted.
Warning: Performing the procedure outlined in this document is at your own risk. The author provides no guarantees, warranties or any liability with respect to any damages or loss of data or any other harm resulting from performing any of the procedures outlined in this document.
This tutorial explains how to uninstall and install the following on the Linux operating system:
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.
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.
Once upon a time, Red Hat had a Java version installed called gcj and is part of the GNU Compiler Collection (GCC). 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. gcj cunningly uses another program called jdkgcj in order to confuse everyone.
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.
The tutorial does not describe installation of the NetBeans IDE Cobundle for a number of reasons:
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.
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.
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.
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
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
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!
You may delete the
java.sh file in
/etc/profile.d/ if you want to remove J2SE permanently.
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:
Warning: The SDK file names changes frequently! You should always start your search for the latest files at http://java.sun.com/
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)
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
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 and
javac 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_HOME, JAVA_HOME or CLASSPATH) are required to run the SDK. However, some applications may 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_PATH line 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
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
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
New Launcher
Java Web Start
/usr/java/jdk*/jre/javaws/javaws"
/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/
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.
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.
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!
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
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.
To log in as the super-user, type:
[user]$ su Password: <the super-user 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.
Click Main Menu > Search for Files...
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.
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.
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.
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
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.
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.
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.
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.
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??
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
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
This part of the tutorial describes how to install the Java plug-in for older 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.
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 *
An alpha release is an incomplete program that includes most major features. An alpha release is used for fixing bugs and bad features.
The Java API is a library of Java packages each containing related classes.
A beta release is a complete program, but not a stable or finished product.
Scripting language from Netscape, formerly known as LiveScript. JavaScript and Java are different languages. Netscape renamed LiveScript to JavaScript in order to confuse developers.
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/
Also known as J2SE with Sun ONE Studio, Community Edition.
Also known as SDK. Sun Microsystems cunningly renamed JDK to SDK in order to confuse everyone.
The JRE is included in the SDK.
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.
Formerly known as JDK. The SDK contains development tools & APIs and the JRE.
JRE or SDK
/etc/profile.d/java.sh was found on several web pages using Google,
for instance Ed Halley's Linux Roadmap, Installing Java.
Posted by Gaute Lykkenborg
@ 2:53 PM
| Links to this post
| Comments: 6
![]()
John told me how to fix the
"/usr/java/j2sdk*/bin is in the path twice" problem.
I have not tried the solution, so please make a comment if you have!
"Regarding the
You can fix this by using the following java.sh script:
if ! echo $PATH | grep -Eq "(^|:)/usr/java/jdk1.5.0_03/bin($|:)" ; then
export PATH=/usr/java/jdk1.5.0_03/bin:$PATH
fi
This will check that the bin directory isn't already in the path before including it.
Credit for this goes to the people at fedoraforum.org."
By Gaute Lykkenborg, at 3:41 PM
I just discovered a few free Red Hat Linux books at a place called Linux-Books.us. It's a collection of Red Hat books and tutorials available for free download at http://www.linux-books.us/red_hat_enterprise.php. They also got free books for other Linux distributions as well.
By Gaute Lykkenborg, at 11:41 AM
i'm getting the wrong version of java when i type in java -version.. do you think i might have an old version of j2se installed? if so, i can't find the folder where it was intalled. i'm guessing self extracting file was used to install it.
By , at 8:22 AM
gaute, i was trying your script, but it is not working .. would you please post more info about that script .. its funtions and so on ..
By , at 7:15 AM
Thanks a lot. Your instructions worked perfectly for me. I appreciate the clarity of your explanations too. You are an asset to the web!
Peace!
By , at 9:52 PM
If your script ain't working, try to relax a little, check for any obvious errors, check for typos, surf the web for any solution. Other than that, I don't know... Oh! Yes, please tell me if you find out what went wrong, so I can update the HOWTO! Good luck with the installation!
By Gaute Lykkenborg, at 11:24 AM
Copyright © 2000-2008 Gaute Lykkenborg. All rights reserved.
Official Site: http://www.lykkenborg.no/
E-mail: glykkenborg@yahoo.no