Hello World in Java on Windows

download Hello World in Java on Windows

of 6

Transcript of Hello World in Java on Windows

  • 8/3/2019 Hello World in Java on Windows

    1/6

    Hello World in Java on Windows (manual

    instructions)

    This document instructs you on how to manually setup a Java programming environment foryour Windows computer and provides a step-by-step guide for creating, compiling, andexecuting a Java program. All of the software is freely available on the Web.

    These instructions are specialized to 64-bit Windows 7, but are similar for 32-bit and 64-bitversions of Windows 7, Vista, and XP.

    You can defer steps 47 until Section 1.5 of the textbook.

    We strongly recommend using ourWindows installerinstead of performing these instructionsmanually.

    0a. Install Java

    You will use Oracle's (previously Sun's) implementation of the Java Platform, StandardEdition Development Kit (JDK 6). Download theJava SE 6 Update 27to the Desktop:

    Click onDownload JDKbutton next toJava SE 6 Update 27. Be sure to choose theJDK, not the JRE. Also, be sure to choose Java SE 6, not Java SE 7. (You don't needthe Java EE or NetBeans bundles.)

    Accept the Oracle Binary Code License Agreemment for Java SE. Choose the Windows x64 product by clicking the linkjdk-6u27-windows-x64.exe.

    Once it is downloaded, install it by double clicking the file jdk-6u27-windows-x64.exe.

    We recommend all of the default options. Note the installation directory for laterprobably something like C:\Program

    Files\Java\jdk1.6.0_27.

    0b. Install DrJava

    You will develop your Java programs in an application calledDrJava. DrJava features manyspecialized programming tools including syntax highlighting, bracket matching, autoindenting, and line numbering. To install DrJava:

    http://introcs.cs.princeton.edu/java/windowshttp://introcs.cs.princeton.edu/java/windowshttp://introcs.cs.princeton.edu/java/windowshttp://www.oracle.com/technetwork/java/javase/downloads/index.htmlhttp://www.oracle.com/technetwork/java/javase/downloads/index.htmlhttp://www.oracle.com/technetwork/java/javase/downloads/index.htmlhttp://www.oracle.com/technetwork/java/javase/downloads/index.htmlhttp://introcs.cs.princeton.edu/java/windows
  • 8/3/2019 Hello World in Java on Windows

    2/6

    Download the current stable release fromdrjava.orgby clicking theDownloadWindows App button.

    Create a desktop icon or entry in the startup menu, as desired.Launch DrJava and make the following customizations:

    If you receive a Windows Security Alert, click either UnblockorAllow Access. Display line numbers by selectingEdit -> Preferences -> Display Options -> Show

    All Line Numbers. Set the indentation level to 4 by selecting Preferences -> Miscellaneous -> Indent

    Level -> 4. Turn off emacs style backups by unchecking Preferences -> Miscellaneous -> Keep

    emacs-style Backup Files.

    1. Create the Program in DrJava

    Now you are ready to write your first Java program.

    In the main DrJava window, type the Java programHelloWorld.javaexactly as itappears below. If you omit even a semicolon, the program won't work.

    public class HelloWorld {public static void main(String[] args) {

    System.out.println("Hello, World");}

    }

    As you type, DrJava does the indenting for you.

    Finally, click the Save button to save the file. Use DrJava to create the folderC:\Users\username\introcs\helloand name the file HelloWorld.java. The filename is case sensitive and must exactly match the name of the class in the Javaprogram.

    2. Compile the Program from DrJava

    It is now time to convert your Java program into a form more amenable for execution on acomputer. To do this, click the Compile button. If all goes well, you should see the followingmessage in the Compiler Outputpane at the bottom:

    Compilation completed.

    http://www.drjava.org/http://www.drjava.org/http://www.drjava.org/http://introcs.cs.princeton.edu/java/11hello/HelloWorld.java.htmlhttp://introcs.cs.princeton.edu/java/11hello/HelloWorld.java.htmlhttp://introcs.cs.princeton.edu/java/11hello/HelloWorld.java.htmlhttp://introcs.cs.princeton.edu/java/11hello/HelloWorld.java.htmlhttp://www.drjava.org/
  • 8/3/2019 Hello World in Java on Windows

    3/6

    If DrJava complains in some way, you mistyped something. Check your program carefully,using the error messages in the Compiler Output pane as a guide.

    3. Execute the Program from DrJava

    Now it is time to run your program. This is the fun part.

    Type the following in theInteractions pane at the bottom. By convention, wehighlight the text you type in bold.

    > java HelloWorld

    If all goes well, you should see the following message:

    Welcome to DrJava. Working directory isC:\Users\username\introcs\hello> java HelloWorldHello, World

    You may need to repeat this edit-compile-execute cycle a few times before it works.Ask for help if you can't see the mistake.

    Congratulations, you are now a Java programmer!

    4. Command-Line Interface

    The command-line provides capabilities beyond those available in DrJava, includingredirection and piping. You will type commands in an application called the CommandPrompt.

    In order to run Java from the Command Prompt, you must add Java to your PATHenvironment variable:

    o Select Start -> Computer -> System Properties -> Advanced system settings -> Environment Variables -> System variables -> PATH.

    [ In Vista, select Start -> My Computer -> Properties -> Advanced ->Environment Variables -> System variables -> PATH. ]

    [ In Windows XP, Select Start -> Control Panel -> System -> Advanced ->Environment Variables -> System variables -> PATH. ]

  • 8/3/2019 Hello World in Java on Windows

    4/6

    o Prepend C:\Program Files\Java\jdk1.6.0_27\bin;to the beginning ofthe PATH variable. Depending on which version of Java you downloaded, the

    jdk1.6.0_27 part might be different.o Click OK three times.

    Launch the Command Prompt viaAll Programs -> Accessories -> Command Prompt.It will look something like:

    Microsoft Windows [Version 6.1.7601]Copyright (c) 2009 Microsoft Corporation. All rights reserved.

    To confirm that the Java compiler is installed, type the command in boldface belowand check that the output matches:

    C:\Users\username>javac -versionjavac 1.6.0_27

    To confirm that the Java interpreter is installed, type the command in boldface belowand check that the output matches:

    C:\Users\username>java -versionjava version "1.6.0_27"Java(TM) SE Runtime Environment (build 1.6.0_27-b07)Java HotSpot(TM) 64-Bit Server VM (build 20.2-b06, mixed mode)

    If you have a 32-bit machine, the last line will be:

    Java HotSpot(TM) Client VM (build 20.2-b06, mixed mode, sharing)

    Since you will be using the Command Prompt frequently, we recommend creating adesktop shortcut or entry in the startup menu. Also, we recommend customizing thedefault settings: right-click the title bar of an open Command Prompt window, selectProperties and, then

    o SetLayout -> Screen Buffer Size to 80 x 500.o Select Options -> Edit Options -> QuickEdit Mode.o Select Options -> Edit Options -> Insert Mode.

    5. Compile the Program From the Command Prompt

    You will use the javac command to convert your Java program into a form more amenablefor execution on a computer.

    From the Command Prompt, navigate to the directory containing HelloWorld.java,say C:\Users\username\introcs\hello, by typing the cd (change directory)comman$

    C:\Users\username>cd introcs

  • 8/3/2019 Hello World in Java on Windows

    5/6

    C:\Users\username\introcs>cd helloC:\Users\username\introcs\hello>

    Compile it by typing the javac command below:C:\Users\username\introcs\hello>javac HelloWorld.javaC:\Users\username\introcs\hello>

    Assuming the file HelloWorld.javais in the current working directory, you shouldsee no error messages.

    6. Execute the Program from the Command Prompt

    You will use the java command to execute your program.

    From the Command Prompt, type the java command below:C:\Users\username\introcs\hello>java HelloWorldHello, World

    You should see the output of the program.

    7. Install the Standard Libraries

    To use our standard libraries, you need to download them and add them both to your DrJavaclasspath and to your CLASSPATH environment variable.

    Downloadstdlib.jarto C:\Users\username\introcs\stdlib.jar. Add to your DrJava classpath:

    o Select Preferences -> Resources -> Extra Classpath -> Add.o Browse to find stdlib.jar.

    Add to your CLASSPATH environment variable:o Select Start -> Computer -> System Properties -> Advanced system settings -

    > Environment Variables -> User variables -> CLASSPATH.

    o Prepend C:\Users\username\introcs\stdlib.jar;to the beginning of theCLASSPATH variable.

    o Click OK three times.If you don't see a variable named CLASSPATH, clickNew and in the popup windowenter CLASSPATH for the variable name. Then, perform the instructions above.

    http://introcs.cs.princeton.edu/java/stdlib/stdlib.jarhttp://introcs.cs.princeton.edu/java/stdlib/stdlib.jarhttp://introcs.cs.princeton.edu/java/stdlib/stdlib.jarhttp://introcs.cs.princeton.edu/java/stdlib/stdlib.jar
  • 8/3/2019 Hello World in Java on Windows

    6/6

    Troubleshooting

    Can I use Java SE 7 instead of Java SE 6? No, the latest stable version of DrJava does not

    currently support Java 7.

    I have a 32-bit verison of Windows. What should I do?

    To identify which version of Windows you are running, select Start -> Control Panel-> System and Security -> System -> System Type.

    If you are running a 32-bit version of Windows, select Windows x86as the Product(instead ofWindows x64) when downloading theJava SE Update 27.

    How do I break out of an infinite loop in DrJava? Click the Reset button in the menubaror select the menu option Tools -> Reset Interactions.

    How can I convince Windows to reveal the .java or .class file extensions? Many Windowsmachines are configured to hide the file extensions. To override this default, select Start ->

    My Computer -> Tools -> Folder Options -> View, uncheck the box next toHide fileextensions for known file types, and click OK.

    http://www.oracle.com/technetwork/java/javase/downloads/index.htmlhttp://www.oracle.com/technetwork/java/javase/downloads/index.htmlhttp://www.oracle.com/technetwork/java/javase/downloads/index.htmlhttp://www.oracle.com/technetwork/java/javase/downloads/index.html