Building Projects with Java IDEs
Eclipse
- First, you'll need to download Eclipse to your computer:
- You also must have the Java Standard Development Kit installed on your
computer
- Eclipse doesn't have to be "installed":
- Unzip the downloaded archive, wherever you want the program
located. (You could even put it on a flash drive, for example
- Open the folder containing Eclipse
- Double-click the "Eclipse.exe" file to run it
- Recommended: Create a shortcut on your desktop to the
application
Creating a Project File for a simple Java application
- From the "File" menu, pick "New", then "Java Project". This opens
a dialog box for you to select your settings
- Type a project name in the "Project Name" box
- Under Contents
- Select "Create new project in workspace" if you are starting a new
project from scratch
- Select "Create project from existing source" if you want to open up
an existing source code file as a new project. This is especially
useful for running any existing code examples that you've downloaded
from the course web site
- Default settings under "JRE" are fine
- Recommend choosing "Use project folder as root for sources and class
files" at the start, for simple examples and assignments. This will place
your resulting class files in the same folder as your source.
- Once we learn about the "package" directive, you may want to start
using the other setting option
- Clicking "Next" will take you to another screen with other settings
options. You shouldn't need those early in the course
- Clicking "Finish" will create your project file.
Creating source code files to add to your project
- "File" menu -- "New" -- pick the type of file to create. Usually this
will be "Class"
- Dialog box will come up with some options. At the very least, you'll
need to type thge name of the class into the "Name" box
- Other options will become more clear as we get to new topics
- If you want this class to have a "main()" in it, you can click the
appropriate box under "Which method stubs would you like", and the
framework of main() will be auto-inserted for you
- Once you click "Finish", the file will open in the built-in text
editor. Type your code, and make sure to "Save"
Compiling and running
To compile and test run your project from within NetBeans:
- Select "Run" from the "Run" menu
- There is also a green arrow button on the toolbar.
This is a shortcut for the same operation
- Keyboard shortcut: Ctrl + F11
- Note that any console I/O will show up in an "Output" window at the
bottom of the screen
- Any GUI output will pop up in separate windows
NetBeans
First, you'll need to have the NetBeans program installed on your
computer. Get this from sun.com:
Creating a Project File for a simple Java application
- From the "File" menu, select "New Project"
- You'll see a pop-up dialog window that asks what kind of project. For
now, go ahead and pick "Java Application" (which should be the default
selection. Click "Next".
- The next screen of the dialog gives you some choices to fill out. For
the initial projects in this course, you don't need any overly advanced
options. Here are the recommendations:
- Go ahead and choose a project name, and type it in the box "Project
Name".
- There will be a default path suggested under "Project Location". You
can take this one, or you can use the "Browse" button to pick some
other location in your file system
- If you want the project to start you off with a file with a "main()
function in it, go ahead and check the "Create Main Class" button
- IMPORTANT: IF you check this box (Create Main Class),
JavaBeans will try to give you a recommended default name for the main
class, usually something like "myproject.Main". The default usually places
your code inside a "package". This is more complex than you need right
now.
- We will talk about how to create packages later in the course
- You should not place your code into packages in the early
assignments
- If you are supposed to create a main program in a class called, for
example, "Program1" -- then just type "Program1" as the name in this
box (the Create Main Class) box
- Click "Finish" on the current dialog box
- NetBeans will open up your project. A window on the left will show
files currently in your project. A window on the right will display
source code files you have open. A starter code file will appear for you
to start typing code in.
Adding more source code files to your project
Some programs will involve writing multiple classes, possibly in multiple
files. To start a new code file within the currently open project:
- Select "New File" from the "File" menu
- In the dialog box that comes up, pick the type of file, then click
"Next".
- This box lets you pick a category of file, then the specific
type
- usually, this will be simply "Java class", although later we will
learn some other special types of classes, which map to some of the
other options
- On the next screen, type the class name in the appropriate box
- Other settings on this window
- Location should be "source packages"
- For now, accepting the "default package" is fine. Note that this
window has a warning against that, but again -- we'll talk about
creating packages later in the course
- This will open up another java code file for you to edit. Make
sure to save after you add code (File menu, "Save")
Compiling and running
To compile and test run your project from within NetBeans:
- Select "Run Main Project" from the "Run" menu
- There is also a green arrow button (like the Play button on a DVD
player) on the toolbar. This is a shortcut for the same
operation
- Keyboard shortcut: F6
- Note that any console I/O will show up in an "Output" window at the
bottom of the screen
- Any GUI output will pop up in separate windows