0% found this document useful (0 votes)
25 views

Eclipse Guide

This document provides instructions for common tasks in Eclipse related to creating and working with Java projects. It covers how to start Eclipse, create a new Java project, generate classes, getters and setters, constructors, run executable classes, import classes, view syntax errors, zip/unzip projects, and more. Tips are also provided for getting method lists, finding matching brackets, and adding Javadocs quickly. The document is organized into sections on basics, tips, and more advanced topics like databases, web projects, and servlets.

Uploaded by

Swapnil Asawa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Eclipse Guide

This document provides instructions for common tasks in Eclipse related to creating and working with Java projects. It covers how to start Eclipse, create a new Java project, generate classes, getters and setters, constructors, run executable classes, import classes, view syntax errors, zip/unzip projects, and more. Tips are also provided for getting method lists, finding matching brackets, and adding Javadocs quickly. The document is organized into sections on basics, tips, and more advanced topics like databases, web projects, and servlets.

Uploaded by

Swapnil Asawa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 49

Eclipse

Developer’s Guide

1
Table of Contents

The Basics

Starting Eclipse …………………………………………………………………………………………………...……… 2


How Do I Create a New Java Project? ………………………………………………………………..…..…… 3
How Do I Create a Generic Java Class? ……………………..……………………………………….……..… 7
How Do I Auto-generate Getters and Setters? ………………………………………………………..……. 9
How Do I Auto-generate a Constructor That Has Parameters That Get Passed In? ….……. 12
How Do I Run an Executable Class? ………………………………………………………………………………. 15
How to Use Quick Fix to Import A Class ……………………………...……………………………………..... 17
Viewing Syntax Errors ……………………………………………………………….………………………………….. 18
How to zip your Java project or Java classes …………………………………………………………………. 19
How to unzip a Java project and import it into Eclipse ………………………………………………….. 21

Tips

Tip: Get a List of Methods for a Class ……………………………………………………………………………. 25


Tip: Get a List of Method Signatures ……………………………………………………………………………… 27
Tip: Find a Matching Bracket …………………………………………………………………………………………. 28
Tip: How to Quickly Add a Javadoc ……………………………………………………………………………….. 29
Beyond The Basics

How Do I Add Database Classes to a Java Project? …………………………….……………...……… 31


How Do I Create a New Static Web Project? …………………………………………..…..………….… 33
How Do I Create a New Dynamic Web Project? ………………………………………………………… 34
How Do I Add Database Classes to a Dynamic Web Project? …………………………………….. 37
How Do I Create a Servlet? ………………………………………………………………………………………… 40
How Do I Add a Tomcat Server? ………………………………………………………………………………… 44
How Do I Add a Project to a Tomcat Server? ……………………………………………………………… 46
Eclipse
The Basics

1
Starting eclipse
When you start eclipse, the first prompt you will get is to
enter the directory for your workspace. This is the area that
you will be creating all of your projects (ex: Hello World When eclipse is finished starting up, you will
project, Calculator project, etc). You can keep the default. see this window. You can close it.

Tip: You might want to create


A shortcut to your eclipse on
your desktop!

2
How Do I Create a New Java Project?

(If you don’t see Java Project, select File > New > Other.
From there select Java Project and click Next.)

Select File > New > Java Project

3
How Do I Create a New Java Project?(step 2)

Give the project a name (Start with a letter. Typically it’s just
using letters (no spaces). You can add numbers (ex:
CalculatorExample2).

JRE should be 1.8

Click Finish

If it prompts you if you want to open it in a new perspective,


click YES.

4
How Do I Create a New Java Project?(cont)

The screen will look like this. If you click the triangle next to your project, it will expand. You will see the following
folders: src, JRE System Library, .settings. You will only be using src. If you try to expand that one, you won’t see
anything since you haven’t created any classes, yet.

5
How Do I Create a Generic Java Class?
Note: If you are creating a servlet (ex: HttpServlet), see “How do I create a servlet?”

Right click src (this holds all of your package and classes).
Select New > Class.

Every class should be in a package. Enter in a package


(typically they start with com). Packages are always
letters in lowercase.

So, if google had a package for utilities, it might be


com.google.utilities.

Then enter a new for the class.

Note: If this class is executable click this


checkbox to create the main class.

Then click Finish.


6
How Do I Create a Generic Java Class?(cont)

7
How Do I Auto-generate Getters and Setters?
First create all of your variables in your class.

8
How Do I Auto-generate Getters and Setters? (cont)
Right click your class. Select Source > Generate Getters and Setters

9
How Do I Auto-generate Getters and Setters? (cont)
If you want to create getters and setters for all variables click “Select All”. (Usually you want to create
getters and setters for all variables). Then select OK.

10
How Do I Auto-generate a Constructor That Has
Parameters That Get Passed In?
Note: By default, all classes have an empty constructor. For example, for this
class you can create a new Employee object by doing:

Employee employee1 = new Employee();

11
How Do I Auto-generate a Constructor That Has
Parameters That Get Passed In? (cont)
Right click the class. Select Source > Generate Constructor using Fields

12
How Do I Auto-generate a Constructor That Has
Parameters That Get Passed In? (cont)
All variables will be selected by
default. If you want to pass in all of
the parameters (you usually do),
just click OK.

Note: When MIGHT you not


pass in a field for a constructor?

Let’s say you have an


employeeId that is
autogenerated and it increments
by one. You won’t want that
parameter passed into the
constructor (also, you won’t
want to have a setEmployeeId
method, either!)
13
How Do I Auto-generate a Constructor That Has
Parameters That Get Passed In? (cont)
Remember, when you create a constructor that passes in parameters, the default constructor is
no longer available. If you ALSO want a constructor that takes NO parameters, you’ll need to
create that as well!

If you want to do this, right


click the class and select
Source > Generate
Constructors from
Superclass

Then click OK.

14
How Do I Run an Executable Class?

If you have a class with a main method, you can run it by right clicking the class and selecting
Run As > Java Application

15
How Do I Run an Executable Class? (cont)

When the class starts running, the console


tab will have a red square. Clicking that red
square will stop the execution.

Any
System.out.println
messages will be
displayed in the
console.

When the program


finishes running, the
square will turn grey
and you will see
<terminated> in the
console bar.

16
How to Use Quick Fix to Import A Class

Right click the red x (in the above example, it’s next to the 5) and select Quick Fix.

Double click the


class you want to
import.

17
Viewing Syntax Errors
You can hover over a red x to see what the error is.

18
How to zip your Java project or Java classes
Right click your package and select Export. Under
the General folder, select Archive File and click
Next.

19
How to zip your Java project or Java classes
For the Coding test, you’ll want to just select the src file. If it’s a web project, you’ll want everything! Make
sure the “Save in zip format” is selected. Change the archive file name or directory if desired. Click Finish.

Java Web
Test Project
=> =>

20
How to Unzip a Java Project and Import it into Eclipse
Right click the file (it’ll probably be in your downloads). Select Extract All…

21
How to Unzip a Java Project and Import it into Eclipse
Browse and look for your workspace. Click OK and extract.

22
How to Unzip a Java Project and Import it into Eclipse
In Eclipse, select: File > Open Projects from File System…

Click “Directory…” and look for the


Project directory in your workspace
(make sure you see a .settings
subdirectory).
Click OK.

Click Finish.

23
Eclipse
Tips

24
Tip: Get a List of Methods for a Class
In this example, we set the object nums to a new ArrayList. To find out what methods are available,
you can type the variable followed by a period and wait.

You will see the list of methods (with their signatures) and more details about that method. You can
scroll through the list of methods. To get information on another method in this list, click it once.
To choose to use that method, click it twice!

25
Tip: Get a List of Methods for a Class (cont)
Many of the most common methods are at the top of the list.

To view the rest of the details for a particular method (the details are on the left side), click tab
twice. You can click the scroll bar next to the methods to scroll through the methods again.

26
Tip: Get a List of Method Signatures
Type the method followed by the open parenthesis. Then press Ctrl + Shift + Space.

In this example, there are three signatures for add. One takes an array of Integer, one takes two ints, and
one takes an ArrayList of Integer.

27
Tip: Find a Matching Bracket
Once you start writing more complicated code, you may have a lot of nested blocks like the code
shown below. If you want to find where the matching bracket is (in this example, we want to see
the starting bracket), put the cursor at the end of a bracket and press

28
Tip: How to Quickly Add a Javadoc
To quickly add a Javadoc, type /** and hit enter!

29
Eclipse
Beyond The Basics

30
How Do I Add Database Classes to a Java Project?

The oracle database classes all reside in a jar file called classes12.jar. To add that jar file to your project,
do the following:

Right click the project and select


Build Path > Configure Build Path

31
How Do I Add Database Classes to a Java Project? (cont)

We’re now using ojdbc6 instead


of clases12.jar!

Click the libraries tab. Click “Add External


JARs”. Go to the directory where your
ojdbc6.jar is located and select it.

Then click Apply and OK.

32
How Do I Create a New Static Web Project?

Static web projects are for simple web apps with (If you don’t see Java Project, select File > New > Other.
HTML, CSS, and Javascript. From there look for the Web folder and open it. Then
select Static Web Project and click Next.)

Select File > New > Static Web Project

33
How Do I Create a New Dynamic Web Project?
Select File > New > Dynamic Web Project (If you don’t see Java Project, select File > New > Other.
From there look for the Web folder and open it. Then
select Dynamic Web Project and click Next.)

34
How Do I Create a New Dynamic Web Project? (cont)
Then type in your project name and select “2.5” from the Dynamic web module version drop down menu.
Click Finish.

If the system prompts you to ask if you want to change the


perspective, click Yes.

35
How Do I Create a New Dynamic Web Project? (cont)
When you expand the project, you will see a lot more folders. Your java source goes under Java Resources >
src. Your HTML, CSS, JS, and JSP goes under WebContent.

Your Dynamic web project


will look for one of these
six files to be under WebContent
in order tostart your application:

• index.html
• index.htm
• index.jsp
• default.html
• default.htm
• default.jsp

36
How Do I Add Database Classes to a Dynamic Web Project?
Using Windows File Explorer, go to the directory where your ojdbc6.jar resides (it might be in your
Downloads folder). Right click it and select copy.

Then go into your Dynamic Web project and under WebContent > WEB-INF > lib, right click there and select
paste!

37
How Do I Add Database Classes to a Dynamic Web Project?
(cont)
Now right click your project and select Build Path > Configure Build Path

Click Add Jar

38
How Do I Add Database Classes to a Dynamic Web Project?
(cont)
Select the location of your ojdbc6.jar and click OK. Then click Apply from the “Java Build Path” window and
then OK.
We’re now
using
ojdbc6.jar
instead of
clases12.jar!

39
How Do I Create a Servlet?
(if you don’t see it, select New > Other . Look for the
Right click src and select New > Servlet Web folder and expand it. Select Servlet)

40
How Do I Create a Servlet? (cont)
Enter in the package name and name of the servlet (the name should end in “Servlet”). Click NEXT!

41
How Do I Create a Servlet? (cont)
Under URL mappings, add the string that matches the action on your form. Click OK and then click Finish

42
How Do I Create a Servlet? (cont)
Note: This will update your web.xml with the servlet information and the servlet-mapping (your web.xml is located under
WebContent > WEB-INF).

Your Class name (including


the package name). There
can only be one class!

The URL pattern that the


action needs to be in
order to use this servlet.

There can be more than


one URL pattern.
Note: The servlet-name matches in the servlet and the servlet-mapping!
43
How Do I Add a Tomcat Server?
Click Window > Show View > Servers.

The server tab will show up in the bottom window.

44
How Do I Add a Tomcat Server? (cont)
Click on the link that says “No servers are available.
Click on this link to create a new server”.
(yeah, it’s a very descriptive link!)

Under the Apache folder, look for your version of


Tomcat. If you want to add your project to the server,
click Next and do steps 2 and 3 on the next page.
Otherwise click Finish.

To start your Tomcat server, right click the server and


select Start.

45
How Do I Add a Project to a Tomcat Server?
Right click the Tomcat server and select Add and Remove. Select your project and click Add. You will see your
project show up under “Configured”. Then click Finish!

46

You might also like