Skip to main content
Engineering LibreTexts

1.5: Editing, Compiling, and Running a Java Program

  • Page ID
    15060
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    In this section we discuss the nuts and bolts of how to compile and run a Java program. Because we are exploring two different varieties of Java programs, console applications and Swing applications, the process differs slightly for each variety. We have already discussed some of the main language features of console and Swing applications, so in this section we focus more on features of the programming environment itself. Because we do not assume any particular programming environment in this book, our discussion will be somewhat generic. However, we do begin with a brief overview of the types of programming environments one might encounter.

    Java Development Environments

    A Java programming environment typically consists of several programs that perform different tasks required to edit, compile, and run a Java program. The following description will be based on the software development environment provided by Oracle, the company that owns and maintains Java. It is currently known as the Java Platform, Standard Edition 8.0 (Java SE 8). Versions of Java SE are available for various platforms, including Linux, Windows, and macOS computers. Free downloads are available at Sun’s Web site at http://www.oracle.com/technetwork/java/. (For more details about the Java SE, see Appendix [appendix-jdk].)

    In some cases, the individual programs that make up the Java SE are available in a single program development environment, known as an integrated development environment (IDE). Some examples include Eclipse, jGrasp, and Oracle’s own NetBeans IDE. Each of these provides a complete development package for editing, compiling, and running Java applications on a variety of platforms, including Linux, macOS, and Windows.

    Figure [fig:compile] illustrates the process involved in creating and running a Java program. The discussion that follows here assumes

    that you are using the Java SE as your development environment to edit, compile and run the example program. If you are using some other environment, you will need to read the documentation provided with the software to determine exactly how to edit, compile, and run Java programs in that environment.

    Editing a Program

    Any text editor may be used to edit the program by merely typing the program and making corrections as needed. Popular Unix and Linux editors include vim and emacs. These editors are also available on macOS and Windows. However, free macOS editors include TextMate and TextWrangler, and Windows has Notepad++ for free.

    As we have seen, a Java program consists of one or more class definitions. We will follow the convention of placing each class definition in its own file. (The rule in Java is that a source file may contain only one public class definition.) The files containing these classes’ definitions must be named ClassName.java where ClassName is the name of the public Java class contained in the file.

    For example, in the case of our HelloWorld application program, the file must be named HelloWorld.java, and for HelloWorldSwing, it must be named HelloWorldSwing.java. Because Java is case sensitive, which means that Java pays attention to whether a letter is typed uppercase or lowercase, it would be an error if the file containing the HelloWorld class were named helloworld.java or Helloworld.java. The error in this case would be a semantic error. Java would not be able to find the HelloWorld class because it will be looking for a file named HelloWorld.java.

    Compiling a Program

    Recall that before you can run a Java source program you have to compile it into the Java bytecode, the intermediate code understood by the Java Virtual Machine (JVM). Source code for both applets and applications must be compiled. To run a Java program, whether an applet or an application, the JVM is then used to interpret and execute the bytecode.

    The Java SE comes in two parts, a runtime program, called the Java Runtime Environment (JRE) and a development package, called the Software Development Kit (SDK). If you are just going to run Java programs, you need only install the JRE on your computer. In order to run Java applets, browsers, such as Internet Explorer and Netscape Navigator, must contain a plugin version of the JRE. On the other hand, if you are going to be developing Java programs, you will need to install the SDK as well.

    The Java SDK compiler is named javac. In some environments—such as within Linux or at the Windows command prompt —HelloWorld.java would be compiled by typing the following command at the system prompt:

    javac HelloWorld.java

    As Figure [fig:compile] illustrates, if the HelloWorld.java program does not contain errors, the result of this command is the creation of a Java bytecode file named HelloWorld.class—a file that has the same prefix as the source file but with the suffix .class rather than .java. By default, the bytecode file will be placed in the same directory as the source file. If javac detects errors in the Java code, a list of error messages will be printed.

    Running a Java Application Program

    In order to run (or execute) a program on any computer, the program’s executable code must be loaded into the computer’s main memory. For Java environments, this means that the program’s .class file must be loaded into the computer’s memory, where it is then interpreted by the Java Virtual Machine. To run a Java program on Linux systems or at the Windows command prompt, type

    java HelloWorld

    on the command line. This command loads the JVM, which will then load and interpret the application’s bytecode(HelloWorld.class). The “HelloWorld” string will be displayed on the command line.

    On Macintosh systems, or within an IDE, which do not typically have a command line interface, you would select the compile and run commands from a menu. Once the code is compiled, the run command will cause the JVM to be loaded and the bytecode to be interpreted. The “Hello, World!” output would appear in a text-based window that automatically pops up on your computer screen. In any case, regardless of the system you use, running the HelloWorldconsole application program will cause the “Hello, World!” message to be displayed on some kind of standard output device (Fig. [fig:stdout]).

    Running a Java Swing Program

    When you run a Java Swing Program, there is typically no console output. You only see your output in the Window (JFrame) that your Graphics are displayed in. This makes automated testing more difficult since you need to visually inspect that the program is working correctly.

    When you run

    java HelloWorldSwing

    A window will open, and you won’t be able to type in the console until you close the window, quit the program, or type ctl-c to send a kill signal to the Swing program. The result of running, as shown in Figure [fig:hello], is that the “Hello, World!” message will be displayed within it’s own window.


    This page titled 1.5: Editing, Compiling, and Running a Java Program is shared under a CC BY 4.0 license and was authored, remixed, and/or curated by Ralph Morelli & Ralph Wade via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.