eclipse Tips

Download Report

Transcript eclipse Tips

eclipse Tips
1
What is eclipse?
• Eclipse is a popular IDE (Integrated Development
Environment) that we will use to create, compile,
execute, and test our Java code.
• It has many advanced features that make it an ideal
tool not only for the academic setting but also for
the corporate world.
• We will be using only a small subset of its
capabilities. To learn more about eclipse, visit
their website at www.eclipse.org, or take
advantage of its extensive help features.
2
eclipse
3
Installing eclipse
• Note: You can skip this step on the school
computer, since Java and eclipse are already
installed on it
• Prerequisite for eclipse: You need the Java
Runtime Environment (JRE) in order to use
eclipse
– More than likely the JRE is already installed on
your computer
– If not, click on this link and follow the instructions:
Download JRE
4
Installing eclipse
(continued)
• Download eclipse (or copy it from Mr. Smith). Pick
one of the 2 options below:
– To download, follow these instructions
– Or you can copy eclipse from Mr. Smith to your flash drive if
you are using a Windows computer
5
Jar files
• Jar files are libraries that store compiled code
that you can use in your own programs.
• At school, copy the jar files to your H: drive. One
recommendation is to create a folder named
H:\JarFiles and copy any needed jar files to this
folder.
• Note: The KarelJRobot.jar file is located on my
website. You can download it from my Daily
Lesson Plan page. You can copy this file to
H:\JarFiles for later use in programs.
6
• workspace
– Projects
• Packages
workspace
– Classes
• A workspace is used to store all of your Java
classes.
• At school, create a workspace on your H:
drive. Do this by creating a new folder
named H:\workspace .
• To begin with, there should be no files in
this folder
7
Launching eclipse
• To launch eclipse at school, select Start > Programs
> Student Applications > eclipse
To launch eclipse at home, double-click on the
eclipse.exe file.
• When you first open eclipse, it may ask you to
select the workspace. While at school, select
H:\workspace. While at home, select the workspace
folder you created.
• If a Welcome screen appears, you can either browse
through some of the topics or close it to get to the
main eclipse windows.
8
• workspace
– Projects
• Packages
Projects
– Classes
• A Java project contains source code and related files
for building a Java program. The Java project is a
folder underneath the workspace folder.
• To create a new Java Project:
– Select File > New > Java Project
– Enter the name of the project
– All radio buttons should be on their top option
– Remember to include the External jar file (listed under the Libraries
tab > Add External JARs). Note: While at school, the jar file should
be located on your H: drive
– Press “Finish”.
9
• workspace
– Projects
• Packages
Packages
– Classes
• A Java package is used to organize related
Java classes. It is a folder underneath the
Java project folder.
• To create a new Java Package:
– Single click on the project
– Select File > New > Package
– Enter the package name.
– Press “Finish”.
10
• workspace
– Projects
• Packages
Classes
– Classes
• A Java class is the programming code. The
source code has an extension of .java and is
located in the package folder. It is a text
file and can be edited with any text editor.
• To create a new Java class:
– Single click on the package
– Select File > New > Class
– Enter the class name.
– Press “Finish”.
11
• workspace
– Projects
• Packages
Saving/Compiling/
Running a class
– Classes
• A class can always be saved, even if there are
errors. To do this, press the Save button.
• Saving the class will automatically try to compile it.
If it is free from errors, it will be compiled
successfully and a file with the extension .class will
be created in thhe package folder.
• To run a new Java class:
– Note: The class must have a main method to be run.
– Make the class the active class: Double click on it in the
Package explorer to make it active in the window.
– Select Run > Run as > Java application
12
• workspace
– Projects
• Packages
– Classes
Proper
Indentation
• It is good practice to indent code so that a human
can more easily read it. The methods within a class
should be indented as well as the statements within
a method.
• Eclipse can help you with indentation:
– Show your class in the active window.
– Optionally, select the lines of code to be indented.
Otherwise, the entire class will be corrected.
– Select Source > Correct Indentation
– The end result is that your code should be indented
properly.
13
Copying Classes between
School and Home
• Since there are times when you will want to continue your work at
home, you will need to be able to transfer your code back and forth.
This is where your flash drive becomes handy.
• To reduce confusion, keep the folder structure on your flash drive the
same as the folder structure on H:\workspace.
• Use file manager to locate the Java source file (that has extension
.java) within the package folder.
• Copy only the .java files (not the .class files) to your flash drive
folder.
• You can then copy these files to your package folder on your
destination computer.
• Note: After you launch eclipse, it is important to refresh the
project and package. Otherwise, the new code may not be seen
within eclipse:
–
Single click on the project and select File > Refresh (or press F5).
–
Single click on the package and select File > Refresh (or press F5).
14