Вы находитесь на странице: 1из 3

Creating a new Java project

This section you create a project. Eventually, will contain a Java package, and the package will
contain a Java class.
1. On the Eclipse menu bar,
choose File. New Project.
You see the New Project dialog, as shown in
the Figure.
Formally, a project is a collection of files and
folders. Intuitively, a project is a basic work unit.
For instance, a self-contained collection of Java
program files to manage your CD collection (along with the files containing the data) may constitute
a single Eclipse project.
2. In the New Project dialog, select Java Project, and then click Next.
You see the New Java Project Wizard, as shown in the Figure.
3. In the Project Name field, type a name for your new
project.
I typed FirstProject. In the steps that follow, I assume
that you also type FirstProject. But if you insist, you can use
dashes, blank spaces, and other troublesome characters.
You have to type a name for your new project. Aside from
typing a name, you can accept the defaults (the Location and
Project Layout stuff) in the New Java Project Wizard.

4. Click Finish.
When you click Finish, the Eclipse workbench reappears.
The leftmost area contains the Package Explorer view. The
views list contains your new FirstProject. (See Figure.)

Creating a package
In the previous section, you create a project to hold your code. The next thing to do is add a package
to your project.
1. In the Package Explorer, right-click the
FirstProject branch. Then, in the resulting
context menu, choose New Package.
The New Java Package Wizard appears, as shown
in Figure.
2. In the New Java Package Wizard, type the name of your new package in the Name text field.
In above Figure, I typed the name com.allmycode.first.
For the package name, youre supposed to reverse your domain
name and then add a descriptive word. In this example I use
com.allmycode.first because Ive registered allmycode.com, and
this is my first example. If you follow this naming convention, other
Java programmers will like you. But if you dont follow this
convention, nothing breaks. For your own use, a package name like
almost.anything.atall (or even a one-part mypack name with no
dots) is just fine.
3. Click Finish to close the New Java Package Wizard.
Your new package (along with some other stuff) appears in the Package
Explorers tree.

Creating and running a Java class


Its time to write some code.
1. In the Package Explorer, right-click your newly created package.
Then, in the resulting context menu, choose New Class. The New Java Class Wizard
miraculously appears, as shown in the Figure.
2. In the New Java Class Wizard, fill in the Name field.
In this Figure, I typed GoodbyeMoon. You can type whatever you darn well please (unless you
want to stay in sync with these instructions).
3. Select other options in the New Java Class Wizard.
For this example, put a check mark in the public static void main(String args[]) box. Aside
from that, just accept the defaults.
4. Click Finish.
You see now the workbench in below Figure. The Package Explorer displays a new
GoodbyeMoon.java file, and the workbenchs middle area displays a Java editor. The Java
editor contains almost all the code in a typical Hello World program. All you need is the
proverbial println call.

5. Add System.out.println(Goodbye Moon!) to the main methods body


By default, Eclipse adds characters as you type. When you type the open parenthesis, Eclipse adds its
own close parenthesis. When you type the quotation mark, Eclipse closes the quotation
automatically.

6. Choose File Save to save your new GoodbyeMoon.java file.


You dont have to tell Eclipse to
compile your code. By default,
Eclipse compiles as you type.
You can turn the feature off. On
Eclipses main menu bar, choose
ProjectBuild Automatically.
Choosing once turns automatic
building off. Choosing again
turns automatic building back on.
Using Eclipse, you can run the
program with only a few mouse
clicks. Choose RunRun
Java Application.
After a brief delay, a new Console view appears in the bottommost area of the Eclipse workbench.

Вам также может понравиться