"Hello World!" for Microsoft Windo…

Trail: Getting Started Lesson: The "Hello World!" Application

"Hello World!" for Microsoft Windows
It's time to write your first application! The following instructions are for users of Windows XP Professional, Windows XP Home, Windows Server 2003, Windows 2000 Professional, and Windows Vista. Instructions for other platforms are in "Hello World!" for Solaris OS and Linux and "Hello World!" for the NetBeans IDE. If you encounter problems with the instructions on this page, consult the Common Problems (and Their Solutions). A Checklist Creating Your First Application Create a Source File Compile the Source File into a .class File Run the Program

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.

Creating Your First Application
Your first application, HelloWorldApp, will simply display the greeting "Hello world!". To create this program, you will: Create a source file A source file contains code, written in the Java programming language, that you and other programmers can understand. You can use any text editor to
java.sun.com/docs/…/win32.html 1/10

Then.html 2/10 . Both the compiler (javac) and launcher tool (java) are case-sensitive. In a new document. To do this in Notepad. so you must capitalize consistently. type in the following code: /** * The HelloWorldApp class implements an application that * simply prints "Hello World!" to standard output. you can use the following (longer) instructions. first choose the File > Save As menu item. you can go straight to Compile the Source File into a .7/22/2009 "Hello World!" for Microsoft Windo… create and edit source files. Then.class File.java on your computer and avoid a lot of typing. The instructions contained within this file are known as bytecodes.java. in the Save As dialog java. Run the program The Java application launcher tool (java) uses the Java virtual machine to run your application. You can launch the Notepad editor from the Start menu by selecting Programs > Accessories > otepad.println("Hello World!"). commands. you have two options: You can save the file HelloWorldApp.com/docs/…/win32.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.out. Compile the source file into a . and file names exactly as shown. */ class HelloWorldApp { public static void main(String[] args) { System. } } Be Careful When You Type Type all code. HelloWorldApp helloworldapp Save the code in a file with the name HelloWorldApp. Or. First. start your editor. // Display the string.sun. top Create a Source File To create a source file.

top Compile the Source File into a .sun.. In this example. or "command." window. the dialog box should look like this. The Save As dialog just before you click Save . The shell window should look similar to the following figure. 2.java". leave the encoding as ANSI. java. 4..com/docs/…/win32. Using the Save in combo box. and exit Notepad.html 3/10 . You can do this from the Start menu by choosing Command Prompt (Windows XP). Now click Save . choose Text Documents (*. or by choosing Run. including the quotation marks. In the File name text field.class File Bring up a shell.txt). type "HelloWorldApp. the directory is java on the C drive. 3. When you're finished. and then entering cmd.7/22/2009 "Hello World!" for Microsoft Windo… box: 1. From the Save as type combo box. specify the folder (directory) where you'll save your file. In the Encoding combo box.

For example. to change to the java directory on the D drive.com/docs/…/win32. if your source directory is java on the C drive. ote: To change to a directory on a different drive. When you bring up the prompt. For example. you must type an extra command: the name of the drive. your current directory is usually your home directory for Windows XP (as shown in the preceding figure). The prompt shows your current directory. change your current directory to the directory where your file is located.7/22/2009 "Hello World!" for Microsoft Windo… A shell window. type the following command at the prompt and press Enter: cd C:\java Now the prompt should change to C:\java>. java. you must enter D:.sun.html 4/10 . To compile your source file. as shown in the following figure.

type the following command and press Enter. type dir to see the new file that was generated. Now you are ready to compile.class. If you enter dir at the prompt.html 5/10 . javac HelloWorldApp. you should see your source file. At the prompt. Directory listing showing the .java The compiler has generated a bytecode file. as the following figure shows. At the prompt.7/22/2009 "Hello World!" for Microsoft Windo… Changing directory on an alternate drive. HelloWorldApp.java source file. java.com/docs/…/win32.sun. as shown in the following figure.

com/docs/…/win32. consult the Common Problems (and Their Solutions).class file. top java. top Run the Program In the same directory.html 6/10 . you can run your program. Congratulations! Your program works! If you encounter problems with the instructions in this step. If you encounter problems with the instructions in this step. enter the following command at the prompt: java HelloWorldApp The next figure shows what you should now see: The program prints "Hello World!" to the screen.7/22/2009 "Hello World!" for Microsoft Windo… Directory listing.class file Now that you have a .sun. consult the Common Problems (and Their Solutions). showing the generated .

Guest says: Wednes day.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. 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. Dont know what to do next. 0 6 :2 0 :0 5 Novice replies: I tried what you said but it's not working for me %SystemRoot%\system32.com/docs/…/win32. About Sun | About This Site | Terms of Use | Trademarks A Sun Developer etwork Site Copyright 1995-2008 Sun Microsystems. 0 4 :5 3 :5 4 Student replies: java.html 7/10 . Good luck! More Like this c omment? [yes ] [no] (Sc ore: 5 by 7 votes ) reply T hurs day. 2 0 0 9 . Your use of this page (http://java.%SystemRoot%\System32\Wbe m\. By submitting a comment. Go to the end and then add the folder where you have javac command.sun. Then look for Environment Variables. 2 0 0 9 . It will usually be C :\Program Files\Java\jdk(with version)\bin. 2 0 0 9 . Additionally. Then in System Variables.7/22/2009 "Hello World!" for Microsoft Windo… Problems with the examples? Try Compiling and Running the Examples: FAQs. June 1 8 . More L ike this c omment? [yes ] [no] (Sc ore: -1 by 1 vote) reply Sunday. Then try to run your application the way you were doing earlier. you agree to these Terms of Use.0_14\bin what i did was transfer my HelloWorldApp. 2 0 0 9 . and (ii) the entire Java Tutorials content is available for download under the Java Tutorials Limited Non-Commercial License for limited.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. June 2 4 . June 1 8 . any suggestions? More L ike this c omment? [yes ] [no] (Sc ore: 0 by 2 votes ) reply Wednes day. 1 2 :2 1 :4 6 Bigger novice replies: I have the same problem as novice.C :\Program Files\Java\jdk1. 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'. 2 0 0 9 . look for path.6. C lick on the command button. (i) any example code contained in any of these Java Tutorials pages is also licensed under the Code Sample License. Complaints? Compliments? Suggestions? Give us your feedback. It worked! Thank you. 1 7 :0 1 :5 2 John replies: Right click on C omputer icon and go to properties. June 1 7 . Inc. June 2 1 .sun. There go to advanced settings. So how to run this program. 2 0 0 9 . 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.%SystemRoot%. noncommercial individual and/or research and instructional use at Sun Download Center. You may optionally provide your email address to be notified of replies — your information is not used for any other purpose. Please keep your comments civil and on point. Select path and then click edit. 1 9 :4 9 :3 5 Guest replies: Hey thanks a lot. June 2 3 .java into that location where javac.

0 9 :5 5 :0 8 varun replies: i added the path where the BIn folder was. 0 9 :3 0 :3 7 Guest replies: I tried to set classpath= and it didn't seem to do anythying.lang.com/docs/…/win32.html 8/10 . 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: T ues day.can u plz suggest wt could be d problem. June 2 4 .sun.. 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 . 0 9 :3 4 :2 8 reply T hurs day. Help? More L ike this c omment? [yes ] [no] (Sc ore: .1 by 1 vote) reply T ues day. the error looks like this: "Exception in thred "main" java. However directory path and all is fine. and it works perfect.NoC lassDefFoundError: imageSignatureUtility" error when I try to run the java file.. 2 0 0 9 . 2 0 0 9 . 1 7 :5 0 :0 2 replies: Thank you John! I had same problem as first guest.1 by 1 vote) Wednes day. June 3 0 .7/22/2009 "Hello World!" for Microsoft Windo… Any computer requires javac location(path) to compile source code. I copy the entire bin folder (26 MB) to program folder. More L ike this c omment? [yes ] [no] (Sc ore: -1 by 1 vote) reply Wednes day. 1 5 :1 7 :1 4 Guest replies: I have compiled the class but still im getting the "Exception in thread "main" java.. July 0 2 . 2 1 :1 5 :0 1 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: . You might have misspelt the method name or might have referred to a class java. More Like this c omment? [yes ] [no] (Sc ore: -1 by 1 vote) reply Guest says: I seem to be getting a weird error. 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.. Then type the following command at the prompt and press Enter: cd C :\java And continue with the run. 2 0 0 9 . thanks. It works well with jEdit. 2 0 0 9 .lang. June 2 3 . June 2 8 . More L ike this c omment? [yes ] [no] (Sc ore: -1 by 1 vote) reply T ues day. 1 5 :5 7 :4 4 jo replies: Hi. 2 0 0 9 . More Axel L ike this c omment? [yes ] [no] (Sc ore: -1 by 1 vote) reply F riday. Save the file in a new folder in C :\Java.NoSuchMethodError: main" Without the quotations. 2 0 0 9 . 2 0 0 9 . June 3 0 . 0 2 :4 6 :3 8 bioingeniero replies: Maybe it´s because you didn´t change the directory to the directory where you have saved your file. 2 0 0 9 . 2 0 0 9 . July 0 1 .. July 0 3 .

I'm so close.now u r path is set.when u install jdk if u give any other path u have to give that only.. Does anybody know why we run program simply by calling HelloWorldApp (name of class?) and not by running HelloWorldApp..5. 2 0 0 9 . hard.com/docs/…/win32.. I figured out the rest of it. then u can check u r path whether its properply set by c:\java>echo %path%.u have done everything correct now.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. or what to do about it. Is there reference to HelloWorldApp created after compiling HelloWorldApp.java:5: error while writing HelloWorldApp: HelloWorldApp.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.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: T ues day. 1 9 :0 6 :0 8 Hi. July 1 4 .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.it will show u r path.7/22/2009 "Hello World!" for Microsoft Windo… that doesn't exist at all. 1 8 :1 9 :4 6 Guest replies: i can't do.then move to the next step.ok.java. I received the error message "javac is not recognized as an internal or external command" Please. July 0 2 . Thank you More L ike this c omment? [yes ] [no] reply T ues day.html 9/10 .class (Access is denied) class HelloWorldApp { ^ 1 error I'm not sure what that's supposed to mean.sun.class. help me i'm jane More L ike this c omment? [yes ] [no] (Sc ore: 1 by 3 votes ) reply Guest says: M onday. 2 0 0 9 . C :\Java>javac (u r filename.class file. 2 0 0 9 .here the path will change according to u r installation.exe.. 2 0 0 9 . 0 0 :2 6 :2 7 reply java. 0 7 :4 3 :4 8 When I compiled HelloWorldApp. I just can't switch it to a . 1 2 :5 0 :5 6 Skynila2007 replies: hey u have to set path u r path which contain javac. July 1 4 . July 1 3 . help.0_01\bin and enter. It's kind of sad. More L ike this c omment? [yes ] [no] 3 days ago.

com/docs/…/win32. More L ike this c omment? [yes ] [no] reply Guest says: hey thank you very much .7/22/2009 Melanie says: "Hello World!" for Microsoft Windo… 3 days ago. I guess my files shouldn't have been in Program Files. Took me a day. I moved them out of there and it worked.sun.html 10/10 . but I figured it out. 2 2 :4 3 :2 9 reply Leave a comment Controls (P owered by JS-Kit) java. it help me a lot More L ike this c omment? [yes ] [no] 3 days ago. 2 0 :0 3 :1 0 Okay. I got it.