"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

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

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

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

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

consult the Common Problems (and Their Solutions).class file Now that you have a . 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.class file.com/docs/…/win32. showing the generated .html 6/10 . top Run the Program In the same directory. Congratulations! Your program works! If you encounter problems with the instructions in this step. top java. If you encounter problems with the instructions in this step.7/22/2009 "Hello World!" for Microsoft Windo… Directory listing. you can run your program. consult the Common Problems (and Their Solutions).sun.

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

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

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

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

Sign up to vote on this title
UsefulNot useful