| COS 212 |
Fall 2009 |
| Intro to NetBeans, Eclipse or another IDE |
Benji Shults |
IDE required
In this class, you are no longer allowed to use a simple text
editor such as Notepad, DOS-Edit, Blue-J, etc. You may be the
fastest Notepad text editor in the world but that isn't enough
anymore. Just like the best swimmer can't swim from Los
Angeles to Hong Kong, your Notepad skills won't get you very
far when you begin to work on big projects.
Therefore, you are now required to use a more sophisticated
tool. I use Emacs for my text editing and NetBeans for
organizing large projects. For projects like you will have in
this course, Emacs is more than enough. For large team
projects, you will probably someday want to learn something
like NetBeans or Eclipse as well.
Once you discover how much faster you can do things using
Emacs shortcuts, you will appreciate the fact that NetBeans
and Eclipse can be set up to use those same key bindings.
The purpose of this assignment is to introduce you to one of
these tools. You are not required to use one of these
particular tools for the rest of the semester but you must be
exposed to them in this lab and you must use something with
similar features for the rest of the semester.
Some IDEs you might want to try include
-
Eclipse
-
NetBeans
-
IntelliJ
-
JCreator
-
Metrowerks CodeWarrior
Note that Microsoft's Visual Studio does not support Java. It
supports a language called J++ which is extremely similar to
Java. It also supports a language called C# which does to C++
what Java did to C. (In other words, C# is like Java but with
a much more syntax.) Neither of these languages is Java so
Visual Studio is not an option for Java development.
These days, I am recommending NetBeans. I might talk about
some reasons for this in class.
Introduction to Emacs and JDEE
Emacs is the most powerful text editor in existence. It also
has many add-on packages (such as the JDEE for Java
development) that let the user do things like compile and run
code at the press of a single key.
Like any such powerful tool, it takes a lot of time to learn
and become proficient. But for every hour you put into
learning it, you will save yourself about a year of typing
time over the rest of your life.
Introduction to NetBeans
NetBeans is a tool that helps when working with a project with
lots of files involved or when working with a team. Emacs has
add-on packages that do everything these packages do, but some
people prefer to use the mouse and click on menus and buttons
rather than learning faster and wrist-healthier key-strokes.
Also, with Emacs it can be a pain to install all the needed
add-on packages but it is easier to install add-ons to
NetBeans.
For example, I am currently working on a project that has 145
java files, about a hundred input files, and several people
around the world who work on it with me. I use NetBeans to
help me organize this. However, I still do most of my actual
coding in Emacs.
Learn one of these!
For this assignment, you must use one of these development
environments to write a program. Complete one of the
following three parts. I recommend that you do Part II
(NetBeans).
Part I: Emacs
Become familiar with Emacs.
This part of the lab should take at least one hour unless you
already know Emacs. Please, take at least 40 minutes to do
this. You will be glad you did.
-
Download and install Emacs on your machine at home or do
this assignment on a machine in the CS lab.
-
Download this file and
name it ".emacs" (or optionally "_emacs" if you are running
emacs on windows.) Put that file in the folder that is the
value of your "HOME" environment variable.
-
Start Emacs and then go through the tutorial by
pressing `C-h t'. This should take about 40 minutes.
-
Here are a few things always to remember when you are using
Emacs:
-
Always pay attention to the minibuffer. If there is a
message in the minibuffer, satisfy it before you try to
do anything else. If you don't do this, strange things
can happen. If you see a message that you do not
understand in the minibuffer, press `C-g'. `C-g' is the
cancel button.
-
You will work most efficiently the more keyboard
commands you learn. Do your best to resist the
tempation to move your hand to the mouse or the cursor
keys. Of course, it takes time to learn the keyboard
commands but the patience pays off. If you spend some
hours this semester learning to use Emacs without the
cursor keys or mouse, you will save months of work in
the future.
-
Don't forget about automatic word completion. Emacs
will try to finish typing a word for you if you hit
`M-/'. In the minibuffer, just hit TAB or SPACE and
Emacs will finish typing whatever word you are in the
middle of if it can figure it out.
-
Here are some commands that I use all the time and don't
seem to be mentioned in the tutorial:
-
`C-x b': change buffer.
-
`M-%' (= `M-x query-replace'): replace selected
occurrences of some text with some other text.
-
`M-x query-replace-regexp': replace selected
occurrences of some regular expression with some
other text. If you know what a regular expression
is, you will love this. If you want to learn about
how regular expressions work with Emacs, you can
learn about them in Emacs info: `C-h i'. This is a
more advanced topic so don't try to learn it now.
-
`C-SPC': set the mark. The rest of these are
mentioned in the tutorial but I want to emphasize
them. This is what you use when you want to create
a region. Set the mark, then move the point
elsewhere. A region will exist between the point
and the mark. Any commands you execute that have to
do with a region, will affect that region.
-
`C-x C-x': replace point and mark. This is a great
way to save your place, go look at some other code
and get back to where you were immediately.
-
`C-w': cut the region and put it in the kill ring.
-
`M-w': copy region to kill ring.
-
`C-y': copy the latest kill at point.
-
`M-y': must be hit right after `C-y'. Replace the
item yanked with the previously killed item. You
can hit this many times to cycle through the kill
ring.
-
Everything in the Emacs tutorial is crucial. Keep doing
the tutorial until you "get it." This investment of
time at the beginning of the semester will save you an
unfathomable amount of time for the rest of your life.
Once you have a good handle on the tutorial, Emacs info
is probably the next place to go to learn more: `C-h i'.
Use Emacs to write a text file
-
Use Emacs to write a text file (.txt) whose contents explain
your experience in computer science and mathematics and what
attracts you to computer science courses. Paragraphs in the
text should not be indented. Paragraphs should be separated
by a single empty line. Headers should be set off from the
rest of the text by at least one empty line above and below.
Sentences should be separated by two spaces. No line of
text should exceed 70 characters in length. There should be
no hyphenation. There should be no grammar or spelling
errors. Be sure to put your name on it. The file name
should include no spaces. Follow this example.
-
Hints
-
Be sure you are in Text mode. Look at the mode line to
find out. If you are not in Text mode, do `M-x
text-mode'.
-
You might also like to use Auto-Fill mode. In Auto-Fill
mode, Emacs will automatically insert newlines when
appropriate. I usually use Auto-Fill mode when I am in
Text mode but it is not so good when you are programming
because you need to determine where to put newlines
yourself in a program. To toggle Auto-Fill mode, do
`M-x auto-fill-mode'.
-
Whether or not you are in Auto-Fill mode, you can use
`M-q' to have Emacs automatically fill a paragraph for
you. It works only on the current paragraph. If you
want to fill more than one paragraph at once, create a
region and do `M-x fill-region'.
JDE: write, compile, and run a Java program.
This should take less than one half hour.
-
The best way to start writing a Java program using the
JDE is to use `M-x jde-gen-console-buffer' or from the
menus, select Files->JDE New->Console. Watch the
minibuffer! First, you must enter the name of the file
where you will write your Java code. Call the file
"Hello.java" and put it on your disk.
If you ever do `M-x jde-gen-console-buffer' or
Files->JDE New->Class (you don't need to right now), then
you will also be prompted to enter a class that your new
class extends and an interface that your new class
implements. You can just hit return on both of those
prompts.
Notice that the JDE writes a lot of code for you.
Here are some things to be aware of concerning JDE mode:
-
`C-c C-v C-l' will make Emacs type a println statement
and prompt you for its argument.
-
`M-x jde-gen-get-set' or JDE->Templates->Get/Set pair is
another one you might want to use.
-
To see other things that Emacs will type for you, do
`M-x jde-gen- TAB'. Most of those we will not use, at
least not until later.
-
`C-j' is a shortcut for newline and indent.
-
TAB behaves in a very wonderful way but it takes some
getting used to. When you hit TAB, the current line
will be properly indented. It doesn't matter where
on the line you are when you hit TAB: it always indents
the current line.
-
To indent a region: first select the region using
`C-SPC'. (To make the entire file the current region,
do `C-x h'.) Then press `C-M-\' (= `M-x
indent-region'.)
-
When you are writing long comments, you can use `M-q' to
fill paragraphs.
-
`M-;' starts a comment at the end of the current line.
It doesn't matter where on the line you are when you do
this.
-
Don't forget about `M-/' to have Emacs do the typing.
- Type in the following program.
Of course, you could just copy and paste, however, I recommend
that you don't. I hope you will type it in by hand so that
you can practice correcting mistakes and do cool things like
`M-/' (complete typing) and `M-q' (fill a paragraph).
/**
* This program is the classic "Hello, World" program written in
* Java. It really serves no useful purpose but to practice
* using editing tools and feel the thrill of writing a first program.
*
* <p>Created: Today's Date Here
*
* @author Your Name Here
*/
public class Hello {
/**
* This program prints "Hello, World." to the console.
*/
public static void main(String[] args) {
System.out.println("Hello, World.");
}
} // Hello
-
Compile the program using `C-c C-v C-c' or `M-x
jde-compile'.
- If there are errors, you can go directly to them by
putting the point on the line with the description of the
error on it and hitting RETURN.
-
Run the program using `C-c C-v C-r' or `M-x jde-run'.
Part II: NetBeans
Become familiar with NetBeans
Download the latest
version of NetBeans and install it on your machine. I recommend
that you get the download that contains all packages because you
will use them eventually.
Start a Project in NetBeans
-
Go to File->New Project and select the "Java" category then
the "Java Application" type of project. Click "Next".
-
Name the project Lab1 and choose the directory where
you want its files to be stored. You probably want to
change the "Project Location" so that it points to the
folder where you want your source code to be.
-
It's nice to have NetBeans create a main class for you. If
you know how to handle packages, then name the main class
"lab1.Hello". If you are unsure of how to deal with
packages, then name if "Hello". You may want to uncheck the
"Set as Main Project" box . Click "Finish".
Customize the project
-
If you didn't have NetBeans create a main class for you,
create one now. Select File->New File. Be sure that your
Lab1 project is selected. Choose the "Java" category and
the "Java Main Class" file type.
-
Name the class
Hello. Make the package
blank or <default package>.
-
NetBeans should have written a fair amount of code for you.
Compiling and running
-
Finish writing the code as described above.
-
Now you should be able to run the program. Right-click in
the code pane and select "Run File". The words "Hello,
World." should appear in the Console pane.
Write a text file
-
Use NetBeans to write a text file (.txt). File->New File.
Select your Lab1 project, the "Other" category, and the
"Empty File" file type. Click "Next".
-
Name the file Lab1.txt. It should be in the src folder.
Click "Finish".
-
The contents of this text file should explain your
experience in computer science and mathematics and what
attracts you to computer science courses. Paragraphs in the
text should not be indented. Paragraphs should be separated
by a single empty line. Headers should be set off from the
rest of the text by at least one empty line above and below.
Sentences should be separated by two spaces. No line of
text should exceed 70 characters in length. There should be
no hyphenation. There should be no grammar or spelling
errors. Be sure to put your name on it. The file name
should include no spaces. Follow this example.
Part III: Eclipse
Become familiar with Eclipse
Download the latest version
of Eclipse and install it on your machine.
Start a Project in Eclipse
-
Go to File->New->Project and select "Java Project".
-
Name the project Lab1 and choose the directory where
you want its files to be stored. Then click "Next".
-
Unless you know what you are doing, leave all the project
paths as their default values. (You probably don't know
what you're doing at this point so leave them alone.) Click
"Next".
Customize the project
-
Now create a new class: File->New Class...
-
Name the class
Hello. Make the package
blank. The following boxes should be checked:
public, public static void main(String[]
args), and Generate comments. Then click "OK".
-
Eclipse should have written a fair amount of code for you.
Compiling and running
-
Finish writing the code as described above.
-
Now you should be able to run the program. Right-click in
the code pane and select "Run as-->Java Application". The
words "Hello, World." should appear in the Console pane.
Write a text file
-
Use Eclipse to write a text file (.txt).
File->New->Untitled Tex File. Save the file as Lab1.txt.
-
The contents of this text file should explain your
experience in computer science and mathematics and what
attracts you to computer science courses. Paragraphs in the
text should not be indented. Paragraphs should be separated
by a single empty line. Headers should be set off from the
rest of the text by at least one empty line above and below.
Sentences should be separated by two spaces. No line of
text should exceed 70 characters in length. There should be
no hyphenation. There should be no grammar or spelling
errors. Be sure to put your name on it. The file name
should include no spaces. Follow this example.
Turning in the result
-
Make sure the Java SDK
bin folder in on your
PATH or the following won't work.
-
Archive the results using the java archiver "jar."
Put all the files you want to turn in into a new folder
named "lab1". Start a shell (the command prompt on windows
or M-x shell in Emacs) and put your text file into a JAR
file named yourlogin-lab1.jar. Use the following jar
command:
jar cvMf yourlogin-lab1.jar <files>
The c means "create." The v means "verbose."
The M means "don't create a manifest." (The manifest
is simply a hassle. You only need one if you are distributing
applets, packages, or JavaBeans.) The f means that the
next word will be the name of the file to create. Replace
<files> with a space-separated list of files or folders
that you want to add to the archive. In this case it will be
something like
jar cvMf yourlogin-lab1.jar lab1
or
jar cvMf yourlogin-lab1.jar lab1/*.txt lab1/*.java
Warning!!! If you forget to type the name of the jar
file you are writing, then it will overwrite the first file in
the list of files you are trying to archive. That's one of
the reasons it is best to jar a folder and not a list of
files.
Warning!!! You might think that it is simpler to use
Eclipse' built-in jar mechanism. Of course it is but that
won't put the correct files into the archive. Please follow
these directions instead.
I highly recommend you test your jar file before turning it
in. You can test it using:
jar tf yourlogin-lab1.jar
or copy it to another location and extract it yourself using:
jar xvf yourlogin-lab1.jar
When you have finished the lab, just email the resulting JAR
file to me on time.