Summary: This module explains how to get started programming in Java.
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.
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:
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.
Perhaps the most complicated thing is to get your computer set up for compiling and running Java code in the first place.
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.
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\binin place of
... ;C:\Program Files\Java\jdk1.7.0\binas 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.
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 .
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.
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) .
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.
| Listing 1: Windows batch file. |
|---|
|
Comments regarding the batch file
The commands in the batch file of Listing 1 will
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.
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.
| Listing 2: A test program. |
|---|
|
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.
This section contains a variety of miscellaneous information.
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-