Professional Documents
Culture Documents
A Checklist
To write your first program, you'll need: 1. The Java SE Development Kit 6 (JDK 6) You can download the Windows version now. (Make sure you download the JDK, not the JRE.) Consult the installation instructions. 2. A text editor In this example, we'll use Notepad, a 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'll need to write your first application.
7/22/2009
Compile the source file into a .class file The Java programming language compiler (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 The Java application launcher tool (java) uses the Java virtual machine to run your application.
top
Create a Source File To create a source file, you have two options: You can save the file HelloWorldApp.java on your computer and avoid a lot of typing. Then, you can go straight to Compile the Source File into a .class File. Or, you can use the following (longer) instructions. First, start your editor. You can launch the Notepad editor from the Start menu by selecting Programs > Accessories > otepad. In a new document, type in the following 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. } }
Save the code in a file with the name HelloWorldApp.java. To do this in Notepad, first choose the File > Save As menu item. Then, in the Save As dialog
java.sun.com/docs//win32.html 2/10
7/22/2009
box: 1. Using the Save in combo box, specify the folder (directory) where you'll save your file. In this example, the directory is java on the C drive. 2. In the File name text field, type "HelloWorldApp.java", including the quotation marks. 3. From the Save as type combo box, choose Text Documents (*.txt). 4. In the Encoding combo box, leave the encoding as ANSI. When you're finished, the dialog box should look like this.
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 shell window should look similar to the following figure.
java.sun.com/docs//win32.html
3/10
7/22/2009
A shell window.
The prompt shows your current directory. When you bring up the prompt, your current directory is usually your home directory for Windows XP (as shown in the preceding figure). To compile your source file, change your current directory to the directory where your file is located. For example, if your source directory is java on the C drive, type the following command at the prompt and press Enter:
cd C:\java
Now the prompt should change to C:\java>. ote: To change to a directory on a different drive, you must type an extra command: the name of the drive. For example, to change to the java directory on the D drive, you must enter D:, as shown in the following figure.
java.sun.com/docs//win32.html
4/10
7/22/2009
If you enter dir at the prompt, you should see your source file, as the following figure shows.
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, HelloWorldApp.class. At the prompt, type dir to see the new file that was generated, as shown in the following figure.
java.sun.com/docs//win32.html 5/10
7/22/2009
Now that you have a .class file, you can run your program. If you encounter problems with the instructions in this step, consult the Common Problems (and Their Solutions).
top
Run the Program In the same directory, enter the following command at the prompt:
java HelloWorldApp
Congratulations! Your program works! If you encounter problems with the instructions in this step, consult the Common Problems (and Their Solutions).
top
java.sun.com/docs//win32.html
6/10
7/22/2009
Your use of this page (http://java.sun.com/docs/books/tutorial (Last Updated 2/14/2008)) and all the material on pages under "The Java Tutorials" banner is subject to the Terms of Use. Additionally, (i) any example code contained in any of these Java Tutorials pages is also licensed under the Code Sample License, and (ii) the entire Java Tutorials content is available for download under the Java Tutorials Limited Non-Commercial License for limited, noncommercial individual and/or research and instructional use at Sun Download Center.
Previous page: "Hello World!" for the NetBeans IDE ext page: "Hello World!" for Solaris OS and Linux Discuss We welcome your participation in our community. Please keep your comments civil and on point. You may optionally provide your email address to be notified of replies your information is not used for any other purpose. By submitting a comment, you agree to these Terms of Use.
Guest says:
Wednes day, June 1 7 , 2 0 0 9 , 1 8 :3 2 :1 2
i tried to run this but it gave an error that 'javac is not recognised as an external or internal command'. So how to run this program. and one more thing how do i set the path? More
L ike this c omment? [yes ] [no] (Sc ore: 5 by 7 votes )
reply
T hurs day, June 1 8 , 2 0 0 9 , 1 7 :0 1 :5 2
John replies:
Right click on C omputer icon and go to properties. There go to advanced settings. Then look for Environment Variables. C lick on the command button. Then in System Variables, look for path. Select path and then click edit. Go to the end and then add the folder where you have javac command. It will usually be C :\Program Files\Java\jdk(with version)\bin. Then try to run your application the way you were doing earlier. Good luck! More
Like this c omment? [yes ] [no] (Sc ore: 5 by 7 votes )
reply
T hurs day, June 1 8 , 2 0 0 9 , 1 9 :4 9 :3 5
reply
Sunday, June 2 1 , 2 0 0 9 , 0 6 :2 0 :0 5
Novice replies:
I tried what you said but it's not working for me %SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbe m\;C :\Program Files\Java\jdk1.6.0_14\bin what i did was transfer my HelloWorldApp.java into that location where javac.exe was then it work but it's not working in the other folder Help please More
L ike this c omment? [yes ] [no] (Sc ore: -1 by 1 vote)
reply
T ues day, June 2 3 , 2 0 0 9 , 1 2 :2 1 :4 6
I have the same problem as novice, Dont know what to do next. any suggestions? More
L ike this c omment? [yes ] [no] (Sc ore: 0 by 2 votes )
reply
Wednes day, June 2 4 , 2 0 0 9 , 0 4 :5 3 :5 4
Student replies:
java.sun.com/docs//win32.html
7/10
7/22/2009
Any computer requires javac location(path) to compile source code. I copy the entire bin folder (26 MB) to program folder. It works well with jEdit. More
L ike this c omment? [yes ] [no] (Sc ore: -1 by 1 vote)
reply
T ues day, June 3 0 , 2 0 0 9 , 0 2 :4 6 :3 8
bioingeniero replies:
Maybe its because you didnt change the directory to the directory where you have saved your file. Save the file in a new folder in C :\Java. Then type the following command at the prompt and press Enter: cd C :\java And continue with the run. More
L ike this c omment? [yes ] [no] (Sc ore: -1 by 1 vote)
reply
Wednes day, June 2 4 , 2 0 0 9 , 1 7 :0 8 :5 9
Guest replies: hoew to program my pc w/out the help of my classm8? ^_^ hekhekhek?> More
L ike this c omment? [yes ] [no]
reply
Sunday, June 2 8 , 2 0 0 9 , 0 9 :5 5 :0 8
varun replies:
i added the path where the BIn folder was.. and it works perfect.. thanks... More Axel
L ike this c omment? [yes ] [no] (Sc ore: -1 by 1 vote)
reply
F riday, July 0 3 , 2 0 0 9 , 1 7 :5 0 :0 2
replies:
Thank you John! I had same problem as first guest. And you solved it! Thanks! More
L ike this c omment? [yes ] [no] (Sc ore: -1 by 1 vote)
reply
Wednes day, July 0 1 , 2 0 0 9 , 0 9 :3 0 :3 7
Guest replies:
I tried to set classpath= and it didn't seem to do anythying. Do i still have to play around with it or is there a specific way to do it? More
Like this c omment? [yes ] [no] (Sc ore: -1 by 1 vote)
reply
Guest says:
i think u haven't installed jdk in your system n u just copied the already installed files in your c drive! that's y it hadn't worked even setting the path also! More
L ike this c omment? [yes ] [no] (Sc ore: - 1 by 1 vote)
reply
T ues day, June 3 0 , 2 0 0 9 , 1 5 :1 7 :1 4
Guest replies:
I have compiled the class but still im getting the "Exception in thread "main" java.lang.NoC lassDefFoundError: imageSignatureUtility" error when I try to run the java file. However directory path and all is fine..can u plz suggest wt could be d problem. More
Like this c omment? [yes ] [no] (Sc ore: -1 by 1 vote)
reply
Guest says: I seem to be getting a weird error. the error looks like this: "Exception in thred "main" java.lang.NoSuchMethodError: main" Without the quotations. Help? More
L ike this c omment? [yes ] [no] (Sc ore: - 1 by 1 vote)
reply
T hurs day, July 0 2 , 2 0 0 9 , 1 5 :5 7 :4 4
jo replies:
Hi, You might have misspelt the method name or might have referred to a class
java.sun.com/docs//win32.html
8/10
7/22/2009
that doesn't exist at all.Also check if ur program has the correct syntax of main method as ----------------------------------------public static void main(String args[]) { //statements u want to write } --------------------------------------More
Like this c omment? [yes ] [no] (Sc ore: 0 by 2 votes )
reply
T hurs day, July 0 2 , 2 0 0 9 , 1 8 :1 9 :4 6
reply
Guest says:
M onday, July 1 3 , 2 0 0 9 , 0 7 :4 3 :4 8
When I compiled HelloWorldApp.java, I received the error message "javac is not recognized as an internal or external command" Please, help. Thank you More
L ike this c omment? [yes ] [no]
reply
T ues day, July 1 4 , 2 0 0 9 , 1 2 :5 0 :5 6
Skynila2007 replies:
hey u have to set path u r path which contain javac.exe.then only u can execute u r file for an example if u have u r file in C :\java then u have to go to the dos mode and go to u r file type C :\java then from there u have to set u r path like this it will be like this"C :\java>set path=C :\Program Files\Java\jdk1.5.0_01\bin and enter.now u r path is set.here the path will change according to u r installation.when u install jdk if u give any other path u have to give that only.ok. then u can check u r path whether its properply set by c:\java>echo %path%.it will show u r path.u have done everything correct now.then move to the next step. C :\Java>javac (u r filename.java) after that call u r class like this C :\Java>java (class name) thats all More
Like this c omment? [yes ] [no]
reply
Alexey says:
Hi, Does anybody know why we run program simply by calling HelloWorldApp (name of class?) and not by running HelloWorldApp.class. Is there reference to HelloWorldApp created after compiling HelloWorldApp.java and stored in the memory? Thank you More
L ike this c omment? [yes ] [no] (Sc ore: 1 by 1 vote)
reply
Melanie says: I'm getting this message: HelloWorldApp.java:5: error while writing HelloWorldApp: HelloWorldApp.class (Access is denied) class HelloWorldApp { ^ 1 error I'm not sure what that's supposed to mean, or what to do about it. I figured out the rest of it, I just can't switch it to a .class file. It's kind of sad. I'm so close... More
L ike this c omment? [yes ] [no]
3 days ago, 0 0 :2 6 :2 7
reply
java.sun.com/docs//win32.html
9/10
7/22/2009
Melanie says:
Okay, I got it. Took me a day, but I figured it out. I guess my files shouldn't have been in Program Files. I moved them out of there and it worked. More
L ike this c omment? [yes ] [no]
reply
Guest says: hey thank you very much . it help me a lot More
L ike this c omment? [yes ] [no]
3 days ago, 2 2 :4 3 :2 9
reply
Leave a comment
Controls
(P owered by JS-Kit)
java.sun.com/docs//win32.html
10/10