Pages

Sunday, February 27, 2011

Running a Java Program

The Way Java Works
  1. Source
Create a source document in Java language.
  1. 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.
  1. 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.
  1. 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).
  1. Compile it
    1. Change the directory where the files are stored.  As an example, I store Hello.java in “d:\”.
    2. Compile the Hello.java file by running javac (the compiler application). Type javac Hello.java then press enter button.
    3. If you don’t have errors, you’ll get a second document named Hello.class

  1. Output
The compiler-generated Hello.class file is made up of bytecodes.
  1. 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.






No comments:

Post a Comment