FIRST About The Java Technology
FIRST About The Java Technology
FIRST About The Java Technology
Architecture
Simple
neutral
Object
Portable
oriented
Distributed High performance
Multithreaded Robust
Dynamic Secure
You've already been introduced to the Java Virtual Machine; it's the base
for the Java platform and is ported onto various hardware-based
platforms.
documentation.
• Deployment Technologies: The JDK software provides standard
mechanisms such as the Java Web Start software and Java Plug-
In software for deploying your applications to end users.
• User Interface Toolkits: The Swing and Java 2D toolkits make it
possible to create sophisticated Graphical User Interfaces (GUIs).
• Integration Libraries: Integration libraries such as the Java IDL
API, JDBC API, Java Naming and Directory Interface ("J.N.D.I.")
TM TM
API, Java RMI, and Java Remote Method Invocation over Internet
Inter-ORB Protocol Technology (Java RMI-IIOP Technology)
enable database access and manipulation of remote objects.
How Will Java Technology Change My Life?
It's time to write your first application! These detailed instructions are for users
of the NetBeans IDE. The NetBeans IDE runs on the Java platform, which
means that you can use it with any operating system for which there is a JDK 6
available. These operating systems include Microsoft Windows, Solaris OS,
Linux, and Mac OS X.
The IDE invokes the Java programming language compiler (javac), which
takes your source file and translates its text into instructions that the
Java virtual machine can understand. The instructions contained within
this file are known as bytecodes.
The IDE invokes the Java application launcher tool (java), which uses the
Java virtual machine to run your application.
3. In the New Project wizard, expand the General category and select Java
Application as shown in the following figure:
NetBeans IDE, New Project wizard,
Choose Project page.
4. In the Name and Location page of the wizard, do the following (as
shown in the figure below):
o In the Project Name field, type Hello World App.
o In the Create Main Class field, type helloworldapp.HelloWorldApp.
o Leave the Set as Main Project checkbox selected.
NetBeans IDE, New Project wizard, Name
and Location page.
5. Click Finish.
The project is created and opened in the IDE. You should see the
following components:
Because you have left the Create Main Class checkbox selected in the
New Project wizard, the IDE has created a skeleton class for you. You
can add the "Hello World!" message to the skeleton code by replacing
the line:
/**
*
* @author
*/
with these lines:
/**
* The HelloWorldApp class implements an application that
* simply prints "Hello World!" to standard output.
*/
These four lines are a code comment and do not affect how the program runs.
Later sections of this tutorial explain the use and format of code comments.
Type
all code, commands, and file names exactly as shown. Both the
compiler (javac) and launcher (java) are case-sensitive, so you
must capitalize consistently.
HelloWorldApp helloworldapp
/*
* HelloWorldApp.java
*
* Created on February 5, 2006, 6:43 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package helloworldapp;
/**
* The HelloWorldApp class implements an application that
* simply prints "Hello World!" to standard output.
*/
public class HelloWorldApp {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
System.out.println("Hello World!"); // Display the string.
}
}
Compile the Source File into a .class File
To compile your source file, choose Build | Build Main Project from the
IDE's main menu.
The Output window opens and displays output similar to what you see
in the following figure
If the build output concludes with the statement BUILD FAILED, you
probably have a syntax error in your code. Errors are reported in the
Output window as hyper-linked text. You double-click such a hyper-link
to navigate to the source of an error. You can then fix the error and once
again choose Build | Build Main Project.
Now that you have built the project, you can run your program.
Now that you've seen the "Hello World!" application (and perhaps even
compiled and run it), you might be wondering how it works. Here again
is its code:
/**
* 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.
}
}
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 you with a basic
understanding of the code, but the deeper implications will only become
apparent after you've finished reading the rest of the tutorial.
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 */
// text
The compiler ignores everything from // to the end of the line.
The following bold text begins the class definition block for the "Hello
World!" application:
/**
* The HelloWorldApp class implements an application that
* simply displays "Hello World!" to the standard output.
*/
class HelloWorldApp {
public static void main(String[] args) {
System.out.println("Hello World!"); // Display the string.
}
}
class name {
...
}
The keyword class begins the class definition for a class named name,
and the code for each class appears between the opening and closing
curly braces marked in bold above. Chapter 2 provides an overview of
classes in general, and Chapter 4 discusses classes in detail. For now it
is enough to know that every application begins with a class definition.
The following bold text begins the definition of the main method:
/**
* The HelloWorldApp class implements an application that
* simply displays "Hello World!" to the standard output.
*/
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:
The main method is similar to the main function in C and C++; it's the
entry point for your application and will subsequently invoke all the
other methods required by your program.
System.out.println("Hello World!");
uses the System class from the core library to print the "Hello World!"
message to standard output. Portions of this library (also known as the
"Application Programming Interface", or "API") will be discussed
throughout the remainder of the tutorial.
Compiler Problems
If you receive this error, Windows cannot find the compiler (javac).
Here's one way to tell Windows where to find javac. Suppose you
installed the JDK in C:\jdk6. At the prompt you would type the following
command and press Enter:
C:\jdk6\bin\javac HelloWorldApp.java
If you choose this option, you'll have to precede your javac and java
commands with C:\jdk6\bin\ each time you compile or run a program. To
avoid this extra typing, consult the section Update the PATH variable in
the JDK 6 installation instructions.
If you mistype part of a program, the compiler may issue a syntax error.
The message usually displays the type of the error, the line number
where the error was detected, the code on that line, and the position of
the error within the code. Here's an error caused by omitting a
semicolon (;) at the end of a statement:
testing.java:14: `;' expected.
System.out.println("Input has " + count + " chars.")
^
1 error
Sometimes the compiler can't guess your intent and prints a confusing
error message or multiple error messages if the error cascades over
several lines. For example, the following code snippet omits a
semicolon (;) from the bold line:
while (System.in.read() != -1)
count++
System.out.println("Input has " + count + " chars.");
When processing this code, the compiler issues two error messages:
testing.java:13: Invalid type expression.
count++
^
testing.java:14: Invalid declaration.
System.out.println("Input has " + count + " chars.");
^
2 errors
The compiler issues two error messages because after it processes
count++, the compiler's state indicates that it's in the middle of an
expression. Without the semicolon, the compiler has no way of knowing
that the statement is complete.
If you see any compiler errors, then your program did not successfully
compile, and the compiler did not create a .class file. Carefully verify the
program, fix any errors that you detect, and try again.
Semantic Errors
In addition to verifying that your program is syntactically correct, the
compiler checks for other basic correctness. For example, the compiler
warns you each time you use a variable that has not been initialized:
testing.java:13: Variable count may not have been initialized.
count++
^
testing.java:14: Variable count may not have been initialized.
System.out.println("Input has " + count + " chars.");
^
2 errors
Again, your program did not successfully compile, and the compiler did
not create a .class file. Fix the error and try again.
Runtime Problems
If you receive this error, java cannot find your bytecode file,
HelloWorldApp.class.
One of the places java tries to find your .class file is your current
directory. So if your .class file is in C:\java, you should change your
current directory to that. To change your directory, type the following
command at the prompt and press Enter:
cd c:\java
The prompt should change to C:\java>. If you enter dir at the prompt, you
should see your .java and .class files. Now enter java HelloWorldApp again.
If you still have problems, you might have to change your 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, you'll have
to change your CLASSPATH variable. To set this variable, consult the
Update the PATH variable section in the JDK 6 installation instructions.
The CLASSPATH variable is set in the same manner.
The Java Virtual Machine requires that the class you execute with it
have a main method at which to begin execution of your application. A
Closer Look at the "Hello World!" Application discusses the main method
in detail. If you are missing this method, you'll see the following error at
runtime:
Exception in thread "main" java.lang.NoSuchMethodError: main
In the above message, classname is the name of the class that you tried
to run.