Sunteți pe pagina 1din 19

Java Development with Eclipse and MyEclipse

Step 1: Install a local JDK

1.) Although you can choose to put it where you want, it makes a lot of sense for all the
developers on one team to use the same directory structure for our development. I will
use C:\dev throughout this guide. If you have chosen to use something else, adjust your
paths accordingly

2.) Although our production is based on Java 1.4.x and we can set up Eclipse to be compliant
to that same JDK, it makes sense to all download JDK1.5.0_10. You absolutely can
configure Eclipse to only ever allow code that matches Java 1.4. However, in the future,
we may want to jump to five and this way it’s just a configuration setting.

Go to this link: http://java.sun.com/javase/downloads/index_jdk5.jsp

Accept the license and choose this one:

Install the JDK (the full JDK, not just the JRE) into the following directory

3.) After you have installed Java 5, it makes sense to change the root directory to something
simple like I have here. Your choice.
4.) Go to the Windows icon My Computer and right-mouse click. From the context menu,
select properties and then the Advanced tab. At the bottom, select “Environment
Variables” Add a new one called: “JAVA_HOME”, as you see in this example.
Obviously, if you did not simplify the name of your Java home directory, it should match.

Step 2: Install Eclipse/MyEclipse and Configure

5.) You absolutely can use just Eclipse without adding on MyEclipse. The main reason for
adding MyEclipse is for the way it adds several productivity-enhancing features and
removes some annoyances. It makes the plug-ins work together out of the box.

6.) First register for MyEclipse: We all will be using a free 30-day trial until funding goes
through.
http://www.myeclipseide.com/user.php?op=register&module=NS-NewUser

7.) Then, go to this page to download the IDE:


http://www.myeclipseide.com/module-htmlpages-display-pid-4.html
8.) You will want to download Eclipse 3.2 along with MyEclipse 5.1. Look for this icon and
please choose “Latest Release”

9.) Choose the All-in-One install and Save it to your Windows file system.

10.) I suggest that you install MyEclipse into a new folder under your C:\dev
directory called Eclipse_Europa, which is the version of Eclipse underlying
MyEclipse.

11.) After you have installed MyEclipse/Eclipse, you should find the icon I’ve selected
in this location. Double click on this. When eclipse pops up, it will ask you to select a
workspace, or the place where it will store your Java source and associated files. I highly
recommend you do not accept the default location. Instead, create a directory (often
prefaced with an underscore so it is visible at the top of your directory)
a. You should have a different workspace for each separate application—web or
otherwise—that you work on. It’s really easy to swap and back forth between
workspaces. Also, then you can deploy one web app at a time. In this screen cap,
you see that I have a workspace devoted to C:\_GodwhaleWeb
12.) MyEclipse will come up with a Welcome screen such as this, that you should just
dismiss.
13.) Next, it’s very important to follow what I suggest to avoid becoming frustrated.
You will see the following screen, which is the raw development environment.

Right now we’re looking at the MyEclipse perspective. In Eclipse there are a dozen
different perspectives, which are each organized around a particular type of work. This
one is for writing new code. There is a Debug perspective, a Database Explorer
perspective, etc. You switch between them by clicking on the yellow plus sign.

Any perspective has a bunch of smaller


windows called “Views.” I generally
close most of these to give myself more
screen real estate. Clicking on the blue
header of any view toggles it between full
screen and a smaller view. I usually don’t
use the Outline, Properties, Image or
Snippets view. If you ever lose a view
and want it back, go to the pull-down
menu Window, Show View.
14.) Before we can write any Java code, we need this non-Java concept of a project,
which is merely a slick way to organize our Java code and the many small files that go
with it. Since we’re working on Web Applications here—generally—we want to use the
ready-made Web project (another plus of MyEclipse) called a Web Project.
15.) The next wizard screen will ask you to name your project. That will end up being
a subdirectory within your _BN1_E workspace file. If you don’t change it, that project
name will also end up being your web application’s context root.
This default Web Project will create a structure like this:

Source code goes here. Before you add a


Java class, you will first add a package by
right-clicking on this directory and adding
a new package.

If you want to add a JSP, you add it to the WebRoot. Right-mouse click and it says “New JSP”.
In this example, though, I already have some code.
How to Add a Jar File

This next bit is very important and is the part that can be frustrating if you don’t do it
right. If you have a Jar file you want to add to the project, your inclination might be to just go to
the place in the directory structure and put it where you need it. BAD IDEA. Instead, you should
but it somewhere outside your workspace and do this. Click on the project and Import the jar
file.

Pick General and then File System

Browse in your file system and pick the directory where your Jar is located. You won’t be able to
see it yet.

In the next screen, you will be able to pick any Jar you want.
Now, we see that indeed the Jar in question has been imported but it is not yet used by our
project, nor is it on the .classpath yet.

Right-mouse click on the Project and choose properties


Select Java Build path and “Add Jars” from the Libraries tab.

It will only show you Jars that you don’t already have. Select the jar and you’re done.

Notice now it moves to a different place to indicate it is on the classpath.

I have added a JSP to WebRoot using the automatic creation and we see one of the perks of
MyEclipse, the ability to design with a virtually WYSIWYG tool:
When I used the MyEclipse auto create to make a servlet, I also clicked on a linking feature that
takes you to the source of any file you click on. This is good and should be used. Also, without
me doing anything except using the wizard to create the new servlet, it automatically configured
the web.xml for me.

Also, I prefer the Hierarchical layout of the packages:


If my Eclipse detects any errors, clicking on the error or warning takes you right to the spot

Eclipse Short Cuts:

CTRL + SHIFT + T = it brings up a Type (as in Java type) browser. You can open the original
source for any Type with this command. Very convenient.

CTRL + SHIFT + R = it brings up a Resource (meaning every file that is not a Type) in your
project.

Add a new Type to your Java class?

In this case, my class is lacking: import java.util.HashSet

Within the class, type CTRL + SHIFT + O and it both adds any needed import (asking you to
clarify any ambiguities) or removes any imports that are not being used. Then, it alerts you if you
have any orphan references:
This is really cool and is a great friend to a lazy programmer. Say you have a block of code that
has gotten hairy and you would like to make a method out of it. Highlight the lines you would
like to send to a method:

Type ALT + SHIFT + M and it will ask you for the method name. (If the block has more than
one return, it will refuse to make a method while explaining why.

Eclipse is a bundle of timesaving aids like this. You don’t need to use any of them but they will
save you buckets of time if you do.
Step 3: Install A Local Copy of Jrun40

MyEclipse has a great aid in that it comes pre-configured for things like Application Servers
—including JRun40, fortunately. [I much prefer Jboss but the company where I wrote this
only permitted JRun…]

First, if you are a developer, you can have a permanent developer’s copy of JRun on your
local machine—as long as you only access it by one IP number.

Go to this place: https://www.adobe.com/cfusion/tdrc/index.cfm?product=jrun


And register as a user. Then, it will take you here:

Save it to your local file system and install it—my recommendation—to your C:\dev
folder.

Next, go back to My Computer, right-mouse click and select properties. Then, choose the
advanced tab and Environment variables. Enter a JRUN_HOME that matches where you
loaded it. See this example.

While you’re there, you might want to add the Java Bin directory to the end of your PATH
environment variable. Although Oracle generally puts its crappy old JDK at the front of the
path, it will work if you do this:
Last step. We need to associate our local JRun instance in MyEclipse so it automatically
deploys to it, and so we can start and stop it from within Eclipse.

Within Eclipse, go to Windows | Preferences

Go to MyEclipse, Application Servers and JRun 4


Then, be certain you choose the radio button “Enable” and locate your instance of JRun.

To deploy the project you were working on to JRun, you click on the deploy button. It
automatically deploys your project as an EAR to your chosen application server. Yet another
sweet benefit of MyEclipse.

Choose Add and here’s where it goes:


To start up that server, you click on the button to the right of the deploy button and follow the
directions:

After you have started your server, you will want to switch to the Debug perspective

By clicking on the purple margin of your page, you can set a breakpoint. When someone
calls up your webapp’s URL, it will stop during execution at this point and you can inspect
the contents of variables to your heart’s content. You can step through code or anything.
You can look within complex data structures to your heart’s content and—I hope I need not
add—this is immensely more efficient than adding System.out.println() statements
galore. The developer productivity boost more than pays for the $52.00 price of MyEclipse.

S-ar putea să vă placă și