Skip to content Skip to navigation

OpenStax-CNX

You are here: Home » Content » Jb0110: Java OOP: Programming Fundamentals, Getting Started

Navigation

Recently Viewed

This feature requires Javascript to be enabled.
 

Jb0110: Java OOP: Programming Fundamentals, Getting Started

Module by: R.G. (Dick) Baldwin. E-mail the author

Summary: This module explains how to get started programming in Java.

Note: You are viewing an old style version of this document. The new style version is available here.

Preface

General

This module is part of a sub-collection of modules designed to help you learn to program computers.

This module explains how to get started programming using the Java programming language.

Prerequisites

In addition to an Internet connection and a browser, you will need the following tools (as a minimum) to work through the exercises in these modules:

The minimum prerequisites for understanding the material in these modules include:

  • An understanding of algebra.
  • An understanding of all of the material covered in the earlier modules in this collection.

Viewing tip

I recommend that you open another copy of this document in a separate browser window and use the following links to easily find and view the listings while you are reading about them.

Listings

Writing, compiling, and running Java programs

Writing Java code

Writing Java code is straightforward. You can write Java code using any plain text editor. You simply need to cause the output file to have an extension of .java.

There are a number of high-level Integrated Development Environments (IDEs) available, such as Eclipse and NetBeans, but they tend to be overkill for the relatively simple Java programs described in these modules.

There are also some low-level IDEs available, such as JCreator and DrJava, which are very useful. I normally use a free version of JCreator, mainly because it contains a color-coded editor.

So, just find an editor that you are happy with and use it to write your Java code.

Preparing to compile and run Java code

Perhaps the most complicated thing is to get your computer set up for compiling and running Java code in the first place.

Downloading the java development kit (JDK)

You will need to download and install the free Java JDK from the Oracle/Sun website. As of November, 2012, you will find that website at http://www.oracle.com/technetwork/java/javase/downloads/index.html

There is a 64-bit version of the JDK, but I haven't tried it yet because my home computer won't can't support it. I am still using the 32-bit version. However, I suspect that the 64-bit version will work just fine if you have a computer that supports it.

Whether you elect to use the 32-bit or 64-bit version is strictly up to you. Either of them should do the job very nicely.

Installing the JDK

As of November 2012, you will find installation instructions at http://download.oracle.com/javase/7/docs/webnotes/install/windows/jdk-installation-windows.html .

I strongly recommend that you read the instructions and pay particular attention to the information having to do with setting the path environment variable.

A word of caution

If you happen to be running Windows Vista or Windows 7, you may need to use something like the following when updating the PATH Environment Variable

... ;C:\Program Files (x86)\Java\jdk1.6.0_26\bin

in place of

... ;C:\Program Files\Java\jdk1.7.0\bin

as shown in the installation instructions.

I don't have any experience with any Linux version. Therefore, I don't have any hints to offer there.

The JDK documentation

It is very difficult to program in Java without access to the documentation for the JDK.

Several different types of Java documentation are available online at http://www.oracle.com/technetwork/java/javase/documentation/index.html .

Specific documentation for classes, methods, etc., is available online at http://download.oracle.com/javase/7/docs/api/ .

It is also possible to download the documentation and install it locally if you have room on your disk. The download links for JDK 6 and JDK 7 documentation are also shown on the page at http://www.oracle.com/technetwork/java/javase/downloads/index.html .

Compiling and running Java code

There are a variety of ways to compile and run Java code. The way that I will describe here is the most basic and, in my opinion, the most reliable. These instructions apply to a Windows operating system. If you are using a different operating system, you will need to translate the instructions to your operating system.

Write your Java program

Begin by using your text editor to write your Java program into one or more text files, each with an extension of .java. (Files of this type are often referred to as source code files.) Save the source code files in an empty folder somewhere on your disk. Make sure that the name of the class containing the main method (which you will learn about in a future module) matches the name of the file in which that class is contained (except for the extension of .java on the file name, which does not appear in the class name) .

Create a batch file

Use your text editor to create a batch file (or whatever the equivalent is for your operating system) containing the text shown in Listing 1 (with the modifications discussed below) and store it in the same folder as your Java source code files..

Then execute the batch file, which in turn will execute the program if there are no compilation errors.

Table 1
Listing 1 . Windows batch file.

echo off
cls

del *.class

javac -cp .; hello.java
java -cp .; hello

pause

Comments regarding the batch file

The commands in the batch file of Listing 1 will

  • Open a command-line screen for the folder containing the batch file.
  • Delete all of the compiled class files from the folder. (If the folder doesn't contain any class files, this will be indicated on the command-line screen.)
  • Attempt to compile the program in the file named hello.java.
  • Attempt to run the compiled program using a compiled Java file named hello.class .
  • Pause and wait for you to dismiss the command-line screen by pressing a key on the keyboard.

If errors occur, they will be reported on the command-line screen and the program won't be executed.

If your program is named something other than hello , (which it typically would be) substitute the new name for the word hello where it appears twice in the batch file.

Don't delete the pause command

The pause command causes the command-line window to stay on the screen until you dismiss it by pressing a key on the keyboard. You will need to examine the contents of the window if there are errors when you attempt to compile and run your program, so don't delete the pause command.

Translate to other operating systems

The format of the batch file in Listing 1 is a Windows format. If you are using a different operating system, you will need to translate the information in Listing 1 into the correct format for your operating system.

A test program

The test program in Listing 2 can be used to confirm that Java is properly installed on your computer and that you can successfully compile and execute Java programs.

Table 2
Listing 2 . A test program.
class hello {
  public static void main(String[] args){
    System.out.println("Hello World");
  }//end main
}//end class

Instructions

Copy the code shown in Listing 2 into a text file named hello.java and store in an empty folder somewhere on your disk.

Create a batch file named hello.bat containing the text shown in Listing 1 and store that file in the same folder as the file named hello.java .

Execute the batch file.

If everything is working, a command-line screen should open and display the following text:

Hello World
Press any key to continue . . .

Congratulations

If that happens, you have just written, compiled and executed your first Java program.

Oops

If that doesn't happen, you need to go back to the installation instructions and see if you can determine why the JDK isn't properly installed.

If you get an error message similar to the following, that probably means that you didn't set the path environment variable correctly.

'javac' is not recognized as an internal or external command,
operable program or batch file.

Beyond that, I can't provide much advice in the way of troubleshooting hints.

Miscellaneous

This section contains a variety of miscellaneous information.

Housekeeping material
  • Module name: Jb0110: Java OOP: Programming Fundamentals, Getting Started
  • File: Jb0110.htm
  • Published: 11/16/12
  • Revised: 12/03/14
Disclaimers:

Financial : Although the Connexions site makes it possible for you to download a PDF file for this module at no charge, and also makes it possible for you to purchase a pre-printed version of the PDF file, you should be aware that some of the HTML elements in this module may not translate well into PDF.

I also want you to know that, I receive no financial compensation from the Connexions website even if you purchase the PDF version of the module.

In the past, unknown individuals have copied my modules from cnx.org, converted them to Kindle books, and placed them for sale on Amazon.com showing me as the author. I neither receive compensation for those sales nor do I know who does receive compensation. If you purchase such a book, please be aware that it is a copy of a module that is freely available on cnx.org and that it was made and published without my prior knowledge.

Affiliation : I am a professor of Computer Information Technology at Austin Community College in Austin, TX.

-end-

Content actions

Download module as:

PDF | EPUB (?)

What is an EPUB file?

EPUB is an electronic book format that can be read on a variety of mobile devices.

Downloading to a reading device

For detailed instructions on how to download this content's EPUB to your specific device, click the "(?)" link.

| More downloads ...

Add module to:

My Favorites (?)

'My Favorites' is a special kind of lens which you can use to bookmark modules and collections. 'My Favorites' can only be seen by you, and collections saved in 'My Favorites' can remember the last module you were on. You need an account to use 'My Favorites'.

| A lens I own (?)

Definition of a lens

Lenses

A lens is a custom view of the content in the repository. You can think of it as a fancy kind of list that will let you see content through the eyes of organizations and people you trust.

What is in a lens?

Lens makers point to materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

Who can create a lens?

Any individual member, a community, or a respected organization.

What are tags? tag icon

Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

| External bookmarks