Last modified: Friday January 14, 2011 10:59:21 AM -0500
There are three basic tasks in programming in Java:
To accomplish task 1, you need a text editor to do text editing; to accomplish task 2, you need a Java compiler; to accomplish task 3, you need a Java interpreter. There are various approaches for accomplishing the three tasks. Below we describe key steps, when you are using your own computers. If you dot not plan to install Java on your own computers and want to use Pantheon or cluster machines, please follow more specific instructions (programming in Java using Pantheon computers).
You will need to install, at most, two pieces of software: The Java Development Kit (JDK) and an Integrated Development Environment. The directions differ based on your operating system. If you plan to use a command-line interface for compiling and running, then you need to install the JDK only.
Macs with the OS X operating system will have the
Java compiler available automatically, so you might be able to skip this
instllation. But many Macs appear to have an out-of-date version of JDK.
Some newer Macs (purchased within the last year or so)
have the latest Mac version of JDK (version 6.0). For older Macs, you
might want to run your Mac's built-in Software Update feature to see if
it updates your Java for you.
Test Java version: You can test whether your
Mac has Java installed (and if so, what version) using the Terminal
application. To run the terminal, use your Spotlight icon at the
upper-right of your screen to search for Terminal. It will find and
suggest the Terminal application. Click it to run the terminal. A
white window will appear with black text and a blinking cursor for
you to type commands. Type the following command and press Enter, to
see your version of Java:
java -version
If your Mac doesn't have Java installed already (or has an obsolete version installed), you can go to the web page below to download it. Choose the link that matches your version of Mac OS X. You can see your OS X version by clicking the Apple icon on the top-left of your screen and choosing "About this Mac."
Java for Mac OS X 10.6 ("Snow Leopard") Update 3: http://support.apple.com/kb/dl972
Java for Mac OS X 10.5 ("Leopard") Update 8: http://support.apple.com/kb/dl971
The preceding downloads come as .dmg disk images. Once you download one of them, you double-click it in the Finder to mount it as a temporary disk. Inside is a .pkg file representing the JDK application installer. Run this .pkg file to install JDK.
It's tougher to give an installation guide for Linux because of the large variety of different distributions and architectures. Here is a rough guide that will work for many distributions such as Ubuntu.
Type the following three commands:
sudo add-apt-repository "deb http://archive.canonical.com/ lucid partner" sudo apt-get update sudo apt-get install sun-java6-jdk
Or use the Step 1 of Windows to download by choosing Linux or Linux 64.
After installing Java, you will probably wish to add Java's directory to your PATH setting, so that you can run Java commands from your terminal. To do so, edit the file .bashrc in your home directory and add the following lines to the end of it:
export PATH=/usr/lib/jdk/bin:$PATH export CLASSPATH=.
You should close your terminal and open a new one for the PATH changes to take effect.
This step is optional, depending on how you want to develop Java programs. There are many Java IDEs available.
We recommend Eclipse, as it is an IDE used by professional computer programmers as well. It has a great online help system. Later in the semester, if you want, you can develop Google Android applications using eclipse easily. Eclipse is available at: http://www.eclipse.org/downloads/.
Windows: Please choose Eclipse IDE for Java Developers, and pick Windows 32 Bit or Windows 64 bit.
For MAC: Please choose Mac OS X (Cocoa)
After downloading eclipse, please click it to extract the programs. Please remember where you extract it. It may be helpful to create a shortcut to the eclipse program (<your-extract-dir>/eclipse/eclipse) .
Another good IDE is Dr. Java:
Once you have Java JDK installed, you must check if your operating system knows where it is. If you have an IDE, you can try starting it up and seeing if it complains about Java not being present. If not, congratulations! You can go to the next step. You can also test using a command-line window by typing javac in a command-line window. To start a command-line window:
In the command window, type
javac
If
you do not get a message saying that the command
isn't recognized, great. Please proceed to the next step! Otherwise, you need to edit your "environment variables" to tell your computer
where to look for javac. Environment variables are little snippets of
information that the OS uses so it won't have to keep asking you questions like
"Where's your login directory?" or "Can I execute a .BAT file?" A key
environment variables is the PATH variable that tells the OS where to look for
executable programs.
Now, with the Java compiler and interpreter installed, we can finally start to program! How exciting! We consider two scenarios:
You maintain a program (e.g., Hello.java) as an ordinary text file, and you write and edit your program with a text editor of your choosing ("Notepad" if you're desperate). For example, a popular editor is emacs (a help file for using emacs). Emacs was written for the Gnu-Linux system, and the latest versions are produced for Linux and other "mainstream" Unix systems first. The Windows version is now maintained on an official basis, and closely tracks the latest Unix version. Mac OS X, although it is Unix, presents some special problems for Emacs, mainly because the Mac uses a different window-management system from everyone else. Another one is pico.
After writing or editing your program using your text editor, you compile your program in a command-line window. You first type
javac Program.java
to compile the program to generate Program.class. Then you type
java Program
to run it.
Using a general text editor is fast. However, many people prefer an Interactive Development Environment (IDE), which provides Java-specific editing capability. Also, an IDE can invoke Java compiler and interpreter automatically without requiring you to use a command-line window.
We use problem assignment 1 as an example of how to use eclipse [We assume you installed eclipse in Step 1.2]