Tutorial
Menus

Java Beginners - Java For Beginners

First Java Program -HelloWorld.java

Rating: 5.0/5 (4 votes cast)

Level   : Beginners
Author : Arunkumar S
Download Source : Not Avaliable

Writing a Hello World! program is the traditional first step taken when learning a new language. It allows you to take the new language and its development tools through a test-drive, stepping through the complete install, edit, compile, and run cycle without getting bogged down in language details (plenty of that later). In your favorite editor, enter the following code and save it in a file named HelloWorld.java. Also, I will assume you are working on a Microsoft Windows PC and that you save HelloWorld.java into the c:\source directory.



/**
* The HelloWorld class is an application that
* displays "Hello World!" to the standard output.
*/
public class HelloWorld {

// Display "Hello World!"
public static void main(String args[]) {
System.out.println("Hello World!");
}
}



Open a DOS or command window and change to the directory where you saved HelloWorld.java.

   C:\>cd c:\source

The Java tool set consists of a set of command line programs located in the bin directory of your java installation. For instance, if your Java installation is in C:\j2sdk1.4.2 then the programs you need will be located in C:\j2sdk1.4.2\bin. Make sure you have modified your PATH environment variable to include this bin directory. You can modify your PATH through the control panel or temporarily on the DOS command line (see your operating system help). You should test the Java command before making any PATH changes. The following command should output version information. If it throws an error, set your PATH before continuing.

   C:\source>java -version  

The "javac" command is used to compile .java files into intermediate bytecode files known as class files. Class files have a .class extension. The "java" command instantiates a JVM (Java   Virtual Machine) instance and loads a Java class file that has a main method. The main method is the first code that is executed in your program.

Now compile HelloWorld.java into Java bytecode with the javac command.

   C:\source>javac HelloWorld.java  

If you do a dir command and see "HelloWorld.class" in the directory, then your compilation was successful.

Since your current working directory is the root of your source tree (the .java files are in your current directory), you won't have to tell the javac command where to find the source. Also, in this simple case you will compile the .class file into the same directory as the source. Normally, you separate out the source files from the class files to make managing your source code easier.

Now start the JVM and and tell it what Java class contains the "public static void main (String args[])" method that it must execute.

   C:\source>java -cp . HelloWorld  

The "-cp ." is a flag that tells the Java command where to look for class files. There are two ways to declare class locations to the JVM. If you have a CLASSPATH environment variable set, then the classes you want to load must be declared in the CLASSPATH variable or declared with the "-cp" flag. If you do not have a CLASSPATH variable set, then your classes must exist in the current working folder or be declared with the "-cp" flag.

The following image shows HelloWorld being run successfully using both classpath declaration methods. Also, it shows the error messages you will get if the classpath is incorrect.

Some notes on the output:

  • You can see that the first two attempts to run HelloWorld threw a java.lang.NoClassDefFoundError. When you see this exception, it is because the classloader cannot find a class file. So, check that your CLASSPATH variable and -cp flag are correct.
  • Make sure you do not include the .java file extension when invoking the java command. This will confuse the java command and cause it to erroneously look for a file named java in the HelloWorld subdirectory (check out Java packages for a full explanation).
  • The third attempt succeeds because I add the "-cp ." flag. Because I had a CLASSPATH environment variable set, the current working directory is not explicitly part of the classpath: I needed to add the "." either to the CLASSPATH variable or to the -cp flag.

1 | 

Discussion about this tutorial

  Start a new Discussion | Read All Discussion
Subject RepliesLast Post
Javaorigin.com contact@javaorigin.com