How do I install Java on my Linux Computer?
Written By: Greg Brouelette
[-Previous Page-]
Close your editor and type ENV to see if the JAVA_HOME and new Path has
taken effect (if you're in a gui like KDE or GNOME you may have to close
and restart your terminal window for these changes to take effect)
Lets test it. Type the word "java" (without the quotes) in a terminal
window and see if you get something like this:
Usage: java [-options] class [args...]
and so on.
If you get the message
java:command not found
then your path is not correct. Essentially the java and javac programs are
in that bin directory and you want to get access to them.
The JAVA_HOME environment variable is used by other programs that need to
know where your jdk is (like Star Office for example).
There is one more change you'll need to make. For Java to work it needs to
know where the class files are for any program that you're writing.
For our example lets make a directory under our home directory called
myjava. Do this
by typing:
mkdir myjava
This is where our example programs are going to be. Now we need to tell
Java where our programs will be by adding the following line to our
.bashrc ( or .bash_profile ) file in exactly the same way we added the
JAVA_HOME and PATH lines earlier.
export CLASSPATH=.:~/myjava
(Remember, you may need to close and reopen your terminal window to make
this change take effect).
Let's test it! Let's write a "Hello World" program in Java and make it
work.
Go to your myjava directory and write the following program saving it as
Hello.java
import java.awt.*;
public class Hello {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
Be sure that the name of the class and the filename are exactly the same
(including capitalization). In other words if you write a class called
DoThisWeirdThing it MUST be saved in a file called DoThisWeirdThing.java
Back at your command line type:
javac Hello.java
If you typed everything correctly you'll get a prompt again. Type ls to
see the contents of your directory and you should see a Hello.class file.
At your command line type:
java Hello
and you should see the words "Hello World" print out.
If you can make this work then you've got Java installed properly. (That
wasn't too hard was it?)
5. Where can I learn more about Java.
I suggest you download the free HTML documentation from www.javasoft.com.
It has the entire language referenced via an easy to use browser interface. I
always keep this file up in a browser while I program as a quick reference.
I'd also like to suggest the book "Thinking In Java" by Bruce Eckel. I
like this book for 2 reasons: One, it's pretty much the best Java book
I've found and Two, the entire book is available for free online at
www.bruceeckel.com (Although it would be cheaper to buy it than to use up
an entire laser printer cartridge printing it out).
All the source code examples in Bruce's book are also available at his web
site.
Another good source of Java knowledge is the Java Developers Connection
(JDC) which is a free forum that you can join at www.javasoft.com. There
are quite a few Java programmers online that can answer your questions.
I hope this helps you install Java on your Linux computer. Please post any
questions on the LinuxNewbie discussion board and I'll try to answer them
A.S.A.P.
Good Luck!
Greg Brouelette A.K.A. Dru Lee Parsec
2-17-00 ADDITIONAL INFORMATION:
I've received e-mail from several folks saying that they get an error
telling them that some shared files are not found when they try to use
java or javac. It turns out that this usually means that the glibc
libraries are not the latest version. I've not had the problem when
installing Mandrake 6.1 but other folks have told me that this has fixed
their problems.
|