|
What is Java and why do I need it?
Java is an innovative programming language that has become the language of choice for programs that need to run-on a variety of different computer systems. First of all Java enables you to write small programs called applets. These are programs that you can embed in Internet web pages to provide some intelligence. Being able to embed executable code in a web page introduces a vast range of exciting possibilities. Instead of being a passive presentation of text and graphics, a web page can be interactive in any way that you want. You can include animations, games, interactive transaction processing – the possibilities are almost unlimited.
Some of the programs on this website have been written in Java, so you will need to install the Java runtime environment in order to run them.
Where can I download Java?
You can download the SDK from Sun for a variety of hardware platforms and
operating systems, either directly from the Sun Java web site at http://java.sun.com (for Windows, Solaris, and Linux
operating systems), or from sites that you can link to from there. The SDK we
are going to use is available from http://java.sun.com/j2se/1.4. For instance a version of
the SDK for Mac OS is available from http://devworld.apple.com/java/.
Install Java
If you do not have Java installed already, follow the instructions provided with the download in order to install Java.
Once Java has been installed, you will be able to run Java program by running the java executable that was installed. You may find it convenient to add the location of this file to your operating system's path variable, otherwise you will need to explicitly refer to the absolute location of java.
Setting up the path
Windows 2000/XP users may set their path by right-clicking on 'My Computer' and selecting 'Properties'. Under the 'Advanced' tab, there is a button that allows you to set the 'Environment variables'. Click on this and alter the 'Path' variable so that it also contains the path to the Java executable. For example, if you have installed Java in c:\jdk and your path is currently set to C:\WINDOWS\SYSTEM32, then you would change your path
to read C:\WINDOWS\SYSTEM32;c:\jdk\bin
When you open a new command prompt, it will reflect these changes and allow you to run java programs by typing "java". If you have installed the SDK, then you will also be able to run "javac" to compile stuff.
Windows 95/98/ME users may find that their path variable is stored in a different place. Edit the c:\autoexec.bat file and add the following line at the end: SET PATH=%PATH%;c:\jdk\bin
(This also assumes that you have installed Java in c:\jdk)
Linux, UNIX, Solaris, FreeBSD users must set their PATH variable to point to where the java binaries have been installed. Please refer to your shell documentation if you have trouble doing this. For example, if you use bash as your shell, then you would add the following line to the end of your .bashrc: export PATH=/path/to/java:$PATH
Setting up the classpath
In addition to setting up the path, you also need to tell Java where to find compiled class files at runtime. You will probably want to set the classpath to include at least the current working directory (.)
Eg: SET CLASSPATH=%CLASSPATH%;.
The classpath can also contain other directories that may contain compiled class files. Note that if you are using classes that are contained inside a .jar file, you will need to specify the full path and name of this file in the classpath, rather than just the name of the directory it is contained within.
Compiling a Java Program
Java sourcecode is always stored in files with the extension .java. Once you have created the sourcecode for a program and saved it in a .java file, you need to process the source using a Java compiler. Using the compiler that comes with the JDK, you would make the directory that contains your Java source file the current directory, and then enter the following command:
javac -source 1.4 MyProgram.java
Executing a Java Application
To execute the byte code program in the .class file with the Java interpreter in the SDK, you make the directory containing the .class file current, and enter the command:
java -enableassertions MyProgram
|