Creating a Java Application

Now let’s actually get to work. We’ll start by creating a simple Java application: the classic Hello World example that many programming language books use to begin.

Java applications are different from Java applets. Applets, as you have learned, are Java programs that are downloaded over the World Wide Web and executed by a Web browser on the reader’s machine. Applets
depend on a Java-enabled browser in order to run.

New Term
Java applications, however, are more general programs written in the Java language. Java applications don’t require a browser to run; in fact, Java can be used to create
all the kinds of applications that you would normally use a more conventional programming language to create.

Java applications are standalone Java programs that do not require a Web browser to run. Java applications are more general-purpose programs such as you’d find on any computer.

A single Java program can be an applet or an application, or both, depending on how you write that program and the capabilities that program uses. Throughout this first week as you learn the Java language, you’ll be
writing mostly applications; then you’ll apply what you’ve learned to write applets in Week 2. If you’re eager to get started with applets, be patient. Everything that you learn while you’re creating simple Java applications
will apply to creating applets, and it’s easier to start with the basics before moving onto the hard stuff. You’ll be creating plenty of applets in Week 2.

Creating the Source File

As with all programming languages, your Java source files are created in a plain text editor, or in an editor that can save files in plain ASCII without any formatting characters. On UNIX, emacs, pico, and vi will work;
on Windows, Notepad or DOS Edit are both text editors that will work (although I prefer to use the shareware TextPad). On the Macintosh, SimpleText (which came with your Mac) or the shareware BBedit will work.
If you’re using a development environment like Café or Roaster, it’ll have its own built-in text editor you can use.

Note
If you’re using Windows to do your Java development, you may have to make sure Windows understands the .java file extension before you start; otherwise, your text
editor may insist on giving all your files a .txt extension. The easiest way to do this is to go to any Windows Explorer window, choose View|Options|File Types,
choose New Type, and add Java Source File and .java to the Description of Type and Associated Extension boxes, respectively.

Fire up your editor of choice and enter the Java program shown in Listing 1.1. Type this program, as shown, in your text editor. Be careful that all the parentheses, braces, and quotes are there, and that you’ve used all the
correct upper- and lowercase letters.

Note
You can also find the code for these examples on the CD-ROM as part of the sample code. However, it’s a good idea to actually type these first few short examples in
so that you get a feel for what Java code actually looks like.

Listing 1.1. Your first Java application.
1: class HelloWorld {
2: public static void main (String args[]) {
3: System.out.println(“Hello World!”);
4: }
5: }

Warning
The number before each line is part of the listing and not part of the program; the numbers are there so I can refer to specific line numbers when I explain what’s going
on in the program. Do not include them in your own file.

After you’ve finished typing in the program, save the file somewhere on your disk with the name HelloWorld.java. This is very important. Java source files must have the same name as the class they define (including
the same upper- and lowercase letters), and they must have the extension .java. Here, the class definition has the name HelloWorld, so the filename must be HelloWorld.java. If you name your file something else
(even something like helloworld.java or Helloworld.java), you won’t be able to compile it. Make absolutely certain the name is HelloWorld.java.

You can save your Java files anywhere you like on your disk, but I like to have a central directory or folder to keep them all in. For the examples in this chapter, I’ve put my files into a directory called TYJtests (short for
Teach Yourself Java Tests).

Compiling and Running the Source File

Now it’s time to compile the file. If you’re using the JDK, you can use the instructions for your computer system contained in the next few pages. If you’re using a graphical development environment, there will most likely
be a button or option to compile the file (check with the documentation that came with your program).

Windows
To compile the Java source file, you’ll use the command-line Java compiler that comes with the JDK. To run the compiler, you’ll need to first start up a DOS shell. In
Windows 95, the DOS shell is under the Programs menu (it’s called MS-DOS Prompt).

From inside DOS, change directories to the location where you’ve saved your HelloWorld.java file. I put mine into the directory TYJtests, so to change directories I’d use this command:
CD C:\TYJtests

Once you’ve changed to the right directory, use the javac command as follows, with the name of the file as you saved it in Windows (javac stands for Java compiler). Note that you have to make sure you type all the
same upper- and lowercase here as well:
javac HelloWorld.java

Note
The reason that I’ve emphasized using the original filename is that once you’re inside the DOS shell, you might notice that your nice long filenames have been truncated to
old-style 8.3 names and that, in fact, HelloWorld.java actually shows up as HELLOW~1.jav. Don’t panic; this is simply a side effect of Windows 95 and how it
manages long filenames. Ignore the fact that the file appears to be HELLOW~1.jav and just use the filename you originally used when you saved the file.

Figure 1.4 shows what I’ve done in the DOS shell so you can make sure you’re following along.

Figure 1.4 : Compiling Java in the DOS shell.

If all goes well, you’ll end up with a file called HelloWorld.class (or at least that’s what it’ll be called if you look at it outside the DOS shell; from inside DOS its called HELLOW~1.cla). That’s your Java bytecode file.
If you get any errors, go back to your original source file and make sure you typed it exactly as it appears in Listing 1.1 with the same upper- and lowercase. Also make sure the filename has exactly the same upper- and
lowercase as the name of the class (that is, both should be HelloWorld).

Once you have a class file, you can run that file using the Java bytecode interpreter. The Java interpreter is called simply java, and you run it from the DOS shell as you did javac. Run your Hello World program like this
from the command line, with all the same upper- and lowercase (and note that the argument to the java program does not have a .class extension):
java HelloWorld

If your program was typed and compiled correctly, you should get the phrase Hello World! printed to your screen as a response. Figure 1.5 shows how I did it.

Figure 1.5 : Running Java applications in the DOS shell.

Note
Remember, the Java compiler and the Java interpreter are different things. You use the Java compiler (javac) for your Java source files to create .class files, and you
use the Java interpreter (java) to actually run your class files.

Macintosh
The JDK for the Mac comes with an application called Java Compiler. To compile your Java source file, simply drag and drop it on top of the Java Compiler icon. The
program will compile your Java file and, if there are no errors, create a file called HelloWorld.class in the same folder as your original source file.

Tip
Putting an alias for Java Compiler on the desktop makes it easy to drag and drop Java source files.

If you get any errors, go back to your original source file and make sure you typed it exactly as it appears in Listing 1.1, with the same upper- and lowercase. Also make sure the filename has exactly the same upper- and
lowercase as the name of the class (that is, both should be HelloWorld).

Once you’ve successfully generated a HelloWorld.class file, simply double-click it to run it. The application Java Runner, part of the Mac JDK, will start, and the program will ask you for command-line arguments.
Leave that screen blank and click OK. A window labeled stdout will appear with the message Hello World!. Figure 1.6 shows that window.

Figure 1.6 : Running Java applications on the Mac using Java Runner.

That’s it! Keep in mind as you work that you use the Java Compiler application to compile your .java files into .class files, which you can then run using Java Runner.

To compile the Java source file in Solaris, you’ll use the command-line Java compiler that comes with the JDK. From a UNIX command line, cd to the directory that contains your Java source file. I put mine in the
directory TYJtests, so to change directories I’d use this command:
cd ~/TYJtests

Once you’re in the right directory, use the javac command with the name of the file, like this:
javac HelloWorld.java

If all goes well, you’ll end up with a file called HelloWorld.class in the same directory as your source file. That’s your Java bytecode file. If you get any errors, go back to your original source file and make sure you
typed it exactly as it appears in Listing 1.1, with the same upper- and lowercase letters. Also make sure the filename has exactly the same upper- and lowercase letters as the name of the class (that is, both should be
HelloWorld).

Once you have a class file, you can run that file using the Java bytecode interpreter. The Java interpreter is called simply java, and you run it from the command line as you did javac, like this (and note that the argument
to the java program does not have a .class extension):
java HelloWorld

If your program was typed and compiled correctly, you should get the phrase Hello World! printed to your screen as a response. Figure 1.7 shows a listing of all the commands I used to get to this point (the part with
[desire]~[1] is my system prompt).

Figure 1.7 : Compiling and running a Java application on Solaris.

Note
Remember that the Java compiler and the Java interpreter are different things. You use the Java compiler (javac) for your Java source files to create .class files, and
you use the Java interpreter (java) to actually run your class files.

More Info

Creating a Java application involves several steps, from setting up your development environment to writing the code and running/debugging it. Here’s a basic outline to get you started:

1. **Set Up Your Development Environment**:
– Install Java Development Kit (JDK): Download and install the JDK from the official Oracle website or use a package manager if you’re on a Unix-based system.
– Choose an Integrated Development Environment (IDE): Popular choices include IntelliJ IDEA, Eclipse, and NetBeans. Install and set up your preferred IDE.

2. **Create a New Java Project**:
– Open your IDE and create a new Java project. Give it a name and specify the location where you want to save it.

3. **Write Your Java Code**:
– Within your project, create a new Java class. This will typically be the entry point of your application.
– Write your Java code inside this class. This could include defining classes, methods, variables, etc., depending on what your application needs to do.

4. **Compile Your Code**:
– Once you’ve written your code, you need to compile it into bytecode. Most IDEs will automatically compile your code when you save it, but you can also do this manually through the IDE’s menu or using command-line tools like `javac`.

5. **Run Your Application**:
– After compiling, you can run your application. IDEs usually provide a “Run” button that you can click to execute your code. Alternatively, you can run it from the command line using the `java` command followed by the name of your main class.

6. **Debug Your Application** (optional):
– Debugging is an essential part of software development. IDEs come with powerful debugging tools that allow you to set breakpoints, inspect variables, and step through your code line by line to identify and fix issues.

7. **Test Your Application**:
– Testing is crucial to ensure that your application behaves as expected. Write unit tests to verify the functionality of individual components and integration tests to test the interaction between different parts of your application.

8. **Package and Distribute Your Application** (if necessary):
– If you’re developing a standalone application that you want to distribute to users, you’ll need to package it into a format that users can easily install and run. This might involve creating a JAR (Java Archive) file or an installer package depending on your target platform.

Remember to document your code as you go and follow best practices for Java development to ensure that your application is maintainable and scalable. Good luck with your Java development journey!

prime number
Scroll to Top