Hello World in Java on Windows
This document instructs you on how to set up our Java programming environment for your Windows computer. It also 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 used is freely available on the Web.
These instructions apply to 32-bit and 64-bit Windows 7, Vista SP1, and XP SP3.
You can defer steps 4–6 until Section 1.5 of the textbook.
We strongly recommend using the Windows installer described below; however, we do have manual instructions.
|
|
Our installer downloads, installs, and configures
the Java programming environment you will be using,
including Java SE 6, DrJava, and the standard libraries from our textbook.
- Log in to the user account in which you will be programming. Your account must have Administrator privileges and you must be connected to the Internet.
-
You will need a Java runtime.
Use this link to test if
the Java runtime is installed.
You should see something like Java SE 6 Update 27 or Java 6 Standard Edition;
if not, download and install a Java runtime from
java.com.
We recommend all of the default options except for allowing optional 3rd party installations.
Relaunch your browser for the changes to take effect.
- Download introcs.exe and double-click it to perform the installation. If you receive a User Account Control alert before the installation, click Yes or Allow; if you receive a Program Compatibility Assistant alert after the installation, click This program installed correctly.
-
If the installation succeeds, you will see the following:
- A Command Prompt windows containing approximately this execution log.
- A Standard Drawing window containing a red bullseye and textbook graphic.
- Delete introcs.exe.
|
|
Now you are ready to write your first Java program.
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.
- The installer creates a shortcut on the desktop to DrJava. Double-click it to launch DrJava. If you receive a Windows Security Alert, click either Unblock or Allow Access.
-
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.
As you type, DrJava does the indenting for you.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.
If all goes well, you should see the following message:> 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.
-
The installer creates a shortcut on the desktop to the Command Prompt.
Double-click it to launch the Command Prompt.
You should see 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 below and check that the output matches:
C:\Users\username>javac -version javac 1.6.0_27
-
To confirm that the Java interpreter is installed,
type the command in boldface below and check that the output matches:
If you have a 32-bit machine, the last line will be:C:\Users\username>java -version java 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)
Java HotSpot(TM) Client VM (build 20.2-b06, mixed mode, sharing)
|
|
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 cd (change directory) commands below:
C:\Users\username>cd introcs C:\Users\username\introcs>cd hello C:\Users\username\introcs\hello>
-
Compile it by typing the javac command below:
Assuming the file HelloWorld.java is in the current working directory, you should see no error messages.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:
You should see the output of the program.C:\Users\username\introcs\hello>java HelloWorld Hello, World
|
|
Listed below are some common questions about this document.
If you encounter errors that you can't figure out, ask for help.
When running the installer, I get an access denied error message. What should I do? Make sure you are using an administrator account. Rerun the installer.
The installer doesn't execute. Why? The installer requires PowerShell. Run Windows Update to update your computer. If you are running Windows XP, you must have Service Pack 3. You can manually download PowerShell 2.0 for Windows XP or Vista by clicking the corresponding link under Windows Management Framework Core (WinRM 2.0 and Windows PowerShell 2.0).
The installer got stuck after selecting some text, but there are no error messages? The Command Prompt may have gone into Select Mode, which pauses the installation until you press Enter twice to exit that mode.
The installer didn't work on my machine. What should I do? First, please contact a staff member to identify what went wrong.
What does the installer do? In short, it downloads, installs, and configures Java, Java3D, DrJava, Checkstyle, Findbugs, and the standard libraries. Here is a more detailed list:
- Downloads and installs Java SE 6, Update 27 from either java32.zip or java64.zip. This includes the Java Runtime Environment and part of the Java Development Kit (javac.exe and tools.jar). Adds Java to the PATH environment variable.
- Downloads and installs Java3D 1.5.2 from either j3d32.zip or j3d64.zip. Adds Java3D to the PATH and CLASSPATH environment variables.
- Downloads the textbook standard libraries from stdlib.jar. Adds the standard libraries to the CLASSPATH environment variable.
- Downloads and installs Checkstyle 5.4 from checkstyle.zip. Downloads our checkstyle configuration file from checkstyle.xml.
- Downloads and installs Findbugs 1.3.9 from findbugs.zip. Downloads our findbugs configuration file from findbugs.xml.
- Downloads and installs the latest stable version of DrJava, from drjava.exe. Creates a shortcut on the desktop to DrJava. Downloads and installs the DrJava configuration file from drjava-config.txt. to C:\\Users\username\.drjava. Note that this will overwrite any existing .drjava configuration file.
- Customizes the Command Prompt preferences by enabling QuickEdit and Insert modes and setting the Screen Buffer Size to 80-by-500. Creates a shortcut on the desktop to the Command Prompt.
- Tests that the installation succeeded by compiling and executing TestIntroCS.java.
How do I uninstall introcs.exe?
- Delete your C:\Users\username\introcs folder (but save any of the .java files you created, if desired).
- Delete these additions from your CLASSPATH environment variable.
- C:\Users\username\introcs\stdlib.jar
- C:\Users\username\introcs\algs4.jar
- C:\Users\username\introcs\j3d\lib\ext\j3dcore.jar
- C:\Users\username\introcs\j3d\lib\ext\j3dutils.jar
- C:\Users\username\introcs\j3d\lib\ext\vecmath.jar
- Delete these additions from your PATH environment variable.
- C:\Users\username\introcs\bin
- C:\Users\username\introcs\java\bin
- Delete the DrJava configuration file C:\Users\username\.drjava.
- Delete the shortcuts to DrJava and the Command Prompt.
What happens if I rerun the installer? It will re-download, install, and configure Java, Java 3D, Checkstyle, Findbugs, DrJava, and the standard libraries.
I couldn't get a Java runtime to install. Any advice? Try downloading Java using the Windows 7, XP offline installer.
What should I do if I have previously installed DrJava in another location? We suggest deleting it and using the version in C:\Users\username\introcs by using the newly created DrJava shortcut.
Why Java 6 instead of Java 7? The latest stable version of DrJava does not support Java 7.
Can I use a different version of Java? Yes, but you will have to configure the Windows environment variables (PATH and CLASSPATH) and DrJava compiler properties yourself.
Can I update DrJava to use a newer version? Yes, but you do not need to do so.
Can I use an IDE other than DrJava? Yes you can use another IDE (such as Eclipse) but you will have to configure the IDE properties yourself (such as the classpath). If you have a 64-bit version of Java, install the 64-bit version of Eclipse.
When I launch the Eclipse IDE, I get a "Failed to load the JNI shared library" error. How can I fix this? You probably have a 64-bit version of Java and a 32-bit version of Eclipse. The installer installs a 64-bit version of Java if you have a 64-bit machine, which takes precedent over a previously installed 32-bit version of Java. To correct, either update to a 64-bit version of Eclipse or remove the C:\Users\username\introcs\java folder so that Windows uses your previously installed 32-bit version of Java. You can also specify which version of Java to use in the Eclipse.ini file.
How can I determine whether I am running a 32- or 64-bit version of Windows? Examine Start -> My computer -> Properties -> System. If you see x64 Edition, you are running the 64-bit version of Windows; otherwise, you are running the 32-bit version.
How do I break out of an infinite loop? In DrJava, click the Reset button in the menubar or select the menu option Tools -> Reset Interactions. From the Command Prompt, type Ctrl-c
When using standard input, how do I signify that there is no more data? If you are entering input from the keyboard, type Ctrl-z for EOF (end of file).
How do I configure Windows to reveal the .java or .class file extensions? Many Windows machines are configured to hide the file extensions. If this is the case, select Start -> My Computer -> Tools -> Folder Options -> View, uncheck the box next to Hide file extensions for known file types, and click OK.
I deleted the shortcut to the Command Prompt. Where can I find it? Go to All Programs -> Accessories -> Command Prompt.
When I type, "java -version" or "javac -version" from the Command Prompt, I get an error. First, verify that the following two files exists:
C:\Users\username\introcs\java\jdk\bin\javac.exe
C:\Users\username\introcs\java\jdk\bin\java.exe
If so, it is likely an issue with the Windows PATH environment variable. From the Command Prompt, type the following command to display it:
The PATH environment variable should include an entry for C:\Users\username\introcs\java\jdk\bin.
C:\Users\username> echo %PATH%
I successfully compiled HelloWorld.java with javac, but, when I execute, I get the error message "Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld". What am I doing wrong? First, verify that the file HelloWorld.class is in the current directory. Be sure to type java HelloWorld without a trailing .class or .java.
When I compile or execute a program from the Command Prompt that uses StdIn, I get an error. How can I fix this? First, verify that the following file exists:
C:\Users\username\introcs\stdlib.jarIf so, it is likely an issue with the Windows CLASSPATH environment variable. From the Command Prompt, type the following command to display it:
The CLASSPATH environment variable should include an entry for C:\Users\username\introcs\stdlib.jar.
C:\Users\username> echo %CLASSPATH%
How do I navigate to another drive from the Windows Command Prompt? From the Command Prompt, type H: to switch to the H: drive. Then, use the cd command to navigate to the desired directory.
Where can I learn more about Command Prompt? Microsoft maintains a command-line reference.
Can I use the Windows PowerShell instead of the Command Prompt?
The Windows PowerShell is a more advanced command-line shell.
However, it does not currently support the redirection of standard input.
