Running Example Programs

Site: Saylor Academy
Course: CS101: Introduction to Computer Science I (2019.A.01)
Book: Running Example Programs
Printed by: Guest user
Date: Sunday, September 24, 2023, 7:27 PM


This chapter provides step-by-step instructions of writing a Java program using a text editor, and then compiling and running this program from command prompt. Several Integrated Development Environments (IDEs) are mentioned in this chapter. We have already installed NetBeans. That and Eclipse are the most popular in Java professional practice.

1. Running Example Programs in Windows

These web notes include many example programs. Run them and play with them. To learn programming, fiddle around with example programs and then write some of your own. This chapter shows how to do this in a bare-bones Windows environment.

This chapter repeats some of what was in the previous chapters but with additional detail. If you don't need the detail, skip the chapter.

You don't have to type in the programs to run them. If you can see the program in a browser window, then you can easily copy the program to Notepad (or other editor) and then save it to a file. Once that is done, you can compile and run the program as described in the previous chapter.

There are many ways to create, compile, and run Java programs. This chapter shows just one way to do this.

Chapter Topics:

      • How to copy, paste, compile, and run programs in a Windows 10 environment
      • Starting Notepad and the command prompt
      • Copying text to the clipboard
      • Pasting text from the clipboard to Notepad
      • Saving the text as a source file
      • Running a program

Question 1:

Is it sometimes beneficial to type in a program rather than just copying it?

Source: Bradley Kjell,
Creative Commons License This work is licensed under a Creative Commons Attribution-NonCommercial 3.0 License.

2. Notes


Yes — sometimes typing in a program forces you to see details you would otherwise miss.
Other times it is a waste of effort. Of course, not running the program at all is a
supreme waste of opportunity.


1. The steps are for Windows 10. Nearly the same steps can be used for other varieties of Windows. The software used in these notes is the minimum possible requirement, free on all Windows computers. Even if you have better software it is wise to start out using this minimum.

2. If you use an integrated programming environment (IDE) such as Eclipse or BlueJ you can copy programs into your environment and run them. But most IDEs require your program to be part of a "project" which takes some effort to set up. To quickly copy a program and run it, the steps described here are best.

3. Rather than use Notepad, you can use the Notepad++ programming editor. It is free, simple and easy to use, but it is a big improvement over Notepad. See

4. For writing big programs, a free Java IDE such as BlueJ may be your best choice. See

If you decide to use an IDE you will have to learn how to use it. For short programs, Notepad++ (or other editor) and the command line are easiest.

Question 2:

Can Microsoft Word be used as a program editor?

3. Step 1: Start Windows Explorer


Start Explorer by clicking on the folder icon in the taskbar at the bottom of the screen.

Step 1: Start Windows Explorer

Left-click on the little folder icon. File Explorer should start up.

(Alternative: click on the window icon at the very bottom left and look for the File Explorer icon.)


Exactly what you see depends on what you have previously done with Explorer. If you last used Explorer with the folder you use for programs, it will start up showing that folder.

You may see something like the above. You will have to "navigate" to the disk folder that holds your Java programs.

Click on various icons until you see the root (the top) of your hard disk.

Question 4:

What is the name of your hard disk?

4. Step 2: Navigate


Usually you want Java programs in a folder on the hard drive C:

Step 2: Navigate


Left-click on C: in the left panel of the Explorer window. (You can also left-click on a USB drive if you have one plugged in.)

You will see something like the above. A view of files and folders is in the right panel. You can click on one of them to go into it, or make a new folder.

To make a new folder, RIGHT-click on C: then move the mouse pointer over NEW and then FOLDER.


LEFT-click on NEW. A new folder is created and visible in the left panel of Explorer. The name of the folder is highlighted. Type in a name for the new folder (JavaSource is used here.) Hit "enter".


You should have a new folder (subdirectory). Double LEFT-Click on it.


The contents of the folder is displayed in the right panel of Explorer. The folder will be empty if you just created it.


It may take several tries to get this far if you are not familiar with Windows operations.

Question 5:

How do you start a command window for the folder?

5. Step 3: Start a command window and Notepad


SHIFT-right-click in the right panel

Step 3: Start a command window and Notepad


Put the mouse cursor in the right panel of the Explorer window. Make sure no file names are highlighted. Left-click in a blank space in the window to un-highlight any file. (If you just created the folder, there will be no files, so this is not needed.)

SHIFT-RIGHT-click in the window.

A menu will pop up. Click on "Open Power-shell window here". (Depending on your revision of Windows, this may be some other similar command window.)


The command prompt is the part PS C:\JavaSource>

In this minimalist environment, you enter commands to the OS by typing them after this prompt. To start Notepad, type notepad after the prompt. Hit "enter".


Question 6:

What is the clipboard in Windows?

6. Step 4: Copy the program into the clipboard


The clipboard in Windows (and other GUI environments) is a utility that allows you to copy
from one program (like a web browswer) and paste into another (like Notepad.)

Step 4: Copy the program into the clipboard

Copy the program from the Web browser into the clipboard.

You very likely have done this before by copying between Word documents or copying from the web into Word or other programs. This is the same. Copy the program (below) into the clipboard, then paste into Notepad.

Here is the example program, again:

public class HelloPlanets
  public static void main ( String[] args )
    String[] planets = {"Mercury", "Venus",  "Earth",   "Mars", "Jupiter", 
                        "Saturn",  "Uranus", "Neptune", "Pluto"};
    for ( int j=0; j< planets.length; j++ )
      System.out.println("Hello " + planets[j] +"!" );


Highlight the program by clicking the mouse on the first character, and dragging to the last character:

  1. In the browser window (this window), put the mouse pointer on the "p" of "program".
  2. Push down on the left mouse button. Without lifting up on the mouse button, drag down until the final "}" of the program is covered.
  3. Lift up on the mouse button.
  4. Put the mouse pointer into the highlighted program.
  5. RIGHT-click.
  6. Select "Copy". (Or hit control-C)

Be careful not to click the mouse again in the window or you will cancel your selection. (But you can always start over.) After you have done this, you should see something like the following:


Question 7:

Could you paste what you just copied into Notepad?

Could you paste what you just copied into Notepad++?

Could you paste what you just copied into BlueJ?

7. Step 5: Paste into Notepad



If you are using some other text editor (such as Notepad++) or an IDE (such as BlueJ) you can paste into it.

Step 5: Paste into Notepad

But for our minimal environment, continued with Notepad.

RIGHT-click in the Notepad window. A menu will appear. Select "Paste"

Or you can click in the window and hit control-V.

The program has been copied to Notepad.


Question 2:

How do you save the text in Notepad to a file?

8. Step 6: Save the Source File


Use the "Save As" menu option.

Step 6: Save the Source File


Click on the "File" menu in the upper left of Notepad. Select "Save-as".

The first time you do this, Notepad will show you some other folder than the one you want. You will need to "navigate" to the same folder you chose in step 2.

Save the example program as

(Of course, other programs will be saved to other files.)


Warning (1): In some older versions of Notepad you must use quote marks around the name of the file. So in the above you would type "" (quotes included) into the File name box.

Warning (2): Some versions of Notepad require Save as type All Files.

Warning (3): Be sure that you pick Encoding ANSI.

Question 9:

Hopefully, the file has been saved to the folder C:\Temp, the same
one that the command prompt window is using. How can you confirm that this indeed happened?

9. Step 7: Compile the Program


Click in the command window and enter the DIR command.

Step 7: Compile the Program

RUN: Compile and Run the program:
  1. Click in the command prompt window.
  2. Type DIR to check that is there.
  3. Compile the program: C:\JavaSource> javac
  4. Run the program: C:\JavaSource> java HelloPlanets


The DIR command lists the files in a DIRectory (also called "folder"). Enter the command (upper or lower case) to see that all the above steps did what you hope. You could also enter the command TYPE to type the text of the file on the screen.

After confirming that the file is there, compile it with the command


Enter a second DIR command to see that the compiler created a bytecode file, HelloPlanets.class

If there are error messages, go back to Notepad and try to correct the errors. Then use "Save" from the "File" menu to save the corrected file. Enter DIR again and check the time to verify that you are saving the file where you expect.

If you copied and pasted the example program, nothing should be wrong. Check that you did not clip off the start or end of the program. Check that you saved the file as "ANSI".

Question 10:

Are we there yet?

10. Step 7: Run the Program


You need to run the program. That is the point of all this.

Step 7: Run the Program

Enter the command

java HelloPlanets

to run the program. Actually, java starts the Java virtual machine, which then reads in the file of bytecode HelloPlanets.class, which the virtual machine starts executing.

Warning: The command is java with no "c" at the end.

Warning: Use the filename HelloPlanets with no ".class" at the end.


Well, that is one way to do it. Experienced users of Windows will be familiar with most of the steps. They will have found this process fairly easy. If you did not, it is merely a matter of practice (or finding a friend.)

Question 10:

Is Pluto a planet?

11. End of the Chapter


Once it was classified as a planet, and the program was correct.

Now, it is not, and the program is not correct.

End of the Chapter

It may seem strange that a correctly written program can become buggy without being altered. But this is actually common. Circumstances external to a program often change, so the program must be revised. Much of what professional programmers do is program maintainance: keeping programs up to date with changing requirements.

You have reached the end of the chapter. If you are unclear about it, take a break, and then try again. Or find a friend that knows how to do this, perhaps the same friend you annoyed at the end of the previous chapter. This stuff is much easier after you've done it a few times.

You may wish to review the following. Click on a subject that interests you to go to where it was discussed.

The next chapter will discuss the beginning elements of the Java language (at last!).