Writing and Running First Program
In the Java programming language, all source code is first
written in plain text files ending with the
.java extension. Those source files are then compiled into .class files by the javac compiler [the name of the class files is same that of the
class defined]. A .class file does not contain code that
is native to the processor; it instead contains bytecodes — the
machine language of the Java Virtual Machine1 (Java VM). The java.exe launcher tool then runs the
application with an instance of the Java Virtual Machine. 
An overview of the
software development process.
Because the Java VM is available on many different
operating systems, the same
.class files are capable of running on Microsoft Windows, the Solaris TM
Operating System (Solaris OS), Linux, or Mac OS. 
Through the Java VM,
the same application is capable of running on multiple platforms.
The
Java Platform
A platform
is the hardware or software environment in which a program runs. We've already
mentioned some of the most popular platforms like Microsoft Windows, Linux,
Solaris OS, and Mac OS. Most platforms can be described as a combination of the
operating system and underlying hardware. The Java platform differs from most
other platforms in that it's a software-only platform that runs on top of other
hardware-based platforms.
The Java platform has two components:
- The Java Virtual Machine
- The Java Application Programming
Interface (API)
The API is a large collection of ready-made software
components that provide many useful capabilities. It is grouped into libraries
of related classes and interfaces; these libraries are known as packages.

The
API and Java Virtual Machine insulate the program from the underlying hardware.
As a
platform-independent environment, the Java platform can be a bit slower than
native code. However, advances in compiler and virtual machine technologies are
bringing performance close to that of native code without threatening
portability.
Step 1. Create a
Source File
/**
* The HelloWorldApp class implements an application that
* simply prints "Hello World!" to standard output.
*/
class HelloWorldApp {
public static void main(String[] args) {
System.out.println("Hello World!"); // Display the string.
} } |
Save the code in a file with the name
HelloWorldApp.java.[Not compulsory to save it with
same name as that of class name] Then, we can go straight to Compile the Source
File into a .class File.
Step 2. Compile the Source File into a
.class File
Bring up a shell, or "command," window. We can do
this from the Start menu by choosing Command Prompt (Windows XP),
or by choosing Run... and then entering
cmd.
To compile the source file, change the current directory to
the directory where the file is located. For example, if the source directory
is
java on the C drive, type the following command at the prompt and press Enter:
cd C:\java
Now the prompt should change to
C:\java>.
If we enter
dir at the prompt, you should see our source file.
Now we are ready to compile. At the prompt, type the
following command and press Enter.
javac HelloWorldApp.java
The compiler has generated a bytecode file,
HelloWorldApp.class. At the prompt, type dir to see the new file that was generated. Now that we have a
.class file, you can run our program.
Step 3. Run the
Program
In the
same directory, enter the following command at the prompt:
java HelloWorldApp
A
Closer Look at the "Hello World!" Application
The
"Hello World!" application consists of three primary components:
source code comments, the
HelloWorldApp class definition, and the main method. The following explanation will provide us with a basic
understanding of the code.
Source Code Comments
The following bold text defines the comments of the
"Hello World!" application:
/**
* The HelloWorldApp class implements an application that
* simply prints "Hello World!" to standard output.
*/
class HelloWorldApp {
public static void main(String[] args) {
System.out.println ("Hello World!"); // Display the string.
}
}
Comments are ignored by the
compiler but are useful to other programmers. The Java programming language
supports three kinds of comments:
/*
text
*/
The compiler ignores everything
from
/* to */. /**
documentation */
This indicates a documentation
comment (doc comment, for short). The compiler ignores this kind of
comment, just like it ignores comments that use
/* and */. The javadoc tool uses doc comments when preparing automatically generated
documentation.// text
The compiler ignores everything from
// to the end of the line.
The HelloWorldApp
Class Definition
The following bold text begins the class definition block
for the "Hello World!" application:
class HelloWorldApp {
public static void main(String[] args) {
System.out.println ("Hello World!"); // Display the string.
}
}
As shown above, the most basic
form of a class definition is:
class Identifier {
. . .
}
The keyword
class begins the class definition for a class named Identifier, and the code for each class appears between the opening and
closing curly braces marked in bold above. Java being the object oriented
programming language every code should and must be written in a class even main
method must be defined in a class.
The main
Method
The following bold text begins the definition of the
main
method: class HelloWorldApp {
public static void main(String[] args) {
System.out.println ("Hello World!"); //Display the string.
}
}
In the Java programming language,
every application must contain a
main
method whose signature is: public static void main(String[] args)
The modifiers
public
and static can be written in either order (public
static or static
public), but the convention is to use public
static as shown above. We can name the
argument anything we want, but most programmers choose "args" or
"argv".
The
main
method is the entry point for our application and will subsequently invoke all
the other methods required by our program.
The
main
method accepts a single argument: an array of elements of type String.
public static void main(String[] args)
This array is the mechanism through which the runtime system passes information to our application. Each string in the array is called a command-line argument. Command-line arguments let users affect the operation of the application without recompiling it
The "Hello World!"
application ignores its command-line arguments, but we should be aware of the
fact that such arguments do exist.
Finally, the line:
System.out.println ("Hello World!");
uses the
System
class from the core library to print the "Hello World!" message to
standard output.
System is a class present in java.lang package [Java
Libraries are bundled in form of bundles called packages]. This class has one
member called out which has functionality of printing.
Common
Problems (and Their Solutions)
1. Compiler Problems
Common
Error Messages on Microsoft Windows Systems
·
'javac' is not recognized as an internal or external command,
operable program or batch file
If we receive this error, Windows cannot find the compiler
(
javac).
Here's one way to tell Windows where to find
javac. Suppose we installed the JDK in C:\jdk5. At the prompt we have to type
the following command and press Enter: C:\jdk5\bin\javac HelloWorldApp.java
If we
choose this option, we willl have to precede our
javac and java commands with C:\jdk6\bin\ each time we compile or run a
program.
·
Update the PATH variable (Optional) will resolve the problem permanently
Set the
PATH variable if we want to be able to conveniently run the JDK executables
(javac.exe, java.exe, javadoc.exe, etc.) from any directory without having to
type the full path of the command. If we don't set the PATH variable, we need
to specify the full path to the executable every time we run it, such as:
C:> "\Program
Files\Java\jdk1.5.0_\bin\javac" MyClass.java
It's
useful to set the PATH permanently so it will persist after rebooting.
·
How do I set the PATH permanently?
To set the PATH permanently, add the full path of the jdk1.5.0_\bin
directory to the PATH variable. Typically this full path looks something like C:\Program
Files\Java\jdk1.5.0_\bin . Set the PATH as follows on
Microsoft Windows:
To set the PATH permanently, add the full path of the jdk1.5.0_
a.
Click Start > Control Panel > System on Windows XP or
Start > Settings > Control Panel > System on Windows 2000.
b.
Click Advanced > Environment Variables.
c.
Add the location of bin folder of JDK installation for PATH in
User Variables and System Variables. A typical value for PATH is:
C:\Program
Files\Java\jdk1.5.0_\bin
1. Exception in thread "main"
java.lang.NoClassDefFoundError: HelloWorldApp
If we receive this error,
java cannot find our bytecode file, HelloWorldApp.class.
In such case we might have to change our CLASSPATH
variable. To see if this is necessary, try clobbering the classpath with the
following command.
set CLASSPATH=.
Now enter
java HelloWorldApp again. If the program works now, we will have to change
your CLASSPATH variable. The CLASSPATH variable is set in the same manner as
that done for the PATH.
2.
Exception in thread "main" java.lang.NoClassDefFoundError:
HelloWorldApp/class
A common mistake made by beginner programmers is to try and
run the
java launcher on the .class file that was created by the
compiler. For example, we will get this error if you try to run your program
with java
HelloWorldApp.class instead of java
HelloWorldApp.
Remember, the argument is the name of the class that you want to use, not
the filename. 3. Exception in thread "main"
java.lang.NoSuchMethodError: main
The Java VM requires that the class you execute with it
have a
main method at which to begin
execution of your application. Check the signature of the main method.
No comments:
Post a Comment