Introduction
to Java Programming
Online Tutorials - Java by Forum member "turnitonagain"
©Technology Vault.
Introduction
The
purpose of this series of tutorials is to try
and teach the basics of programming using the
Sun Microsystems language Java. Hopefully these
will suit people who have never done programming
before and those who have done a bit, but just
never programmed in Java before. Whatever level
you are at, please get in touch and let me know
what you think!
Right,
lets get started. Probably the most complex bit
of this first tutorial, is setting up the Java
compiler. So that's what we will do first...
1.
Obtaining the Java compiler
The Java complier can be obtained from http://java.sun.com/j2se/downloads.html
and (just to complicate things...) there are a
few versions on the site. These range from the
latest version - 1.4.1 (37Mb) to version 1.2.2
(20Mb). For novice programmers starting out, you
probably will not notice the difference between
1.2.2 and 1.4.1, so if you have a dial-up connection,
1.2.2 is probably the one to go for. If you feel
you might carry on with this programming lark,
you'd be better biting the bullet and getting
1.4.1 at the outset.
When
you get to the download screen asking which type
you want to download, you want to go for the 'SDK'
version. This stands for 'Software Developer Kit'
and is what you need to compile and run the Java
programs you will be writing. The other one, the
JRE (Java Run-time Environment), is the version
you use purely to run the programs, so ignore
it. I should add that all the Java versions I
have listed above are free.
2.
Installing the compiler
Run the .exe file that you have downloaded and
follow the instructions on screen (remember to
take note of the name of the directory it installs
to - this will vary depending on the version).
The
next step is to allow us to run the compiler from
any directory on the PC (more specifically the
directory where we are going to store our programs).
Open
the system editor by clicking: Start-> Run,
and enter 'sysedit' (without the quotes) in the
Open box and click on Ok. One of the files that
loads in the window will be 'Autoexec.bat' (it
still clings on, even in XP!). Add the following
line at the end:
PATH=%PATH%;<dir>\bin
(Where
<dir> is the drive and directory you installed
Java to). So my PATH looks like:
PATH=%PATH%;C:\j2sdk1.4.1_01\bin
This
is the default directory for version 1.4.1. Once
you are finished save and close Autoexec.bat and
Sysedit and reboot the PC.
3.
Creating a Program
Now, create a directory on one of your drives
called 'javaprogs' or something equally meaningful.
It has to be reasonably accessible since we will
be navigating to it in a DOS prompt, so I have
put on my main C drive (C:\javaprogs).
Ok,
a quick bit of terminology to keep you on your
toes, Java programs can also be called 'classes'
so program and class can be read interchangeably
in this context. Now we are ready to type our
first program! First, open up notepad (Start->Programs->Accessories->Notepad),
now type the following code:
class
Hello {
public static void main(String args[]) {
System.out.println("Hellooooooo!!");
}
}
And
save the file as Hello.java in your javaprogs
directory. Now it's time to 'compile' the class
(compile just means 'put it into a language the
PC can understand more easily', we will discuss
this in more detail later on...).
Open
a command prompt by either:
Start->Run and enter 'command' in the Open
box if you are in Windows 95 or 98 or
Start->Run and enter 'cmd' in the Open box
if you are in Windows 2000/XP
This
will open a DOS command prompt; now navigate to
your javaprogs directory. Mine is on the main
C drive I make sure I am on my C drive by entering:
c:
<return>
Then,
cd
\javaprogs <return>
and
this will take you from the directory you are
currently in to your javaprogs directory.
Now
we can compile it by entering the command:
javac
Hello.java
If
you get an error, then recheck your PATH settings
to make sure it can find the Javac executable.
Also make sure you have copied the program down
exactly as above (case of letters DOES matter,
in the filename too).
If
you get no error messages, then the program compiled
ok! type:
java
Hello
to
run the program. You should get Hellooooooo!!
outputted to screen. If you do get any weird errors
you cannot shift, then either post them on the
HTML and programming board or IM me.
That
is probably a good place to leave it for now.
In the next tutorial, I will point out some key
features of the Hello program and try and explain
why it does what it does.
If
you have any comments about this tutorial (usefulness/clearness/etc.,
even just tell me if you tried it and it worked!)
then please get in touch either in the forums,
or IM me.
Next
Java Tutorial