COMP 110/401 APPENDIX: INSTALLING AND USING ECLIPSE...

126
COMP 110/401 APPENDIX: INSTALLING AND USING ECLIPSE Instructor: Prasun Dewan (FB 150, [email protected])

Transcript of COMP 110/401 APPENDIX: INSTALLING AND USING ECLIPSE...

COMP 110/401

APPENDIX: INSTALLING AND

USING ECLIPSE

Instructor: Prasun Dewan (FB 150, [email protected])

2

SCOPE: BASICS AND BEYOND

Basic use: CS 1Basic use: CS 1

Beyond basic use: CS2Beyond basic use: CS2

3

DOWNLOAD FROM WWW.ECLIPSE.ORG

Get the latest IDE for Java Developers, not Indigo

Get the latest IDE for Java Developers, not Indigo

4

EXTRACT ALL

5

SELECT DESTINATION FOLDER

6

CREATE SOME KIND OF SHORTCUT FOR

ECLIPSE.EXE

7

RUN ECLIPSE.EXE

8

SPECIFY PROJECTS DIRECTORY

9

REMOVE WELCOME TAB IF IT APPEARS

10

ADD A NEW JAVA PROJECT

11

ENTER PROJECT NAME

12

SWITCH TO JAVA PERSPECTIVE (MAY GET THIS

MESSAGE)

13

EMPTY PROJECT CREATED

14

ADD A PACKAGE TO SELECTED PROJECT

Select project and File Package

Select project and File Package

15

ENTER PACKAGE NAME

16

EMPTY PACKAGE IN NON EMPTY PROJECT

17

ADD A CLASS

Select project and File Class

Select project and File Class

18

NAME THE CLASS

19

EMPTY CLASS IN NON EMPTY PACKAGE

20

EDIT THE FILE (COPY AND PASTE CODE)

package warmup;import java.io.BufferedReader;import java.io.InputStreamReader;public class AGreeter {

static BufferedReader inputStream = new BufferedReader(new InputStreamReader(System.in));public static void main(String[] args) {System.out.println("Hello World");if (args.length > 0) {

System.out.println(args[0]);}try {

System.out.println (inputStream.readLine());} catch (Exception e) {

System.out.println("Could not read line");e.printStackTrace();

}}}

Copy and paste code from:Copy and paste code from:

21

SAVE (AND COMPILE) THE FILE

FileSave or CTRL+SFileSave or CTRL+S

22

WORKSPACE, PROJECT, SOURCE PACKAGE

FOLDER

Source PackageSource

PackageProjectProjectWorkspaceWorkspace

Class SourceClass Source

23

WORKSPACE, PROJECT, BINARY PACKAGE

FOLDER

Binary PackageBinary

PackageProjectProjectWorkspaceWorkspace

Class Object (Binary) CodeClass Object

(Binary) Code

24

EXECUTE THE MAIN METHOD ON SELECTED

CLASS

Right MenuDebug As Java Application

Right MenuDebug As Java Application

Can also select Run but Debug is more defensive

Can also select Run but Debug is more defensive

25

FIND OR SWITCH WORKSPACE FOLDER:

FILESWITCH WORKSPACE

26

FIND PROJECT FOLDER: SELECT PROJECT,

RIGHTCLICKPROPERTIESRESOURCE

27

VIEW OUTPUT IN CONSOLE WINDOW

28

PROVIDE INPUT IN CONSOLE WINDOW

29

THE OUTPUT AFTER ENTER

30

SUPPLY USER ARGUMENT:

Right MenuDebug As Debug ConfigurationRight MenuDebug As Debug Configuration

31

SELECT MAIN CLASS IN CONFIGURATION

32

SPECIFY AND APPLY ARGUMENT IN ARGUMENT

TAB AND PRESS DEBUG

Argument must be in

quotes. Args[0] = “Ca

Va”

Argument must be in

quotes. Args[0] = “Ca

Va”

Without quotes Java

will make each word a

separate argument

(args[0] = “Ca”, args[1]

= “Va”

Without quotes Java

will make each word a

separate argument

(args[0] = “Ca”, args[1]

= “Va”

33

EXECUTE WITH SUPPLIED ARG

34

ARG PRINTED, PROGRAM, STILL RUNNING,

WAITING FOR INPUT

Button available in

both Debug and Java

Perspective, only of

them is active

Button available in

both Debug and Java

Perspective, only of

them is active

35

TERMINATED PROGRAM

36

DEBUG LAST MAIN LAUNCHED: F11

No need to select a

main class

No need to select a

main class

37

CLOSING A WINDOW PERHAPS ACCIDENTALLY

38

VANISHED CONSOLE

39

WINDOWRESET PERSPECTIVE

40

WINDOWSHOW VIEW

Can selectively remove and

add sub-windows/views

Can selectively remove and

add sub-windows/views

41

TARGET

Basic useBasic use

Beyond basic use (ignore for 401)

Beyond basic use (ignore for 401)

42

CREATE A PROJECT FROM EXISTING CODE

Unzip the zipped directoryUnzip the zipped directory

Zipped DirectoryJavaToHTML

Source Code of Class Examples

Click on Zipper Directory Link

in SlideShow Mode

Click on Zipper Directory Link

in SlideShow Mode

Make a Java project out of it

by following the steps

described

Make a Java project out of it

by following the steps

described

43

CREATE A PROJECT OUT OF EXISTING CODE:

FILENEWJAVA PROJECT

44

UNCHECK DEFAULT LOCATION BOX

45

ENTER LOCATION AND NAME

46

NEW PROJECT CREATED

But it will have errors because of missing

ObjectEditor library

But it will have errors because of missing

ObjectEditor library

47

LIBRARY LOCATION ON THE WEB

Downloads

Click on the latest version (on my web page,

not this page) to download the jar (same as

zip) and do not unzip/unjar it

Click on the latest version (on my web page,

not this page) to download the jar (same as

zip) and do not unzip/unjar it

48

DOWNLOAD LIBRARY TO LOCAL COMPUTER

Do not unzip/unjar itDo not unzip/unjar it

49

SELECT PROJECT, AND PROJECTPROPERTIES

Must do this for each project!Must do this for each project!

50

SELECT BUILD PATH, ADD EXTERNAL JAR

51

SELECT SAVED JAR

52

SELECT BUILD PATH, PROJECTS

53

REMOVE SPURIOUS PROJECTS

54

PRESS OK TO COMMIT

55

CHANGE THE JAVA VERSION (IF NECESSARY):

WINDOWPREFERENCES

56

JAVACOMPILER PREFERENCES

57

REBUILD OR RECOMPILE ALL CLASSES

58

EXECUTING CODE GIVES JAVA, JAVAC

INCOMPATIBILITY ERROR

59

CHANGE THE JAVA VERSION:

WINDOWPREFERENCES

60

PRESS SEARCH BROWSE BUTTON

61

PRESS SEARCH BROWSE BUTTON TO INSTALLED

JDK (SEE APPENDIX ON INSTALLATION)

62

JRE ADDED TO ECLIPSE, SELECT IT AND CAN

EXECUTE WITHOUT ERRORS

63

SOME EXPLICIT INSTRUCTIONS – FOR MAC

You can get that for mac here:http://download.oracle.com/otn-pub/java/jdk/7u40-b43/jdk-7u40-macosx-x64.dmg

The problem is Eclipse won't change the compiler to use 1.7 so you will have to specify that under:Eclipse -> Preferences -> Java -> Installed JREs

Click "Add..." then choose "MacOS X VM"

Next, then under directory copy/paste this:

/Library/Java/JavaVirtualMachines/jdk1.7.0_40.jdk/Contents/Home

Make sure it recognizes this as Java SE 7. Then name doesn't really matter.

Now go to:Eclipse -> Preferences -> Java -> CompilerChange the compliance level to: "1.7"

Now it probably still won't work (at least it didn't on mine)This is because your project is still set on 1.6

So, go to:

Project -> Properties -> Java Build Path -> LibrariesAdd Library...

JRE System Library

"Workspace default" should be automatically there as the 1.7, but if not find the 1.7 library.Then remove the 1.6 library

Also you need to change the compiler being used by the project:Project -> Properties -> Java Compilerand make sure everything is on 1.7

Now, if you're getting an error about the main class not being found or working.

You may just want to create a new project and when you select the 1.7 JRE.

64

DELETING A PROJECT

Right click project and delete. Usually not a good idea to delete

project on disk, can always use the OS to do so, which will put it in the

recycling bin

Right click project and delete. Usually not a good idea to delete

project on disk, can always use the OS to do so, which will put it in the

recycling bin

65

COPY TYPES (CLASSES, INTERFACES, ENUMS)

FROM PACKAGE TO ANOTHER: COPY

Select type(s) in source project and package and press CTRL-C

(or FileCopy right menu item)

Select type(s) in source project and package and press CTRL-C

(or FileCopy right menu item)

66

COPY TYPES FROM ONE PACKAGE TO ANOTHER:

PASTE

Select package in destination project and package and press

CTRL-V (or FilePaste right menu item)

Select package in destination project and package and press

CTRL-V (or FilePaste right menu item)

67

COPYING CLASSES FROM ONE PACKAGE TO

ANOTHER: NEW CLASS IN NEW PACKAGE AND

PROJECT

Repackaged type added (and its imports are changed to other copied types if multiple types are copied and

pasted together)

Repackaged type added (and its imports are changed to other copied types if multiple types are copied and

pasted together)

68

JUMPING TO IDENTIFIER DEFINITION AND

NAVIGATION STACK

F3 when cursor is a start of an identifier

F3 when cursor is a start of an identifier

Makes Eclipse take you to the definition of the

identifier in an edit window

Makes Eclipse take you to the definition of the

identifier in an edit window

ALT + Back Arrow takes you back where you

came from, a la Browser back button, can execute multiple

times to back in in the stack of visited points

ALT + Back Arrow takes you back where you

came from, a la Browser back button, can execute multiple

times to back in in the stack of visited points

ALT + Forward Arrow goes forwards in the navigation stack a la

browser forward button

ALT + Forward Arrow goes forwards in the navigation stack a la

browser forward button

69

FIND THE USES

CTRL-SHIFT-G when cursor is a start of an identifier

CTRL-SHIFT-G when cursor is a start of an identifier

Makes Eclipse show the methods that reference the identifier in the search tab

Makes Eclipse show the methods that reference the identifier in the search tab

Double clicking a method reference

Double clicking a method reference

Makes Eclipse show the method and highlight all references of

the identifier

Makes Eclipse show the method and highlight all references of

the identifier

70

MULTI FILE TEXT SEARCH FOR PRINTLN

71

SEARCH RESULTS

Use CTR+SHIFT+G (shown earlier) to find

active uses of the method

Use CTR+SHIFT+G (shown earlier) to find

active uses of the method

72

MENUREFACTORRENAME IDENTIFIER: ALT +

SHIFT + R

Select type, right menuRefactorRename

Select type, right menuRefactorRename

All references will be updated!All references will be updated!

73

COMPLETE IDENTIFIER: CTRL + SPACE AFTER

PREFIX

public static final long NUMBER_OF_LARGE_FACTORIAL_COMPUTATIONS= 1000000;

Give file nameGive file name

74

UNDERSTAND AND CORRECT ERRORS

75

CORRECT ALL IMPORT ERRORS IN FILE: CTRL +

SHIFT + O

In Eclipse press CTRL-SHIFT-O to

automatically import all used types that

need to be imported but have not been

imported

In Eclipse press CTRL-SHIFT-O to

automatically import all used types that

need to be imported but have not been

imported

If class is in more than one package,

Eclipse gives a choice

If class is in more than one package,

Eclipse gives a choice

All unused imports are also removedAll unused imports are also removed

Can select multiple types or a project to

refresh all imports in the selected

types/project

Can select multiple types or a project to

refresh all imports in the selected

types/project

76

SET A BREAK POINT OR TEL ECLIPSE TO STOP

WHEN IT REACHED SOME LINE

Double clicking again will remove dot and

break point

Double clicking again will remove dot and

break point

Can also use right menu in side bar to

enable and disable break point

Can also use right menu in side bar to

enable and disable break point

77

DEBUG PROGRAM AND ENTER INPUT

78

MAY GET THIS MESSAGE BEFORE BREAK POINT

HIT

79

DEBUG PERSPECTIVE AND STOPPING AT

BREAKPOINT

80

RUNSTEP OVER TO NEXT STATEMENT (F6)

81

STOPPED AT NEXT STATEMENT

Local variable

82

Call stackCalled method

parameters

RUNSTEP INTO SETNUMBER (F5)

83

RUNSTEP RETURN OR F7

Call stack

84

STEP OVER AND THEN STEP INTO GETFACTORIAL

Elided (Collapsed) Instance Vars

85

EXAMINE VARIABLES

Expanded Instance Vars

86

STEP INTO RECURSINGFACTORIAL

87

STEP OVER IF

88

STEP INTO RECURSIVE CALL

89

EXAMINE PARENT CALL

90

STEP OVER IF

91

STEP INTO YET ANOTHER RECURSIVE CALL

92

RUNRESUME TO NEXT BREAK POINT OR USER

INPUT (NO MORE SINGLE STEPPING)

93

ENTER NEGATIVE NUMBER TO TERMINATE

94

STOPPING AGAIN AT BREAKPOINT

95

TERMINATED IN DEBUG PERSPECTIVE

96

RETURN TO JAVA PERSPECTIVE

97

JUNIT: NEW CLASS WITHOUT MAIN

98

JUNIT: INSTALL JUNIT

99

JUNIT ADDED AND IMPORTED

10

0

RUN JUNIT

10

1

JUNIT RESULT

10

2

RETURNING TO JAVA PERSPECTIVE

10

3

10

4

FINDING THE DEFINITION

CTRL + SHIFT + G before a identifierCTRL + SHIFT + G

before a identifier

10

5

FINDING THE DEFINITION

F3 before a useF3 before a use

10

6

EXAMINING MAIN PARAMETERS

Main

Parameters

10

7

NEW OUTPUT AND LOCAL VARIABLE

Index gets

initialized

println()

executed

10

8

STEP OVER

10

9

STEP INTO NEXT LINE (SAME AS STEP OVER)

11

0

TERMINATING RUNNING PROGRAM

11

1

Call stackCalled method

parameters

STEP INTO SETNUMBER (RUNSTEP INTO OR F5)

11

2

STEP RETURN STEP RETURN OR F7

Call stack

11

3

STEP OVER AND THEN STREP INTO GETFACTORIAL

Elided (Collapsed) Instance Vars

11

4

EXAMINING VARIABLES

Expanded Instance Vars

11

5

STEP INTO RECURSINGFACTORIAL

11

6

STEP OVER IF

11

7

STEP INTO RECURSIVE CALL

11

8

EXAMINING PARENT CALL

11

9

STEP OVER

12

0

STEP INTO YET ANOTHER RECURSIVE CALL

12

1

RESUME TO NEXT BREAK POINT OR USER INPUT

(NO MORE SINGLE STEPPING)

12

2

ENTER NEGATIVE NUMBER TO TERMINATE

12

3

STOPPING AGAIN AT BREAKPOINT

12

4

TERMINATED

12

5

RETURNING TO JAVA PERSPECTIVE

12

6

TWO OTHER USEFUL BUT COMPLICATED

REFACTOR OPTIONS

Change method signatureChange method signature

Will try to update references to match the signature, which can be erroneous

Will try to update references to match the signature, which can be erroneous

Maybe better to get all references and update them individually for both purposes

Maybe better to get all references and update them individually for both purposes

Extract interface of class CExtract interface of class C

Will create interface of selected public methods and will make the interface the type of all variables

declared to be of class type

Will create interface of selected public methods and will make the interface the type of all variables

declared to be of class type

Adds potentially unwanted comments to method headers and sometimes updates to references have

not worked in the past for me

Adds potentially unwanted comments to method headers and sometimes updates to references have

not worked in the past for me