Saturday 17 December 2011

HOW TO INSTALL JAVA ON YOUR PC

How to install the JDK


Microsoft Windows 95

Installing the JDK

  1. Run the setup program jdk-1_5_0-windows-i586.exe
    Running the setup program.
  2. Click "Continue" on the warning message.
    Warning message.
  3. Click "Accept Terms in License Agreement" then click on the Next button.
    Licence Agreement.
  4. Select the components to install. If you are low on free space you can leave out the Demos and Source Code.
    Components to Install
  5. The wizard will now install the JDK files on your system. This may take a while.
    Installing the files
  6. Click the Next button to install the J2SE Runtime Environment
    Install the Java Runtime Environment
  7. In the next dialog select the browsers to integrate the JRE with, I recommend all. Click the Next button.
  8. The installation should be complete! Click the Finish button.
    Installation complete

Setting the Path Variable

We need to set the path variable so that the system can find the Java compiler.
  1. Open the Windows Explorer and navigate to the C:\ drive. Right-click on the file named Autoexec.bat and select edit from the menu.
    Edit the autoexec.bat file
    Warning
    Autoexect.bat may not be visible if it is set as a hidden file. You should set folder options to display hidden files by selecting Options from the Folder menu.
  2. The file may be empty, but it will most likely contain some entries. You should add the following entries at the bottom of the file, after the last entries:
    set JAVA_HOME=C:\Program Files\Java\jdk1.5.0
    set PATH="%PATH%;%JAVA_HOME%\bin"
    Note
    If you changed the installation location of java to some other location, you will have to change the location here to.
  3. Save the file, close the text editor and restart your computer.

Microsoft Windows 98

Installing the JDK

  1. Run the setup program jdk-1_5_0-windows-i586.exe
    Running the setup program.
  2. If you use Windows 98 First Edition and get the warning message, click "Continue" on the warning message.
    Warning message.
  3. Click "Accept Terms in License Agreement" then click on the Next button.
    Licence Agreement.
  4. Select the components to install. If you are low on free space you can leave out the Demos and Source Code.
    Components to Install
  5. The wizard will now install the JDK files on your system. This may take a while.
    Installing the files
  6. Click the Next button to install the J2SE Runtime Environment
    Install the Java Runtime Environment
  7. In the next dialog select the browsers to integrate the JRE with, I recommend all. Click the Next button.
  8. The installation should be complete! Click the Finish button.
    Installation complete

Setting the Path Variable

We need to set the path variable so that the system can find the Java compiler.
  1. Open the Windows Explorer and navigate to the C:\ drive. Right-click on the file named Autoexec.bat and select edit from the menu.
    Edit the autoexec.bat file
    Warning
    Autoexect.bat may not be visible if it is set as a hidden file. You should set folder options to display hidden files by selecting Options from the Folder menu.
  2. The file may be empty, but it will most likely contain some entries. You should add the following entries at the bottom of the file, after the last entries:
    set JAVA_HOME=C:\Program Files\Java\jdk1.5.0
    set PATH="%PATH%;%JAVA_HOME%\bin"
    Note
    If you changed the installation location of java to some other location, you will have to change the location here to.
  3. Save the file, close the text editor and restart your computer.

Microsoft Windows 2000

Installing the JDK

Note
You should be logged in as Administrator or have Administrative privileges to perform this installation. If not, you should get your System Administrator to do the install for you.
  1. Run the jdk-1_5_0-windows-i586.exe setup program.
  2. Select Accept with the Terms in the License Agreement and click the Next button.
    Accept the terms in the licence
  3. Select the components you wish to install. If you are low on disk space you can leave out the Demos and Source Code. Click the Next button to continue.
    Select components to install
  4. The installer will now copy files to your computer, this may take a while.
    Copying files to your computer
  5. Install the Public Java Runtime Environment by clicking the Next button. You should not have to edit any settings here.
    Install the runtime environment
  6. Select which browsers to integrate the runtime environment with. I recommend all.
    Select browsers to integrate the runtime with
  7. That should take care of the installation. The JDK is now successfully installed on your system.
    Installation completed.

Setting the Path Variable

We need to set the path variable so that the system can find the Java compiler.
Note
We will create the variables in the global system space so that any user on the system can use the java tools. If you don't want this, you can create the variables in the Administrator's user space, but then only the administrator will be able to use the java tools. This might be what you want if there is only one user on the computer.
  1. Right click on the My Computer icon on the desktop and select Properties from the pop up menu. Click the Environment Variables button.
    My Computer Properties
  2. Under the System Variables section click the New button to add a new variable.
    Environment Variables
  3. Add a variable named JAVA_HOME with the value C:\Program Files\Java\jdk1.5.0 and click the Ok button.
    The Java Home Variable
    Note
    If you installed Java to a different location, you should change the value here to reflect this.
  4. Lastly edit the variable named PATH, add a semi colon (;) after the last entry in the value field and then add C:\Program Files\Java\jdk1.5.0\bin
    The PATH variable

Microsoft Windows XP

Installing the JDK

Note
You should be logged in as Administrator or have Administrative privileges to perform this installation. If not, you should get your System Administrator to do the install for you.
  1. Run the jdk-1_5_0-windows-i586.exe setup program.
    Run the setup program
  2. Select Accept with the Terms in the License Agreement and click the Next button.
    Accept the terms in the licence
  3. Select the components you wish to install. If you are low on disk space you can leave out the Demos and Source Code. Click the Next button to continue.
    Select components to install
  4. The installer will now copy files to your computer, this may take a while.
    Copying files to your computer
  5. Install the Public Java Runtime Environment by clicking the Next button. You should not have to edit any settings here.
    Install the runtime environment
  6. Select which browsers to integrate the runtime environment with. I recommend all.
    Select browsers to integrate the runtime with
  7. That should take care of the installation. The JDK is now successfully installed on your system.
    Installation completed.

Setting the Path Variable

We need to set the path variable so that the system can find the Java compiler.
Note
We will create the variables in the global system space so that any user on the system can use the java tools. If you don't want this, you can create the variables in the Administrator's user space, but then only the administrator will be able to use the java tools. This might be what you want if there is only one user on the computer.
  1. Right click on the My Computer icon on the desktop and select Properties from the pop up menu. Click the Environment Variables button.
    My Computer Properties
  2. Under the System Variables section click the New button to add a new variable.
    Environment Variables
  3. Add a variable named JAVA_HOME with the value C:\Program Files\Java\jdk1.5.0 and click the Ok button.
    The Java Home Variable
    Note
    If you installed Java to a different location, you should change the value here to reflect this.
  4. Lastly edit the variable named PATH, add a semi colon (;) after the last entry in the value field and then add C:\Program Files\Java\jdk1.5.0\bin
    The PATH variable

Linux (Red Hat, Fedora Core & Mandrake)

Installing the JDK

Note
This how-to assumes you have root privileges and that you are to install Java system wide so that all users can use the Java tools.
  1. Start a shell. Copy the Linux installer to the /usr/local directory.
    cp jdk-1_5_0-linux-i586.bin /usr/local
  2. Now change into the /usr/local directory and run the installer.
    cd /usr/local
    chmod +x jdk-1_5_0-linux-i586.bin
    ./jdk-1_5_0-linux-i586.bin
  3. Read through the license agreement and if you agree type yes. This will start the installer.
  4. If everything goes well you should end up with output that ends with:
    Done.

Setting the Path Variable

  1. Create the file named java.sh in the directory named /etc/profile.d using your favourite text editor. I used Vi.
    cd /
    touch /etc/profile.d/java.sh
    vi /etc/profile.d/java.sh
  2. Add the following to the file.
    export JAVA_HOME=/usr/local/jdk1.5.0
    export PATH=${PATH}:${JAVA_HOME}/bin
  3. Make the script executable.
    chmod +x /etc/profile.d/java.sh
  4. Log out and then log in again for the changes to take effect.

Sun Solaris

The JDK should be installed and setup by default. For information on upgrading go to Sun's Website and browse through the Solaris documentation.

MacOS

Most newer versions of MacOS have the JDK installed and setup by default. For information on upgrading check out the MacOS documentation.

Compiling and Running Java Programs

Compiling Java Programs

We will now test our installation by compiling and running a simple Java Program.

Microsoft Windows

  1. Open the Notepad text editor Start > All Programs > Accessories > Notepad and type the following program into it:
    class HelloWorld {
        public static void main(String[] args) {
            System.out.println("Hello World!");
        }
    }
  2. Save the program to the C:\ drive in a file named HelloWorld.java. Make sure to select "All Files" in the Type field of the Save As dialog box.
  3. To compile the program, open the Dos Prompt Start > All Programs > Accessories > Command Prompt and type:
    cd\
    javac HelloWorld.java
    If you don't get any errors then everything went well. If you get errors, check firstly that you saved the program on the C:\ drive and that it is named HelloWorld.java.

Linux

  1. Start your favourite text editor and type the following program in it, then save it in a file named HelloWorld.java in your home directory.
    class HelloWorld {
        public static void main(String[] args) {
            System.out.println("Hello World!");
        }
    }
  2. To compile the program, start a shell and use the Java Compiler:
    cd ~
    javac HelloWorld.java
    If you don't get any errors then everything went well. If you get errors, check firstly that you saved the program in your home directory and that it is named HelloWorld.java.

Solaris

  1. Start a text editor, either vi in the command line or Gedit. Type the following program in it then save it in a file name HelloWorld.java in your home directory.
    class HelloWorld {
        public static void main(String[] args) {
            System.out.println("Hello World!");
        }
    }
  2. To compile the program, start a shell and use the Java Compiler:
    cd ~
    javac HelloWorld.java
    If you don't get any errors then everything went well. If you get errors, check firstly that you saved the program in your home directory and that it is named HelloWorld.java.

Running Java Programs

  1. To run the program, type:
    java HelloWorld
    you should get the following output:
    Hello World!

Common Errors and Solutions

  1. Exception in thread "main" java.lang.NoSuchMethodError: main You must define a main method as: public static void main(String[] args) {...}
  2. HelloWorld.java:3: illegal character: \8220 System.out.println(öHelloWorld!ö); Do not type your code in a word processor application that uses smart quotes!!
  3. HelloWorld.class Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld/class The correct way to call a program is java HelloWorld without the ".java" or ".class"

Valuable Resources

The Java Application Programmer Interface

The Java API is an Application Programmer Interface that describes in great detail the core Java Foundation Classes that make up the Java Platform. It highlights which packages make up the Platform, which classes belong to which packages and what methods and fields are available in each class. It is an invaluable tool to any Java Programmer.
The Java API

Sun's Java Page

Sun Microsystems's Java page can be accessed via the Internet at http://java.sun.com and it provides a download page where you can download all of the Java Technology that you want. It includes documentation: API's, Tutorials, Short Courses and White Papers. It often has industry news and updates and is also an invaluable resource to any Java programmer.
Sun't Java Page

No comments:

Post a Comment