Docstoc

Running a Java Program

Document Sample
Running a Java Program Powered By Docstoc
					                                        Running a Java Program

The Way Java Works




    1. Source
       Create a source document in Java language.
    2. Compiler
       Run the document through a source code compiler. The compiler checks for errors and won’t let
       us compile until it’s satisfied that everything will run correctly.
    3. Output Code
       The compiler creates a new document, coded into Java bytecode. Any device capable of running
       Java will be able to interpret this file into something it can run. The compiled bytecode is
       platform-independent.
    4. Virtual Machines
       The Virtual Machine reads and runs the bytecode.



Running a Java Program

After learning how Java works, let's try to make a simple program in Java language. But first make sure
you have installed Java Development Kit (JDK). JDK can be downloaded at
http://www.oracle.com/technetwork/java/javase/downloads/index.html. Let’s start.

    1. Create Source Program
       We will use the "Notepad" editor that comes with Microsoft Windows operating systems. The
       goal is to create a text file called Hello.java containing the characters below:

        class Hello{
          public static void main ( String[] args ){
            System.out.println("Hello Java!");
          }
        }
       Copy the code above into notepad and then save it as Hello.java (NOT txt file).
    2. Compile it
   a. Change the directory where the files are stored. As an example, I store Hello.java in “d:\”.
   b. Compile the Hello.java file by running javac (the compiler application).
      Type javac Hello.java then press enter button.
   c. If you don’t have errors, you’ll get a second document named Hello.class




3. Output
   The compiler-generated Hello.class file is made up of bytecodes.
4. Run the program
   Run the program by starting the Java Virtual Machine (JVM) with the Hello.class file. The JVM
   translates the bytecode into something the underlying platform understands, and runs the
   program. To do that enter command java Hello.

				
DOCUMENT INFO
Categories:
Tags:
Stats:
views:12
posted:2/28/2011
language:English
pages:2