Title: First Cup of Java
1First Cup of Java
- Written by Jeff SmithPortions borrowed from the
Sun Java Tutorial
2First Cup of Coffee -1
- To write your first program, you need
- The JavaTM 2 Platform, Standard Edition. You can
download the JDK from http//java.sun.com. Make
sure you download the JDK, not the JRE (Java
runtime environment). - A text editor. In this example, we'll use
NotePad, the simple editor included with the
Windows platforms. You can easily adapt these
instructions if you use a different text editor. - These two items are all you need to write your
first Java program. Later on, we will use the
Eclipse IDE to develop our Java programs.
3First Cup of Coffee -2
Using an IDE (integrated development
environments) is generally a more productive way
to write Java programs. An IDE provides you with
syntax highlighting, code completion (type in an
object name and a . and the IDE lists the
properties and methods for that object),
project/file management, and integrated
debugging. JBuilder, NetBeans, and Eclipse are
excellent (and free!) IDEs
4First Cup of Coffee -3
- Your first program, HelloWorldApp, will simply
display the greeting "Hello world!". To create
this program, you will - Create a source file. A source file contains
text, written in the Java programming language,
that you and other programmers can understand.
You can use any text editor to create and edit
source files. - Compile the source file into a bytecode file. The
compiler, javac, takes your source file and
translates its text into instructions that the
Java Virtual Machine (Java VM) can understand.
The compiler converts these instructions into a
bytecode file.
5First Cup of Coffee -4
- 3. Run the program contained in the bytecode
file. The Java interpreter installed on your
computer implements the Java VM. This interpreter
takes your bytecode file and carries out the
instructions by translating them into
instructions that your computer can understand. -
-
6First Cup of Coffee -5
Why Bytecodes are Cool So, you've heard that
with the Java programming language, you
can "write once, run anywhere." This means that
when you compile your program, you don't generate
instructions for one specific platform. Instead,
the compiler generates Java bytecodes, which are
instructions for the Java Virtual Machine (Java
VM). If your platform--whether it's Windows,
UNIX, MacOS, or an Internet browser--has the
Java VM, it can understand those bytecodes.
7First Cup of Coffee -6
- To create the program, create a source file.
- Start NotePad. In a new document, type in the
following code - /
- The HelloWorldApp class implements an
application - that displays "Hello World!" to the standard
out. - /
- public class HelloWorldApp
-
- public static void main(String args)
-
- // Display "Hello World!"
- System.out.println("Hello World!")
-
-
8First Cup of Coffee -7
Note Java Is Case Sensitive The Java compiler
and interpreter are case-sensitive, so you
must capitalize consistently. HelloWorldApp is
not the same as Helloworldapp. This case
sensitivity reflects Javas heritage as an
outgrowth of C and C.
9First Cup of Coffee -8
Save this code to a file. All the code we write
for these our Java training classes will go under
a folder called C\CoreJava For this hello
world example, create a sub folder C\CoreJava\He
lloWorld In notepad, do a File, Save
As C\CoreJava\HelloWorld\HelloWorld.java Note
if you include the quotation marks, notepad
wont save the file with the default .txt
extension
10First Cup of Coffee -9
Compile the Code Pop up a command prompt and
navigate to your C\CoreJava\HelloWorld
directory. You should see the HelloWorld.java
file you just created. To compile the
code, execute the following command javac
HelloWorldApp.java Note if you get an error
message like bad command or name specified not
recognized, then Windows cant find the java
compiler, javac. Verify that JDK bin directory is
in the path. The compiler has now generated a
Java bytecode file for you, with the name
HelloWorld.class.
11First Cup of Coffee -10
Run the Java Program In the same directory, type
the following to run your program java
HelloWorld You should see your message, Hello
World! appear. Note If you get an
error Exception in thread "main
java.lang.NoClassDefFoundError HelloWorldApp
Java cannot find your bytecode file,
HelloWorldApp.class. This means that Java could
not find your bytecode file in the
current directory. It could also mean that your
environment variable, CLASSPATH is messed up. You
can fix this by typing set CLASSPATH
12First Cup of Coffee -11
- Youre Done!
- You created a Java source file with a .java
extension - You compiled it, creating a bytecode file with a
.class extension - You ran it from the command line by typing java
filename (without the .class extension)