|
Introduction
Using separate components
Integrated Development Environments
Classpath Problems
Back to Java Resourses
Back to Main Page
|
Getting Started with Java in CS 1322
Introduction
Using separate components
Integrated Development Environments
Classpath Problems
Before you can start writing programs you need two
things, a text editor and a java compiler. There are two ways to fufill these requirements, using separate
components or using an integrated development environment (IDE). A text editor
allows us to enter the program in human-readable form using the syntax of the
Java programming language. A compiler takes the human-readable Java program and
translates it into a form that can be executed by the Java Virtual Machine
(JVM).
The first lecture and the first recitation will make these relationships more
clear. We will also demonstate both command-line and IDE development at the
first recitation.
A good place to learn about java is the Java tutorial at
http://java.sun.com/docs/books/tutorial/index.html.
References to the Java classes are available on-line at
http://java.sun.com/j2se/1.4.2/docs/api/
Using a separate compiler and editor allows you to minimize the disk space required
and allows you to use your favorite text editor to do your programming (like
emacs or notepad). A big disadvantage is that this limits your debugging
options. Also from a future job perspective, no commercial developers generally
develop this way. Almost all software development companies use IDE's to
develop non-trivial applications. Developing with a separate compiler and
editor is often called command-line development.
The first thing you will need
to do is to download and install the java development kit, standard edition
(J2SE JDK). You can get the JDK from two places:
1. The CD in the back of the
book. The book provides you with J2SE version 1.4.0. This should be adequate
for CS1322. The latest version is 1.4.2. If you desire to use the latest
version of the JDK, would need to use option 2.
IMPORTANT: The CD contains only a windows version of the JDK. If you use a Mac,
or U**x environment, you will need to use option 2.
a. Place the book CD into your PC's cdrom drive.
b. Go to the JavaSDK directory
c. Double click on the jdk-1_4_0_01-windows-i586.exe to start the self-install
process. Follow the on-screen prompts.
d. If you wish to have the Java documentation (API) installed locally, then
extract
the files in the j2sdk-1_4_0.zip archive. Extract them to your JDK install
location.
e. Be sure you place the bin directory in your path. If you install to the
default location it would be: c:\j2sdk1.4.0_01\bin for example. For Windows XP, you can do this by going to Control Panel, selecting System,
Select the Advanced tab, and push the Environment Variables button.
f. Test your installation. Bring up the
command prompt and type: java -version
at the prompt. If you get the version number displayed, then everything is OK.
If you get a program not found error, then you
do not have the bin directory for the JDK on your path.
g. Now you can bring up your favorite text editor and type in your java
program.
h. Save the program and go to the command prompt. Change into the
directory that you saved your program
into.
i. Type javac my-program-name.java
where my-program-name.java is the name of the java program you saved. If you
get a program not found, then the javac command is not in your path. Go back to
step e above.
j. Now type java my-program-name and your program should
execute. If you get a java exception that says main not found, then you have a
classpath problem. See the section on Classpath for help.
2. Downloading the latest version direct from sun.
a. Go to Sun's Java website: java.sun.com.
Look at the popular downloads link on the
right of the web page. The third link down is
J2SE
1.4.2 SDK.
Follow that link to the download page.
b. Scroll down to the Download J2SE v 1.4.2_03
and select your platform. You want to download
the SDK (software development kit) and not the
JRE (runtime enviroment). The JRE allows you
to execute java programs, but doesn't include
the compiler.
c. The detailed installation instructions are
on the same web page.
d. After downloading the exe file, then the
install instructions are the same as Option 1
for steps c-j.
IDE's are a powerful and easy way to develop software applications. IDE's will
integrate the editor, compiler and runtime functionality all into one program.
The editors tend to have many helpful features like color-coded syntax
highlighting, and sophisticated error checking. There are many IDE's available
for Java, and the ultimate selection is often a matter of personal preference.
We will cover four of the more common IDE's here.
JGRASP
JGrasp is available on the cdrom accompaning your textbook. A more up to date
version is available from the JGrasp home
page. The most recent version is 1.6.4. Clicking on the download link
will allow you to download the latest version of the program and the available
documentation. There is a pdf manual available that will give you detailed
instructions on this application.
JGrasp is a fairly simple IDE. It provides basic debug and editor
functionality. This is probably the best IDE to use if you are not a CS major
and do not intend to ever develop larger-scale programs than those in CS1322.
If you are a CS major, or you intend to develop software commercially, then one
of the other more powerful IDE's would be well worth your effort to learn.
NetBeans
NetBeans is the IDE provided by Sun (The makers of java). NetBeans is a much
more sophisticated IDE than JGrasp. NetBeans has a graphical GUI builder, which
can help during more advanced programming assignments. You can download the
latest version of NetBeans by going to
the Java SDK download page and selecting the Download NetBeans IDE v
3.5.1 with J2SE v 1.4.2 COBUNDLE (info) option.
Eclipse
Eclipse is a major open-source effort to develop an extensible workspace for
many different kinds of applications. Developers extend eclipse by building
plug-ins. The basic download of the Eclipse IDE includes the Java development
tools necessary to build java applications. Eclipse does not currently have a
graphical GUI builder, but it is very competitive with its commercial
counterparts. Eclipse can be downloaded from
here. This is one of the best IDE's for students with Mac's. Download
the latest stable version 2.1.2.
JBuilder
This is one of our instructor's (Bob) personal favorites, but it is only available for Windows, Linux or
Solaris. The current version is Version 9. It has many syntax correcting help
functions, plus powerful project management capabilities. JBuilder also
integrates with many 3d party Java tools that will be useful in your future
software development career. It has a little bit of a learning curve because it
is so powerful, so if you are a casual developer, you might steer clear of this
package. If however, you plan to develop professional applications, then this
IDE will do it all. You want to download the free Personal version. JBuilder is
available
here. You do have to register with Borland to download this product, so
if you have an aversion to this, you may want to consider Eclipse.
One of the more frustrating things that you have to deal with in the
development of Java programs is the concept of a classpath. When we create
.class files from our .java text files, the java environment needs to know how
to find them. That is the purpose of the classpath. It may seem stupid if we
are only creating a java program that uses all the class files in a single
directory to have to fool with a classpath variable. Think about building real
applications though. We might need to reuse classes and libraries that other
people have already developed. We need to be able to tell the compiler and the
execution environment where to find those classes.
There are two different ways to set the classpath. If you use 3d party
libraries in most of your projects, you can set the classpath globally through
the use of environment variables. For windows we would use:
set CLASSPATH=c:\myclasses\myclasses.jar
For unix we would put the following in the .bashrc config file:
CLASSPATH=/usr/j2ee/j2ee.jar:.;export CLASSPATH
If we need to set different paths for different projects that we create, then
we can pass the classpath to the compiler and the execution environment.
For the compiler we would use:
WINDOWS
javac -classpath "c:\myclasses\myclasses.jar;." my-java-prog.java
UNIX
javac -classpath "./myclasses/myclasses.jar:." my-java-prog.java
Two important things to note:
1. The primary difference between Windows and Unix in the classpath is the
delimeter between locations. For windows it is a semicolon ";" and for unix it
is a colon ":".
2. Note the period "." as the last entry in the classpath list. That tells Java
to search the current directory for class files.
The main symptoms of a classpath problem are error messages that state
Java cannot find the startup code of your application: Exception in thread
"main" java.lang.NoClassDefFoundError: foo
You reference a class in one file from another and you know that class is
defined, but you still get a error like:
Demo.java:4: cannot resolve symbol symbol : class MyClass location: class Demo
MyClass mc = new MyClass();
Copyright © College of Computing Any unauthorized reproduction or use is strictly prohibited.
|