[ Previous Section | Chapter Index | Main Index ]

Section 2.6

Programming Environments


Although the Java language is highly standardized, the procedures for creating, compiling, and editing Java programs vary widely from one programming environment to another. There are two basic approaches: a command line environment, where the user types commands and the computer responds, and an integrated development environment (IDE), where the user uses the keyboard and mouse to interact with a graphical user interface. While there is just one common command line environment for Java programming, there are several common IDEs, including Eclipse, NetBeans, and BlueJ. I cannot give complete or definitive information on Java programming environments in this section, but I will try to give enough information to let you compile and run the examples from this textbook. (Readers are strongly encouraged to read, compile, and run the examples. Source code for sample programs and solutions to end-of-chapter exercises can be downloaded from the book's web page, http://math.hws.edu/javanotes.)

One thing to keep in mind is that you do not have to pay any money to do Java programming (aside from buying a computer, of course). Everything that you need can be downloaded for free on the Internet.


2.6.1  Java Development Kit

The basic development system for Java programming is usually referred to as a JDK (Java Development Kit). It is a part of Java SE, the Java "Standard Edition." Note that Java SE comes in two versions, a Development Kit version (the JDK) and a Runtime Environment version (the JRE). The Runtime can be used to run Java programs, but it does not allow you to compile your own Java programs. The Development Kit includes the Runtime but also lets you compile programs. You need a JDK for use with this textbook.

If a JDK is properly installed on your computer, you can use the command line environment to compile and run Java programs. An IDE will also require a JDK, but it might be included with the IDE download.

Java was developed by Sun Microsystems, Inc., which was acquired by the Oracle corporation. Oracle makes the JDK for Windows, Mac OS, and Linux available for free download at its Java Web site. Some computers might come with a Java Runtime already installed, but you will probably need to install the JDK in any case. For most Linux distributions, a version known as OpenJDK is available for installation as a package. As of summer 2018, the official Oracle versions are available at

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

You will need a JDK for Java 8 or later for use with this textbook. The sample programs for this textbook have been compiled with Java 8 and with Java 10.

About JavaFX: The Graphical User Interface programs in this book use the JavaFX GUI toolkit. JavaFX is included in the JDKs for Java 8, 9, and 10 from Oracle Corporation, but Oracle has announced that it will not be included in Java 11 and later; instead, it will be available as a separate download. For the alternative OpenJDK, JavaFX has never been included as a standard component, but is available separately as OpenJFX. As I write this, it is only available for OpenJDK 8, but a version for OpenJDK 9 through 11 is under development. On my Ubuntu Linux desktop computer using Java 8, I had to install an "openjfx" package in addition to the "openjdk" package. All of the examples and exercise solutions for this book were developed in that environment, and they were also compiled using JDK 10 from Oracle.


2.6.2  Command Line Environment

Many modern computer users find the command line environment to be pretty alien and unintuitive. It is certainly very different from the graphical user interfaces that most people are used to. However, it takes only a little practice to learn the basics of the command line environment and to become productive using it.

To use a command line programming environment, you will have to open a window where you can type in commands. In Windows, you can open such a command window by running a program named cmd or PowerShell. In Mac OS, you want to run the Terminal program, which can be found in the Utilities folder inside the Applications folder. In Linux, there are several possibilities, including an old program called xterm; try looking for "Terminal" in your Applications menu.

No matter what type of computer you are using, when you open a command window, it will display a prompt of some sort. Type in a command at the prompt and press return. The computer will carry out the command, displaying any output in the command window, and will then redisplay the prompt so that you can type another command. One of the central concepts in the command line environment is the current directory which contains files that can be used by the commands that you type. (The words "directory" and "folder" mean the same thing.) Often, the name of the current directory is part of the command prompt. You can get a list of the files in the current directory by typing in the command dir (on Windows) or ls (on Linux and Mac OS). When the window first opens, the current directory is your home directory, where all your files are stored. You can change the current directory using the cd command with the name of the directory that you want to use. For example, to change into your Desktop directory, type in the command cd Desktop and press return.

You should create a directory (that is, a folder) to hold your Java work. For example, create a directory named javawork in your home directory. You can do this using your computer's GUI; another way to do it is to open a command window, cd to the directory that you want to contain the new directory, and enter the command mkdir javawork. When you want to work on programming, open a command window and use the cd command to change into your work directory. Of course, you can have more than one working directory for your Java work; you can organize your files any way you like.


The most basic commands for using Java on the command line are javac and java; javac is used to compile Java source code, and java is used to run Java programs. If a JDK is correctly installed on your computer, it should recognize these commands when you type them on the command line. Try typing the commands java -version and javac -version which should tell you which version of Java is installed. If you get a message such as "Command not found," then Java is not correctly installed. If the "java" command works, but "javac" does not, it means that a Java Runtime is installed rather than a Development Kit. (On Windows, after installing the JDK, you need to modify the Windows PATH environment variable to make this work. See the JDK installation instructions on Oracle's download site for information about how to do this.)

To test the javac command, place a copy of HelloWorld.java into your working directory. (If you downloaded the Web site of this book, you can find it in the directory named source; you can use your computer's GUI to copy-and-paste this file into your working directory. Alternatively, you can navigate to HelloWorld.java on the book's Web site and use the "Save As" command in your Web browser to save a copy of the file into your working directory.) Type the command:

javac  HelloWorld.java

This will compile HelloWorld.java and will create a bytecode file named HelloWorld.class in the same directory. Note that if the command succeeds, you will not get any response from the computer; it will just redisplay the command prompt to tell you it's ready for another command. You will then be able to run the program using the java command:

java  HelloWorld

The computer should respond by outputting the message "Hello World!". Note that although the program is stored in a file named HelloWorld.class, the java command uses the name of the class, HelloWorld, not the name of the file.

Many of the sample programs for this book use TextIO to read input from the user (see Subsection 2.4.3). Since TextIO is not a standard part of Java, you must make it available to any program that uses it. This means that your working directory should contain a folder named textio, and inside that folder should be the file TextIO.java. You can copy TextIO.java from this book's source directory, or your can download it from the web site, but you should be sure to place it inside a folder named textio.

Once you have TextIO.java you can run a sample program such as Interest2.java to test user input. First, compile the program with the command

javac  Interest2.java

If successful, this will create the compiled file named Interest2.class. But you will also notice that it creates the file TextIO.class inside the textio folder, if that file does not already exist. More generally, the javac command will compile not just the file that you specify but also any additional Java files that are needed. Once you have Interest2.class, you can run it using the command

java  Interest2

You will be asked to enter some information, and you will respond by typing your answers into the command window, pressing return at the end of each line. When the program ends, you will see the command prompt, and you can enter another command. (Note, by the way, that "java TextIO" would not make sense, since TextIO does not have a main() routine, and so it doesn't make sense to try to execute it as a program.)

You can follow a similar procedure to run any of the examples in this book. Some examples require additional files, such as TextIO.java, which also need to be in your working directory.


To create your own programs, you will need a text editor. A text editor is a computer program that allows you to create and save documents that contain plain text. It is important that the documents be saved as plain text, that is without any special encoding or formatting information. Word processor documents are not appropriate, unless you can get your word processor to save as plain text. A good text editor can make programming a lot more pleasant. Linux comes with several text editors. On Windows, you can use notepad in a pinch, but you will probably want something better. For Mac OS, you might download the BBEdit application, which can be used for free. One possibility that will work on any platform is to use jedit, a programmer's text editor that is itself written in Java and that can be downloaded for free from www.jedit.org.

To work on your programs, you can open a command line window and cd into the working directory where you will store your source code files. Start up your text editor program, such as by double-clicking its icon or selecting it from a Start menu. Type your code into the editor window, or open an existing source code file that you want to modify. Save the file into your working directory. Remember that the name of a Java source code file must end in ".java", and the rest of the file name must match the name of the class that is defined in the file. Once the file is saved in your working directory, go to the command window and use the javac command to compile it, as discussed above. If there are syntax errors in the code, they will be listed in the command window. Each error message contains the line number in the file where the computer found the error. Go back to the editor and try to fix one or more errors, save your changes, and then try the javac command again. (It's usually a good idea to just work on the first few errors; sometimes fixing those will make other errors go away.) Remember that when the javac command finally succeeds, you will get no message at all, or possibly just some "warnings" that do not stop the program from running. Then you can use the java command to run your program, as described above. Once you've compiled the program, you can run it as many times as you like without recompiling it.

That's really all there is to it: Keep both editor and command-line window open. Edit, save, and compile until you have eliminated all the syntax errors. (Always remember to save the file before compiling it—the compiler only sees the saved file, not the version in the editor window.) When you run the program, you might find that it has semantic errors that cause it to run incorrectly. In that case, you have to go back to the edit/save/compile loop to try to find and fix the problem.


2.6.3  Eclipse

In an Integrated Development Environment, everything you need to create, compile, and run programs is integrated into a single package, with a graphical user interface that will be familiar to most computer users. There are a number of different IDEs for Java program development, ranging from fairly simple wrappers around the JDK to highly complex applications with a multitude of features. For a beginning programmer, there is a danger in using an IDE, since the difficulty of learning to use the IDE, on top of the difficulty of learning to program, can be overwhelming. However, for my own programming, I generally use the Eclipse IDE, and I introduce my students to it after they have had some experience with the command line. I will discuss Eclipse in some detail and two other IDEs, NetBeans and BlueJ, in much less detail. All of these IDEs have features that are very useful even for a beginning programmer, although a beginner will want to ignore many of their advanced features.

You can download an Eclipse IDE from eclipse.org. It is a free program. Eclipse is itself written in Java. It requires a Java Runtime Environment, but not necessarily a JDK, since it includes its own compiler. You should make sure that the JRE or JDK, Version 8 or higher, is installed on your computer, as described above, before you install Eclipse. There are several versions of the Eclipse IDE; you can use the "Eclipse IDE for Java Developers."

The first time you start Eclipse, you will be asked to specify a workspace, which is the directory where all your work will be stored. You can accept the default name, or provide one of your own. When startup is complete, the Eclipse window will be filled by a large "Welcome" screen that includes links to extensive documentation and tutorials. You should close this screen, by clicking the "X" next to the word "Welcome"; you can get back to it later by choosing "Welcome" from the "Help" menu.

The Eclipse GUI consists of one large window that is divided into several sections. Each section contains one or more views. For example, a view can be a text editor, it can be a place where a program can do I/O, or it can contain a list of all your projects. If there are several views in one section of the window, then there will be tabs at the top of the section to select the view that is displayed in that section. Each view displays a different type of information. The whole set of views is called a perspective. Eclipse uses different perspectives, that is, different sets of views of different types of information, for different tasks. For compiling and running programs, the only perspective that you will need is the "Java Perspective," which is the default. As you become more experienced, you might want to the use the "Debug Perspective," which has features designed to help you find semantic errors in programs.

The Java Perspective includes a large area in the center of the window that contains text editor views. This is where you will create and edit your programs. To the left of this is the Package Explorer view, which will contain a list of your Java projects and source code files. To the right are some other views that I don't find very useful; I suggest that you close them by clicking the small "X" next to the name of each one. Several other views that will be useful appear in a section of the window below the editing area. If you accidently close one of the important views, such as the Package Explorer, you can get it back by selecting it from the "Show View" submenu of the "Window" menu. You can also reset the whole window to its default contents by selecting "Reset Perspective" from the "Window" menu.


To do any work in Eclipse, you need a project. To start a Java project, go to the "New" submenu in the "File" menu, and select the "Java Project" command. In the window that pops up, it is only necessary to fill in a "Project Name" for the project and click the "Finish" button. The project name can be anything you like. The project should appear in the "Package Explorer" view. Click on the small triangle or plus sign next to the project name to see the contents of the project. Assuming that you use the default settings, there should be a directory named "src," which is where your Java source code files will go. It also contains the "JRE System Library"; this is the collection of standard built-in classes that come with Java.

To run any of the sample Java programs from this textbook, you need to copy the source code file into your Eclipse Java project. Assuming that you have downloaded the source code file onto your computer, you can copy-and-paste it into the Eclipse window. (Right-click the file icon, or control-click on Mac OS, select "Copy" from the pop-up menu, then right-click the project's src folder in the Eclipse window, and select "Paste". Be sure to paste it into the src folder, not into the project itself; files outside the src folder are not treated as Java source code files.) Alternatively, you can try dragging the file icon onto the src folder in the Eclipse project.

To use the TextIO based examples from this textbook, you must add the source code file TextIO.java to your project. This file has to be in a "package" named textio. If you have downloaded TextIO.java and placed it into a folder named "textio," as described above, then you can simply copy-and-paste the textio folder into the "src" folder of your project. Alternatively, you can create the textio package using the "New/Package" command from the "File" menu. This will make a folder named "textio" in your project, and you can then copy-and-paste TextIO.java into that folder. In any case, package textio should appear under "src" in your project, with TextIO.java inside it.

Once a Java program is in the project, you can open it in an editor by right-clicking the file name in the "Package Explorer" view. To run the program, right-click in the editor window, or on the file name in the Package Explorer view (or control-click in Mac OS). In the menu that pops up, go to the "Run As" submenu, and select "Java Application". The program will be executed. If the program writes to standard output, the output will appear in the "Console" view, in the area of the Eclipse window below the editing area. If the program uses TextIO or Scanner for input, you will have to type the required input into the "Console" view—click the "Console" view before you start typing, so that the characters that you type will be sent to the correct part of the window. (For an easier way to run a program, find and click the small "Run" button in Eclipse's tool bar.) Note that when you run a program in Eclipse, it is compiled automatically. There is no separate compilation step.

You can have more than one program in the same Eclipse project, or you can create additional projects to organize your work better. Remember to place a copy of TextIO.java, inside a folder named textio, in any project that requires it.


To create a new Java program in Eclipse, you must create a new Java class. To do that, right-click the Java project name in the "Project Explorer" view. Go to the "New" submenu of the popup menu, and select "Class". (Alternatively, there is a small icon in the toolbar at the top of the Eclipse window that you can click to create a new Java class.) In the window that opens, type in the name of the class that you want to create. The class name must be a legal Java identifier. Note that you want the name of the class, not the name of the source code file, so don't add ".java" at the end of the name. The window also includes an input box labeled "Package" where you can specify the name of a package to contain the class. Most examples in this book use the "default package," but you can create your own programs in any package. To use the default package, the "Package" input box should be empty. Finally, click the "Finish" button to create the class. The class should appear inside the "src" folder, in a folder corresponding to its package. The new file should automatically open in the editing area so that you can start typing in your program.

Eclipse has several features that aid you as you type your code. It will underline any syntax error with a jagged red line, and in some cases will place an error marker in the left border of the edit window. If you hover the mouse cursor over the error marker or over the error itself, a description of the error will appear. Note that you do not have to get rid of every error immediately as you type; some errors will go away as you type in more of the program. If an error marker displays a small "light bulb," Eclipse is offering to try to fix the error for you. Click the light bulb—or simply hover your mouse over the actual error—to get a list of possible fixes, then click the fix that you want to apply. For example, if you use an undeclared variable in your program, Eclipse will offer to declare it for you. You can actually use this error-correcting feature to get Eclipse to write certain types of code for you! Unfortunately, you'll find that you won't understand a lot of the proposed fixes until you learn more about the Java language, and it is not a good idea to apply a fix that you don't understand—often that will just make things worse in the end.

Eclipse will also look for spelling errors in comments and will underline them with jagged red lines. Hover your mouse over the error to get a list of possible correct spellings.

Another essential Eclipse feature is content assist. Content assist can be invoked by typing Control-Space. It will offer possible completions of whatever you are typing at the moment. For example, if you type part of an identifier and hit Control-Space, you will get a list of identifiers that start with the characters that you have typed; use the up and down arrow keys to select one of the items in the list, and press Return or Enter. (You can also click an item with the mouse to select it, or hit Escape to dismiss the list.) If there is only one possible completion when you hit Control-Space, it will be inserted automatically. By default, Content Assist will also pop up automatically, after a short delay, when you type a period or certain other characters. For example, if you type "TextIO." and pause for just a fraction of a second, you will get a list of all the subroutines in the TextIO class. Personally, I find this auto-activation annoying. You can disable it in the Eclipse Preferences. (Look under Java / Editor / Content Assist, and turn off the "Enable auto activation" option.) You can still call up Code Assist manually with Control-Space.

Once you have an error-free program, you can run it as described above. If you find a problem when you run it, it's very easy to go back to the editor, make changes, and run it again.


About JavaFX and Eclipse: Eclipse will work with JavaFX, the GUI toolkit that is used in this textbook. However, you might want to install a plugin named efxclipse for better support. See www.eclipse.org/efxclipse/ for information. If, like me, you are using Eclipse with OpenJDK 8 and OpenJFX, you might get an error when you try to use OpenJFX classes and methods. This is because OpenJFX is installed as an "extension," and Eclipse doesn't like to run extension code. To get rid of the errors, open the Eclipse preferences dialog, go to "Java" / "Compiler" / "Errors and Warnings". Under "Errors and Warnings" / "Deprecated and restricted API", set "Forbidden reference (access rule)" to "Ignore".


2.6.4  NetBeans

Another IDE for professional programming is NetBeans. It can be downloaded from netbeans.org. Alternatively, a bundle containing both NetBeans and JDK 8 is available on Oracle's Java download page. As I write this, Netbeans only supports Oracle's Java 8, including JavaFX, but support for later versions is planned.

Using NetBeans is very similar to using Eclipse. Even the layout of its window is very similar to the Eclipse window. Create a project in NetBeans with the "New Project" command. You will have to select the type of project in a pop-up window. You want to create a "Java Application." The project creation dialog will have a suggested name for the project, which you will want to change. It also has an option to create a main class for the project, which is selected by default. If you just want a project for working with the sample programs from this book, then you can disable that option.

A project will have a "Source Folder" where the source code files for the project are stored. You can drag files onto that folder, or you can copy-and-paste them from the file system. For running a file, you can right-click the file and select "Run File" from the pop-up menu. There is also a "Run" button in the NetBeans toolbar. There is no explicit compilation step. Input and output are done in an area below the edit window, just as in Eclipse.

When you are editing a file, NetBeans will mark errors as you type. (Remember, again, that many errors will go away on their own as you continue to type.) If NetBeans displays an error marker with a light bulb in the left-hand margin of the editor, you have to click the light bulb to get a list of possible automatic fixes for the error. NetBeans also has a "Code Completion" feature that is similar to Content Assist in Eclipse. Just press Control-Space as you are typing to get a list of possible completions.


2.6.5  BlueJ

Finally, I will mention BlueJ, an IDE that is designed specifically for people who are learning to program. It is much less complex than Eclipse or NetBeans, but it does have some features that make it useful for education. BlueJ can be downloaded from bluej.org. It currently supports Java 8, and requires that you have a JDK, not just a JRE.

In BlueJ, you can begin a project with the "New Project" command in the "Project" menu. A BlueJ project is simply a folder. When you create a project, you will have to select a folder name that does not already exist. The folder will be created and a window will be opened to show the contents of the folder. Files are shown as icons in the BlueJ window. You can drag .java files from the file system onto that window to add files to the project; they will be copied into the project folder as well as shown in the window. You can also copy files directly into the project folder, but BlueJ won't see them until the next time you open the project. When you restart BlueJ, it should show the last project you were working on, but you can open any project with a command from the "Project" menu.

There is a button in the project window for creating a new class. An icon for the class is added to the window, and a .java source code file is created in the project folder. The file is not automatically opened for editing. To edit a file, double-click its icon in the project window. An editor will be opened in a separate window. (A newly created class will contain some default code that you probably don't want; you can erase it and add a main() routine instead.) The BlueJ editor does not show errors as you type. Errors will be reported when you compile the program. Also, it does not offer automatic fixes for errors. It has a less capable version of Eclipse's Content Assist, which seems only to work for getting a list of available subroutines in a class or object; call up the list by hitting Control-Space after typing the period following the name of a class or object.

An editor window contains a button for compiling the program in the window. There is also a compile button in the project window, which compiles all the classes in the project.

To run a program, it must already be compiled. Right-click the icon of a compiled program. In the menu that pops up, you will see "void main(String[] args)". Select that option from the menu to run the program. Just click "OK" in the dialog box that pops up. A separate window will open for input/output.

One of the neatest features of BlueJ is that you can actually use it to run any subroutine, not just main. If a class contains other subroutines, you will see them in the list that you get by right-clicking its icon. A pop-up dialog allows you to enter any parameters required by the routine, and if the routine is a function, you will get another dialog box after the routine has been executed to tell you its return value. This allows easy testing of individual subroutines. Furthermore, you can also use BlueJ to create new objects from a class. An icon for the object will be added at the bottom of the project window, and you can right-click that icon to get a list of subroutines in the object. This will, of course, not be useful to you until we get to object-oriented programming in Chapter 5.


2.6.6  The Problem of Packages

Every class in Java is contained in something called a package. Classes that are not explicitly put into a package are in the "default" package. All of Java's standard classes are in named packages. This includes even classes like String and System, which are in package named java.lang. Classes in java.lang are automatically imported into any Java file, but classes in other packages must be imported using an import directive. My TextIO class is in a package named textio, and it must be imported into a program that wants to use it. I will discuss packages in much greater detail in Section 4.6. For now, you just need to know some basic facts.

Although most of my examples are in the default package, in fact, the use of the default package is discouraged, according to official Java style guidelines. Nevertheless, I have chosen to use it, since it seems easier for beginning programmers to avoid packages as much as possible, at least at first. If Eclipse or NetBeans tries to put a class into a package, you can delete the package name from the class-creation dialog to get it to use the default package instead. But if you do create a class in a package, the source code starts with a line that specifies which package the class is in. For example, if the class is in a package named test.pkg, then the first line of the source code will be

package test.pkg;

For example, the source code for TextIO begins with "package textio;". I put TextIO in a package because a class that is in a non-default package cannot use a class from the default package. That is, if TextIO were in the default package, then it could only be used by programs that are also in the default package. (In fact, in earlier versions of this textbook, TextIO was in the default package. I have moved it to package textio for this version of the book.)

When packages are used in a command-line environment, some complications arise. For example, if a program is in a package named text.pkg, then the source code file must be in a subdirectory named "pkg" inside a directory named "test" that is in turn inside your main Java working directory. Nevertheless, when you compile or execute the program, you should be working in the main directory, not in the subdirectory. When you compile the source code file, you have to include the name of the directory in the command: For example, for a program in package test.pkg use "javac test/pkg/ClassName.java" on Linux or Mac OS, or "javac test\pkg\ClassName.java" on Windows. The command for executing the program is then "java test.pkg.ClassName", with a period separating the package name from the class name.


End of Chapter 2


[ Previous Section | Chapter Index | Main Index ]