Hello World in Java on Windows (manual instructions)
This document instructs you on how to manually setup a Java programming environment for your Windows computer and provides a step-by-step guide for creating, compiling, and executing a Java program using either DrJava or the Command Prompt. All of the software is freely available on the Web.
These instructions are specialized to 64-bit Windows 8, but are similar for 32-bit and 64-bit versions of Windows 8, Windows 7, Vista, and XP.
You can defer steps 4–7 until Section 1.5 of the textbook.
We strongly recommend using our Windows installer instead of these manual instructions. The manual instructions do not include instructions for installing Checkstyle, Findbugs, or Java3D—none of these are required for COS 126.
|
You will use Oracle's (previously Sun's)
implementation of the Java Platform, Standard Edition Development
Kit (JDK 7).
Download the
Java SE 7 Update 67 to the Desktop:
- Click on Download JDK button next to Java SE 7 Update 67. Be sure to choose the JDK, not the JRE.
- Accept the Oracle Binary Code License Agreement for Java SE.
- Choose the Windows x64 product by clicking the link jdk-7u67-windows-x64.exe.
Once it is downloaded, install it by double clicking the file jdk-7u67-windows-x64.exe.
- We recommend all of the default options.
- Note the installation directory for later—probably something like C:\Program Files\Java\jdk1.7.0_67.
|
You will develop your Java programs in an application called
DrJava.
DrJava features many specialized programming tools
including syntax highlighting, bracket matching, auto indenting,
and line numbering. To install DrJava:
- Download the current stable release from drjava.org by clicking the Download Windows App button.
- Create a desktop icon or entry in the startup menu, as desired.
- If you receive a Windows Security Alert, click either Unblock or Allow Access.
- Display line numbers by selecting Edit -> 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.
|
Now you are ready to write your first Java program.
-
In the main DrJava window, type the Java program
HelloWorld.java exactly as it appears 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"); } }
- Finally, click the Save button to save the file. Use DrJava to create the folder C:\Users\username\introcs\hello and name the file HelloWorld.java. The file name is case sensitive and must exactly match the name of the class in the Java program.
|
It is now time to convert your Java program into a form more amenable for
execution on a computer. To do this, click the Compile button.
If all goes well, you should see the following message in the Compiler Output
pane at the bottom:
Compilation completed.
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.
|
Now it is time to run your program. This is the fun part.
-
Type the following in the Interactions pane at the bottom.
By convention, we highlight the text you type in boldface.
> java HelloWorld
Welcome to DrJava. Working directory is C:\Users\username\introcs\hello > java HelloWorld Hello, 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.
|
The command-line provides capabilities beyond those available in DrJava,
including redirection and piping.
You will type commands in an application called the Command Prompt.
- In order to run Java from the Command Prompt, you must add Java to
your PATH environment variable:
-
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. ]
- Prepend C:\Program Files\Java\jdk1.7.0_67\bin; to the beginning of the PATH variable. Depending on which version of Java you downloaded, the jdk1.7.0_67 part might be different.
- Click OK three times.
-
-
Launch the Command Prompt via
All Programs -> Accessories -> Command Prompt.
It will look something like:
Microsoft Windows [Version 6.3.9600] (c) 2013 Microsoft Corporation. All rights reserved.
-
To confirm that the Java compiler is installed,
type the command in boldface below and check that the output matches:
C:\Users\username>javac -version javac 1.7.0_67
-
To confirm that the Java interpreter is installed,
type the command in boldface below and check that the output matches:
C:\Users\username>java -version java version "1.7.0_67" Java(TM) SE Runtime Environment (build 1.7.0_67-b10) Java HotSpot(TM) 64-Bit Server VM (build 24.65-b04, mixed mode)
-
Since you will be using the Command Prompt frequently,
we recommend creating a desktop shortcut or entry in the startup menu.
Also, we recommend customizing the default settings: right-click
the title bar of an open Command Prompt window,
select Properties and, then
- Set Layout -> Screen Buffer Size to 80 x 500.
- Select Options -> Edit Options -> QuickEdit Mode.
- Select Options -> Edit Options -> Insert Mode.
|
You will use the javac command to convert your Java program into a form more
amenable for execution on a computer.
-
From the Command Prompt, navigate to the directory containing HelloWorld.java,
say C:\Users\username\introcs\hello, by typing the following cd
(change directory) command,
C:\Users\username>cd C:\Users\username\introcs\hello C:\Users\username\introcs\hello>
-
Compile it by typing the javac command below:
C:\Users\username\introcs\hello>javac HelloWorld.java C:\Users\username\introcs\hello>
|
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 HelloWorld Hello, World
|
To use our standard libraries, you need to download them and add them
both to your DrJava classpath and to your CLASSPATH environment variable.
- Download stdlib.jar to C:\Users\username\introcs\stdlib.jar.
- Add to your DrJava classpath:
- Select Preferences -> Resources -> Extra Classpath -> Add.
- Browse to find stdlib.jar.
- Add to your CLASSPATH environment variable:
- Select Start -> Computer -> System Properties -> Advanced system settings -> Environment Variables -> User variables -> CLASSPATH.
- Prepend C:\Users\username\introcs\stdlib.jar; to the beginning of the CLASSPATH variable.
- Click OK three times.
If you don't see a variable named CLASSPATH, click New and in the popup window enter CLASSPATH for the variable name. Then, perform the instructions above.
|
I have a 32-bit version 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 x86 as the Product (instead of Windows x64) when downloading the Java SE Update 67.