Java Online Tutorial
Your first Java Applet and Applications
GETTING STARTED WITH JAVA
Setting up the Java Development Kit
So you want to learn how to program in Java. No doubt
you have heard all about the wonders of Java and you
wish to know what the hype is all about. This guide
will steer you through the basics of the programming
language in an easily comprehensible, hands-on manner.
So without wasting any more time let us jump into getting
started in real earnest.
The first step is, of course, to procure the Java Development
Kit, distributed free by sun. You can get one off the
web at java.sun.com or Sunsite
Thailand . After clicking on the self-executable
file appropriate to your system you will have set up
the java environment. However, it is always better to
check whether you have indeed set it up correctly. This
guide only contains information specific to Windows.
There are two environment variables that need to be set -
the path and the classpath. Both can be
set up through the autoexec.bat file. Look for
a line beginning with...
set path=c:\windows;c:\windows\command;...
This is the path environment variable and is very important. It
should also contain the full path to the bin
sub-directory of your java installation. If you installed
jdk1.3 the path looks like this -- c:\jdk1.3\bin. If
your autoexec.bat file doesn't contain this line - add
it manually - now. Edit the autoexec.bat file with your
favourite text editor so that the following line appears
in it.
set path=c:\jdk1.3\bin;c:\windows;c:\windows\command\;
This line may also contain other entries. It is actually a set of
semi-colon delimited paths pointing to directories and
sub-directories in your system.
The next environment variable is the classpath and is equally important.
The JDK contains a number of files (jar files) in the
lib and jre/lib subdirectories which must appear in
the classpath for your java programs to function correctly.
(Jar is a command line utility that comes with the JDK.
Go to a dos-window and type jar to get usage and help
parameters.)These are
c:\jdk1.3\lib\tools.jar
c:\jdk1.3\lib\dt.jar
c:\jdk1.3\jre\lib\rt.jar
c:\jdk1.3\jre\lib\jaws.jar
c:\jdk1.3\jre\lib\i18n.jar
A good idea is to add all these files to the classpath. So make
the following changes to your autoexec.bat file.
set classpath=c:\jdk1.3\lib\tools.jar;c:\jdk1.3\lib\dt.jar;
c:\jdk1.3\jre\lib\rt.jar;c:\jdk1.3\jre\lib\jaws.jar;
c:\jdk1.3\jre\lib\i18n.jar
Make sure it all appears in a single line. This line can contain
other paths as well. A common practice is to add the
directory which holds your work to the classpath environment
variable. See troubleshooting.
There - you have completed the most difficult part
of the java installation. Now lets get on with writing
your first program.
Back
to TOP
Your first java application
Copy and paste the following code into a text editor and save it as
HelloWorld.java
public class
HelloWorld {
public static void main(String [] args){
System.out.println("Hello World");
}
}
Java is case-sensitive which means that "helloworld" is not the same
as "HelloWorld". Be careful when you type
in the code. Once you have saved this file as HelloWorld.java
you have to invoke the java compiler to compile it.
The java compiler is javac which resides in your
bin directory of your java installation and it is invoked
as follows from a dos window.
javac Helloworld.java
If all goes well you will end up with a file called HelloWorld.class
in the same directory. To run your application you have
to invoke the java interpreter on this file (the .class
file).That is accomplished as the following line shows
how.
java HelloWorld
That's it! Your program should now produce the following output:
Hello World.
If you face problems you might want to see the section
under troubleshooting.
Note that the java compiler (javac) and java interpreter
(java) are two different things but both may be invoked
from the command-line.What you developed just now was
a bare-bones stand-alone java application. Java programs
can be stand-alone applications or applets or both.
Applets run inside a browser like Netscape Navigator
or Internet Explorer. By contrast, java applications
are run from the command-line. Now that you have written
your first java spplication, let us move ahead to your
first java applet.
Back to TOP
Your first java applet
Typically, the only extra component here is an html file which
invokes the applet. You write the java source file and
compile it using javac just like in the above
example. The difference is applets cannot be invoked
by the java interpreter. They have to be invoked by
a java-enabled browser such as Netscape Navigator and
Internet Explorer. You can also use the appletviewer
shipped with the JDK as it is more reliable than these
browsers.
Copy the following code exactly appears into a text editor and save it
giving the same name as the class, in this case "HelloWorldApplet.java"
import java.applet.Applet;
import java.awt.Graphics;
public class HelloWorldApplet extends Applet {
public void paint(Graphics g) {
g.drawString("Hello World !", 5,20);
}
}
We'll take closer look at the code we've just written when we
deal with applets in the next section.For the moment,
suffice to say that applets contain any of the methods
init(), start(), or paint() or all of the above.
So lets concentrate on getting this example up and running.
Assuming you have saved the file as "HelloWorldApplet.java"
and have made no mistakes in the code - like writing
"applet" instead of "Applet", "drawstring"
instead of "drawString", you are now ready
to compile it. Do so immediately. The following line
shows you how.
javac HelloWorldApplet.java
If the file compiles correctly you should end up with a file called
HelloWorldApplet.class in the same directory. Now the
next step is to invoke it using the browser or the appletviewer
and for that we need a html file.
Copy and save the follwing html in the directory which contains your
class file. Save it as applet.html though you can give
any name to this file, as long as it has the .html or
.htm extension.
<HTML>
<HEAD>
<TITLE>Hello to Everyone!</TITLE>
</HEAD><BODY>
<P>My Java applet says:
<APPLET CODE="HelloWorldApplet"
WIDTH=150 HEIGHT=25>
</APPLET>
</BODY>
</HTML>
* * You can see a sample
of it here
in action.
If you'd like to use the appletviewer to view the applet you can do
so issuing the following command in your dos-window.
appletviewer applet.html
However if you run into problems you might want to
see the next section on troubleshooting.
Back to TOP
Troubleshooting
This section lists the common problems faced by newbie java programmers
and how to fix them.
- Bad command or filename or Command not found
These errors result when you do not have the
JDK's bin directory in your execution path,
or the path to that directory is wrong. On Windows,
double-check your autoexec.bat file. You
might want to re-read the section on setting
up the JDK.
- javac: invalid argument
Make sure the name of the file you're giving
to the javac command is exactly the same
name as the file. In particular, in the DOS shell
you want to use the Windows filename with a .java
extension, not the DOS equivalent (HELLOW~1.jav,
for example).
- Warning: public class HelloWorldApplet must be defined in a
file called HelloWorldApplet.java
This error most often happens if there is a mismatch
between the name of the class as defined in the Java
file itself (the name following the word class) and
the name of the java source file. Both the
filenames must match, including upper- and lowercase
letters (this particular error implies that the filename
had lowercase letters). Rename either the filename
or the class name, and this error will go away.
- Exception in thread "main" java.lang.NoClassDefFoundError:
HelloWorld
You might argue that your class indeed
contains a main method. This error is because the
java interpreter cannot find your class file. Run
the java interpreter specifying the classpath flag
like this:
java -classpath "%classpath%;.;" HelloWorld
Now it should work.
Back to TOP
Now on to the next chapter Animation
Techniques
All questions and comments can be addressed to the
author.
All material appearing within this website is copyright
protected and may not be reproduced elsewhere without
the express written permission of the author (Sanjeev
Dasgupta)
|