Running your first Java application from the command prompt for user of Microsoft Windows
To write and run your first Java application on Microsoft Windows, you’ll need the following:
- The Java SE Development Kit 6 (JDK 6)
You can download the Windows version from http://java.sun.com/javase/6/download.jsp. you can Consult the installation instructions.
- A Text Editor
We’ll be using Notepad, a simple editor included with the Windows Platform. These two items are all you’ll need to write and run your applications.
Creating Your First Application
Your first application, MyNameIsGino, will simply display “My Name Is Gino Wine” To create this program, you will:
- Create a source file
- Compile the source file into a .class file
javac
) 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.- Run the program
java
) uses the Java virtual machine to run your application. Create a Source File
First, start your editor. You can launch the Notepad editor from the Start menu by selecting Programs > Accessories > Notepad. In a new document, type in the following code:/**
* The MyNameIsGino class implements an application that
* simply prints "My Name is Gino Wine!" to standard output.
*/
class MyNameIsGino {
public static void main(String[] args) {
System.out.println("My Name Is Gino Wine!"); // Display the string.
}
}
Save the code in a file with the name
MyNameIsGino.java
. To do this in Notepad, first choose the File > Save As menu item. Then, in the Save Asdialog box:- Using the Save in combo box, specify the folder (directory) where you'll save your file. In this example, the directory is Gino
Java
on theC
drive. - In the File name text field, type
"
MyNameIsGino.java"
, including the quotation marks. - From the Save as type combo box, choose Text Documents (*.txt).
- In the Encoding combo box, leave the encoding as ANSI.
- Now click Save, and exit Notepad.
Compile the Source File into a .class File
Bring up a shell, or "command," window. You can do this from the Start menu by choosing Command Prompt (Windows XP), or by choosing Run...and then entering cmd
.The prompt shows your current directory. When you bring up the prompt, your current directory is usually your home directory for Windows XP.
To compile your source file, change your current directory to the directory where your file is located. For example, if your source directory is Gino
Java
on the C
drive, type the following command at the prompt and press Enter:cd C:\Gino java
Now the prompt should change toC:\java>
.
If you enterdir
at the prompt, you should see your source file.
Now you are ready to compile. At the prompt, type the following command and press Enter.javac HelloWorldApp.java
The compiler has generated a bytecode file,MyNameIsGino.class
. At the prompt, typedir
to see the new file that was generated
Now that you have a.class
file, you can run your program.
Run the Program
In the same directory, enter the following command at the prompt:
java MyNameIsGino
Walla! You are done. You have successfully run your first java application.
Click here for how to set your PATH Environment Variable
No comments:
Post a Comment