New to Java? We'll help you get started with our revised beginner's tutorial, or our free online textbook.


Get the latest Java books
h t t p : / /w w w . j a v a c o f f e e b r e a k . c o m /

Java Coffee Break

Menu



Learning Java

Articles
Author Profiles
Lessons
FAQ's
Books
Newsletter
Tutorials
Talk Java!

Using Java

Applets
JavaBeans
Servlets
Resources
Discuss Java


Looking for Java resources? Check out the Java Coffee Break directory!

Getting started with Java

"A beginner's guide to Java programming"

This tutorial will introduce you to Java programming. You'll compile and run your very own Java application, using Sun's Java Development Kit.

The Java programming language has, within only a few years, become a de facto industry standard for developing portable applications, and interactive dynamic content for web pages. It's extremely easy to pick up Java programming skills, and in this lesson, you'll learn how to write, compile, and run Java applications.

First, though, you'll need to download the Java Development Kit (JDK). You may prefer at a later time to use a third party IDE, such as Visual J++ or Borland JBuilder, but it is important to become familiar with the basics of Java first.

Tip You should always use third party tools in combination with Sun's JDK, not exclusion, to ensure compatibility.

Installing the JDK

Sun offers many versions of its JDK, for different Java versions and platforms. You are advised to use the most recent JDK, to gain access to all of the functionality of the latest version of the Java platform. If you don't already have the JDK installed, then stop off at Sun's official Java site, http://java.sun.com/. There you'll be able to download the JDK for Windows & Solaris.

The installation process is fairly straightforward, but you should consult the documentation if you encounter problems. You'll need to make sure that the JDK is installed correctly, and that the JDK tools are within your operating system's path, before proceeding.

Setting up environmental variables for JDK

Under Windows, you set environmental variables by modifying the autoexec.bat file, located in the root directory of your hard-drive. This batch file is executed on startup; modifications to this file will not take effect until you run it, or reboot.

Tip Remember to reboot! I can't stress this enough. Many readers make the changes, forget to reboot, and wonder why JDK doesn't work properly.

Modifying autoexec.bat

You'll need to edit the autoexec.bat file with a simple text editor. Open it up, and add a path statement pointing to the /bin/ directory under your JDK installation directory. 

Tip Use a simple text editor, like notepad or the DOS command 'edit'.

For example, if I'd installed my JDK to c:\jdk1.3\, I'd want to point the path statement to c:\jdk1.3\bin\. However, you must also be careful not to overwrite existing path statements set for other software packages. To prevent this, use the following statement (modifying directory for your installation location)
set path=%path%;c:\jdk1.3\bin\

Next, you need to set the classpath to include the current directory. The classpath helps JDK locate Java software (class files) that are installed on your machine. Some versions of JDK don't require this, but some of the tools that ship with JDK do require it, so it's best to set this environmental variable up properly.

set classpath=%classpath%;.\

Tip Sometimes, you may run out of environmental space, and see the message "Too many parameters" on boot up. If this happens, add the following line to config.sys (located in the root directory)
shell=c:\command.com /p /e:32000

That done, reboot and you'll be ready to start writing your first Java application.

Writing your first Java application

Like any other language, Java programming starts with writing source code. Source code are instructions that will be followed by the computer. We can create source code using any standard text editor, such as 'notepad' on Windows, or 'vi' on Unix. All Java source code ends in a '.java' extension.

Our first application will be extremely simple - the obligatory "Hello World". If you've never heard of "Hello World" before, its a common programming exercise when learning a new language. This example will display the words "Hello World" when executed.

class myfirstjavaprog
{  
   public static void main(String args[])
   {
      System.out.println("Hello World");
   }
}
Code : myfirstjavaprog.java

You should type this code out, using your preferred text editor, and save it as 'myfirstjavaprog.java'. Place this file in a directory (for example, "c:\src\") of its own, not your Java installation directory. Its best to keep your tools and source code separate.

Tip Java compilers expect the filename to match the class name. Thus the class, myfirstjavaprog, must be saved as myfirstjavaprog.java. If you're using notepad, you'll need to rename if from myfirstjavaprog.java.txt back to myfirstjavaprog.java

Compiling your first Java application

While some languages are interpreted (such as PERL), others introduce an extra step, called compilation. Compilation takes source code (files ending in a .java extension), and converts it into byte-code. This byte-code is low level machine code, which is executed inside a Java Virtual Machine (JVM).

To compile Java code, we need to use the 'javac' tool. This ships as part of the JDK, so if you have it installed and in the current path, you can compile your application using the following command from any DOS prompt or Unix session :

javac myfirstjavaprog.java

This will produce a Java class file, called myfirstjavaprog.class. Once your program is in this form, its ready to run. Check to see that a class file has been created. If not, or you receive an error message, check for typographical errors in your source code.

Running your first Java application

Now the time has come, (said the Walrus). You're ready to run your first Java application. From a dos prompt or Unix session, enter the directory where your source code and class file is stored, and type the following :

java myfirstjavaprog
Tip You should not type java myfirstjavaprog.class.  All Java classes end in this extension, it is not necessary, and will cause an error messages.

Your Java application will load, display its message, and then terminate. Congratulations!

Summary

Writing and developing Java applications and applets need not be a great mystery. Armed with the JDK and a text editor, virtually any programmer can write in Java. You've just taken the first step, in a long journey, towards learning Java.

Test your knowledge and take our Java quiz!

Back to main


Copyright 1998, 1999, 2000 David Reilly

Privacy | Legal | Linking | Advertise!

Last updated: Monday, June 05, 2006