justlinux.com
Thu, 18-Mar-2010 04:18:23 GMT
internet.com
Forum: Registered Users: 73706, Online: 101
nhfs Here you can view your subscribed threads, work with private messages and edit your profile and preferences Registration is free! Calendar Find other members Frequently Asked Questions Search Home Home

Help File Library: Installing Java on a Linux Computer


Written By: Greg Brouelette

1. What is Java?
2. What makes Java so cool?
3. Where can I get Java for Linux?
4. How do I set up Java on my computer?
5. Where can I learn more about Java?

1. What is Java?

Java is a computer language developed by Sun Microsystems and it's spinoff company JavaSoft. It is similar to C++ in many ways, however, many people think that Java is easier to understand and to learn than C++.

2. What makes Java so cool?

Well, there are many things that makes Java a cool language to write code in. But the primary reason is something called "WORA" or Write Once Run Anywhere.

WORA allows you to write a program that will run (without re-compiling) on a Linux computer, a Windows 95, 98 NT or 2000 computer, a Mac, or a computer with Sun OS.
How does it do that you may ask?

As we all know, computers run on ones and zeros. The problem is that the set of ones and zeros that means "Add the numbers 4 and 5" on a PC might mean "Print the letter 'Z'" on a Mac. (It doesn't but this is just a hypothetical example). In any case, programs written on one platform won't run on a different platform.

The problem gets worse when you consider the problem of GUI (Graphical User
Interface) programming. The Windows GUI library, MFC (Microsoft Foundation
Classes) is not supported on any other platform. So any program that uses dialog boxes, windows, buttons, checkboxes, or any of the other gui components we're use to seeing in our software will work only on the platform for which it was written (until now).

Java has a set of gui components, called "Swing", that is core to the language. That's right, the entire gui library is part of the core language. This means you can write programs with a gui that will run on any platform that supports Java.

"OK, That's great!" you say, "But how can Java possibly work on all these different platforms?"

Java source code is written as ASCII text and saved as a .java file. When you run the compiler ( javac ) on a .java file you get one or more .class files.
Normally, a compiler would compile into machine language that is specific to a particular computer platform. But with Java it compiles as far as it can without having to know what kind of computer it's going to run on.

When you run a java program you run it inside a Java Virtual Machine (JVM). This Java Virtual machine knows how to take the .class files and make them run on a particular computer platform. In other workds, there is a JVM for Windows, a different JVM for Linux, a different JVM for Mac OS, and so on.

By installing the correct JVM for your computer you can run any Java program that was properly compiled to a .class file.

3. Where can I get Java for Linux?

The Java Development Kit (JDK) for Linux is available from www.blackdown.org and www.javasoft.com At the time that this FAQ was written the latest version was jdk 1.2.2

4. How do I set up Java on my computer?

Here's how "I" do it. Your mileage may vary.

In my home directory I create a new directory called "devtools". This is where I put all my developer's tools. You may want to put Java in /usr/local so all your users can have access to it easily. [Addition 2-17-2000 the /usr/local is probably the better choice, just make sure that you chmod the directories so your users will have access to the developer tools]

The file I downloaded was jdk1_2_2rc1-linux-i386.tar.gz Your file might have a slightly different name depending on the version number and what kind of system you're using. (i386 is for PC's).

The file is both gzipped and tar'd. I put the file in my ~/devtools directory and decompressed it by typing:

tar -xvzf jdk1_2_2rc1-linux-i386.tar.gz

The 'z' option will automatically filter the tar file through gunzip.

You will now have a new directory under devtools called jdk1.2.2. Under that is a directory called bin. Put this directory in your path. I'm using a bash shell but this example should be similar to what you need to do if you're using a different shell.

Open up your ~/.bashrc file and add the following lines.

export JAVA_HOME=~/devtools/jdk1.2.2
export PATH=$JAVA_HOME/bin:$PATH

[Addition 2-17-2000 It's probably better to put PATH, JAVA_HOME and CLASSPATH environment variables in your .bash_profile file]

You'll add one more line in a minute, but this will be fine for now.

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.


internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers
internet.commerce
Be a Commerce Partner












The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers