1
1
10403 Lab and Eclipse Introduction
(Last updated by asanchez, jcomer 8/22/16.)
I. Access and check your student account.
1. Log into the TCU network with your TCU student account Username and Password. (If your
password does not work, try your original password - DDMMMYY of your birthday. For
example, 05MAR79.)
Username:
Password:
Domain: TCU
If at anytime during the semester you have problems with your account or password you
should go to Information Commons Help Desk on the first floor of the TCU Library. The
phone number to the Help Desk is X5855. If you have Internet access, you can reset the
password on your account by accessing https://mypw.tcu.edu/pwreset/.
2. Take a moment to familiarize yourself with the Windows XP desktop. Find each of the
applications noted below. You should realize that the desktop might look a little different on
different computers.
Internet Explorer – The web browser you should always use to check the execution of your
Java applet on the web.
ECLIPSE IDE - The integrated development environment (IDE) used to create your Java
programs. This program is available from the Start menu and may also be available from a
desktop shortcut labeled “Computer Science Applications”.
TURNIN - A program used to submit your Java source code for grading. This program is
available from the Start menu and may also be available from a desktop shortcut.
3. Double-click on My Computer to access the various resources of this machine. Besides the
usual C: and D: drives you should see a shared W: “//studentweb/” with your username.
This is your TCU student drive space and the location you will store your work for
grading. You will be able to access it from any computer connected to the TCU network or
that can connect to the Internet.
IF YOU CANNOT LOGIN OR DO NOT SEE YOUR W: DRIVE, STOP HERE AND GET
ASSISTANCE IMMEDIATELY! (Your account may not be set up correctly yet.)
4. It is wise to occasionally check the available storage space of your student account on your
W: drive. To check the space you can open the W: drive (by double-clicking), from the Edit
menu select “Select All”, right-click on the highlighted icons and select “Properties” from the
drop-down menu. You will be shown the amount of used space on your account. Make sure to
always keep plenty of available space for saving lab projects and your email. Most student
accounts are given 50 Mbytes (approx. 50,000,000 bytes) of storage. If your space quota is
exceeded you will not be able to run and save your labs correctly!
2
2
II. Prepare a wwwpub directory on your W: drive. (Done only ONCE.) 1. At
this
point
you
need
to
obtain
some
files
to
set
up
a
WWWPUB
directory
on
your
W:
drive.
(Note:
you
must
be
in
one
of
the
labs
on-‐campus
in
order
to
perform
the
following
steps).
For
Windows
8:
Right
click
the
bottom
left
Windows
icon
and
select
the
Run
application
and
selected
it
should
look
as
follows:
Type
in
in
the
search
\\tcu.edu\dfs1\lab_common\COSC_10403
and
hit
OK.
The
window
shown
below
should
appear.
For
Windows
7:
Go
to
the
MS
bottom
flag
or
start
Button
and
Select
Run.
Type
in
\\labsrv1\lab_common\COSC_10403
or
\\tcu.edu\dfs1\lab_common\COSC_10403
and
hit
OK.
The
window
shown
below
should
appear.
2.
Open
MyComputer
by
double-‐clicking
on
the
icon.
3.
Inside
the
window
just
opened
for
MyComputer,
open
your
W:
drive.
If
you
already
have
a
wwwpub
directory
on
your
w:
drive
delete
the
wwwpub
file;
otherwise,
continue
with
Step
4
below.
3
3
4.
Copy
the
entire
WWWPUB
(not
the
WWWPUB.zip)
directory
and
its
contents
from
COSC_10403
onto
your
W:
drive.
NOTE
that
if
in
your
W:
there
is
already
a
previous
wwwpub
folder
you
should
delete
this
one
and
copy
the
new
one
5.
Close
your
W:
drive.
Skip
to
Step
7.
6.
Do
this
step
ONLY
if
you
did
not
do
Steps
4-‐5
above
(because
you
already
had
a
WWWPUB
directory
on
your
W:
drive).
Open
WWWPUB
in
the
COSC_10403
directory.
Select
all
items
inside
the
WWWPUB
directory
in
COSC_10403
and
drag
them
into
the
already
existing
WWWPUB
folder
on
your
W:
drive.
Close
your
W:
drive.
7.
Now
let’s
verify
that
WWWPUB
has
been
created
correctly.
Open
your
W:
drive
by
double-‐clicking.
Open
WWWPUB
directory
on
your
W:
drive.
You
should
see
Lab0-‐9
folders
and
a
10403labs.html
icon
as
shown
below.
If
you
do
not
see
a
window
with
the
Address
and
contents
EXACTLY
as
shown
below,
ask
for
assistance.
Currently
the
folders
are
all
empty.
As
you
finish
each
lab
this
semester,
you
will
place
specified
files
in
the
appropriate
lab
folder.
These
files
can
then
be
accessed
from
a
web
browser
to
demonstrate
the
execution
of
your
Java
applet
for
grading.
8.
Close
all
windows
except
these
instructions.
4
4
III. Use Eclipse (version NEON) to create and run a Java applet.
Eclipse is an open source IDE (integrated development environment) available to programmers
who want to develop Java projects. Since it is an IDE, the tools needed for editing, compiling,
building, debugging, executing, and obtaining online help are all integrated in one graphical user
interface. Using these tools effectively can greatly increase your productivity. However there are
many options available and you may get easily confused; follow these instructions to gain an initial
feel for the IDE, later you can spend time using the various options. The current version of Eclipse
are using is MARS.
1. Before beginning, you should create a COSC10403 folder to hold all of your work related to
this class. Note: this folder should reside on your W: drive (but outside of your wwwpub
folder)!
2. Start Eclipse by selecting Eclipse from the Start menu or by double-clicking the shortcut
found inside the Computer Science Applications desktop folder on TTC lab computers. The
first thing that you have to do is to select the workspace area where you will store your Eclipse
Java Programs. This workspace will most commonly be the COSC10403 folder that you
created in Step #1 above - so you may simply select OK after Browsing to get to that location.
3. If the Eclipse Welcome Window appears, close it by clicking on the “X” to the far right of its
title bar. (Note that the welcome page in Eclipse Neon may be a bit different)
After this, you should now be observing only the File, Edit, Source … bar and the various
Package, Tasks and empty windows (as seen in the figure below).
Click Here!!
yyy
vvvdddyou
You m
5
5
4. If this is your the first time to launch Eclipse, your Package Explorer window should be
empty indicating that you have not yet created any projects; if you have used Eclipse before
other Java projects may be listed (for example Lab0Project, Lab1Project, Lab2Project, and so
on). To continue working on a project that was begun earlier, simply click on the desired
project in the Projects window and continue with the development.
Note: To switch between Projects, Files, or Services windows, simply click on the desired
window’s title bar.
5. To create and run a new Java program you must first create a project. Follow the steps below
a. Choose New/Java Project… from the File menu. The screen below should appear.
The following window appears - to create a new project, let us name it as Lab0Project
and select Next. (Note: you should always name your projects as: LabiProject, where i
will correspond to the current lab number).
Make this
selection.
6
6
A new window appears, simply select Finish
The Project Explorer window will show a new folder named Lab0Project
6. Create an initial class for Lab0Project
Name your project:
Lab0Project
Lab0Project
7
7
Choose New Class… from the File menu. The screen below should appear.
Name your class as Lab0, as follows:
After you select Finish the right window of the IDE will display the source code for this new
class (as shown below):
Name your project:
Lab0 (Note that it will be
stored as Lab0.java)
Make this
selection.
The source code of the
selected java class is
displayed in this window.
8
8
7. Editing a java file.
For this initial practice, replace (overwrite) the java source code in the right-side window with
the following new java code EXACTLY as it is given here. (Otherwise you will have Java
errors and the program will not run):
import java.awt.*;
import javax.swing.JApplet;
public class Lab0 extends JApplet
{
public void init() {
repaint();
System.out.println("hello World");
}
public void paint(Graphics g) {
g.drawString("Hello World!", 30, 30);
}
}
Your source code window should now look like the following:
We will be discussing the significance of each of these Java statements in class shortly but
here is a brief explanation. The two import statements are needed to allow your program to
access two predefined packages of Java code. The public class is your Lab0 program and it
consists of two methods init and paint. Each method is a set of instructions separated by
semicolon (;) and enclosed in braces { }. The complete class is also enclosed by a pair of { }.
8. Running the program
To run your program you must insure that the class Lab0.java file is highlighted in the right-
side Package Explorer window. From the top menu bar, choose the Run/Run As/Java
Applet option as follows:
Selected java reserved
words will automatically
be color-coded.
9
9
Before running the program, the IDE may need to first compile your program. If this is the
case simply select OK.
When running your program, it should open a window in the upper left corner (as shown
below):
Note that you can also run your program using the green and black arrows in the tool bar (as
shown below)
The Run command is part of the options in the main menu
This window displays
the applet class named
Lab0 your program
The Run command is
also available from the
green and bottom
arrow icons
Run As/Java Applet
10
10
1. About source and binary Java Files
After your program has successfully executed – an inspection will reveal that you now have
two folders stored in your original Lab0Project folder. An src folder (containing the java
code that you entered earlier) and a bin folder (containing a Lab0.class which is the actual
code that will ultimately be executed by the computer).
To verify this, open the COSC10403 folder. Opening a window in XP (i.e. outside Eclipse)
you should have two subfolders in the Lab0Project, a bin and a src. Opening the bin and src
folders will show the screen shots below (note this is done outside of Eclipse, using the
Windows explorer)
In the bin folder you have the object code of your program Lab0.class (this is actually the code that
the machine executes). In the src folder you will find the source code of your program Lab0.java
(what you typed earlier).
a. Saving and copying your files.
Your files are automatically saved before any run command, but you can also select to
save the file using the File …Save option once you have selected the given file.
As seen in the previous windows, if you are using computers located in one of the TCU
labs (e.g., TTC 353/357, Library, etc), your files are most likely being saved on your W:
drive in the COSC10403 folder that you created at the beginning of the IDE session.
In your COSC10403 folder
you will find a subfolder for
Lab0Project that contains
two subfolders: src and bin The bin folder contains the
Lab0.class file, while the src folder
contains the Lab0.java source
11
11
If you are using your own computer, at home on in your dorm room, your files most likely
reside on your local C: drive. You will have to take steps to transfer both the .class and
.java program files to your TCU W: drive in order for your work to be graded. (We cover
this topic in section IV of this guide).
2. Creating a New Project
Back again in the Eclipse IDE, you can now use this initial template (saved now as
Lab0.java) to create other projects.
Create the new project by selecting the File/New/Java Project option and name the project
Lab1Project (as shown in the window below):
In order to create a new class, this time let us use, as a starting point, the code we created for
Lab0.java.
Select the Lab0.java code and using the Save As option from the File menu, save the class
in the src folder of your new Lab1Project.
Lab1Project
12
12
See the following screen. Note: that Lab1.java has been entered into the File name field.
After selecting the OK, you now have a copy of your Lab0.java file in the src subfolder of
your new Lab1Project. You can use Eclipse to open it to edit this new java (as shown in the
window below):
Use the Save As to make a copy
in the new project Lab1Project. In
this way you now have a template
for future projects.
In this case the new name is
Lab1.java
13
13
Obviously the IDE expects you to correct the errors in the new program before it will run; for
example: the class should be named Lab1 instead of Lab0 since it is located in the Lab1.java
file. Make that correction and run the Applet again, as you did with Lab0.
For the sake of practice, modify your Lab1 class by EXACTLY replacing its current source
code with the following code:
import java.awt.*;
import javax.swing.*;
public class Lab1 extends JApplet
{ public JButton btnStop = new JButton("STOP");
public void init() {
add(btnStop);
}
}
Run this applet from Run … Run As Java Applet option using Lab1.java this time and you
should get the following results:
3. Testing your Programs
When you work on a new programming assignment you will enter Java source code for the
new project (such as the Lab0.java file as done above) and test and retest it until everything
looks perfect. Each time that you Run the program – Eclipse generates two files: an updated
You have to correct the errors you have.
The IDE marks them with red flags. For this
case the new class should be named Lab1
rather than Lab0.
The new code that displays a
button in the window applet.
Results attained after
running the applet.
14
14
Lab1.java file and a new Lab1.class file containing the new executable code.
A default internal browser (the Applet Viewer) is then used to display the applet.
Once you are satisfied with your program’s results, and it is perfect – you must submit the
assignment for grading. TWO things must be done.
i. YOU MUST place the program on the web for viewing. This involves copying
specified files into the appropriate lab folder of the WWWPUB directory on your
W: drive. (See Section IV below) and
ii. YOU MUST submit your source code with TURNIN. (See Section V)
4. Other options in Eclipse
a. Occasionally, in order for your applet to be properly displayed on the web, you will have
to make changes to the width and height parameters of the program. You can change these
values by selecting the Run … Run Configurations option from the top menu. From
there select the Parameters Tab to change the width and height of the applet to be
displayed. The default dimension is 200 pixels x 200 pixels.
For example in this case we have changed the width of the window to width = 400, height =
200. Running the applet again yields the following results (notice the different size window):
b. As you can see the options available in the Eclipse IDE are multiple and one can get lost
rather easily. You will need to use Eclipse for a while in order to become comfortable with
it. For example you can add different views in your left most window by selecting on the
menu bar Window… Show View option as follows:
Using the Run … Run Configurations command,
By selecting the parameters tab you can change the Width
and Height of the applet window
Observe the larger dimensions.
Also,
observe that files will be
automatically compiled and
saved as needed, i.e., you do not
have to explicitly save, then
compile, then execute.
15
15
By using the Navigator tab you can now see both the src and bin folders for both
projects that we’ve created (as shown below).
c. Closing Eclipse
To close Eclipse you only need to get out of the application by selecting the exit option (in
the right most corner). However, before you do that you may want to close the Applet
you have running, you can do that by selecting the red square in the bottom window (as
shown below)
The navigator option will show you both the src and bin
contents of your projects
16
16
You should now have a working applet that runs inside Eclipse. Close all windows in
preparation for the 4-step submission process outlined below.
You can stop the execution of your applet by selecting the red
box in the console tab
Exit….
17
17
IV. Place your lab on the web (in wwwpub) and access it from a web browser.
In order to complete an assignment, and to prepare your applet to run on the web, five steps must be
taken. These are:
1. Copying the contents of the assignment’s bin folder (i.e., the .class file(s)) into the
corresponding folder in your WWWPUB folder,
2. Prepare an html file that can be used to execute the .class file.
3. Enable the URL to allow the browser to run your Applet.
4. Use Firefox to validate that you applet is running properly over the web.
5. Use Turnin to submit your assignment’s .java file to your instructor for grading. Note:
this step should be done last – only after you have verified that your applet is executing
properly over the web!!
1. Step 1 – copying contents of the object code
This step involves copying ONLY the Lab0.class from bin folder from your
COSC10403/Lab0Project folder into the corresponding Lab0 folder in your WWWPUB
directory on your W: drive. The procedure below should work on-campus anywhere you
have access to your W: drive. It will also be possible to FTP from off-campus using a
browser (https://stuftp.tcu.edu) or an FTP program such as Core FTP Lite.
Open My Computer (by double-clicking on the icon). Open your W: drive. Open your
COSC10403 folder and, finally, open the Lab0Project folder to view its contents. (Seen in
the window on the left below.)
Open the WWWPUB directory on your W: drive. Open the Lab0 folder inside your
WWWPUB. (Seen in the window on the right below.). It should be empty at this point.
Note: The icons might appear differently depending on the operating system and
viewing options that are set.
Copy (DO NOT Move or Drag & Drop) the Lab0.class found in the Lab0Project
folder into your wwwpub\Lab0 directory. Copy by right-clicking on the Lab0.class,
18
18
selecting Copy, and then right-clicking inside the Lab0 folder and selecting Paste
2. Step 2 – prepare the html file
Write a simple html file to run your applet. In this case you will have to write the
following html file and save it in you wwwpub\Lab0 folder. To do this you should use
Eclipse with the option file new Untitled Textfile File and then save it with the name
Lab0.html in your Lab0Project Folder
Lab0Project
Using Eclipse this how you create an text file:
Copy/Save this file into your WWWPUB\Lab0 folder and you will be able to run your
applet from the web by pointing a browser to the .html file.
You should use the eclipse text
editor to write the Lab0.html file.
Note that you may need to
change the width=xxx and
height=xxx parameters.
Note that the extension may not
show, but you have copied the
Lab0.class file in your LAB0 folder
19
19
3. Step 3 – Enable the URL to allow the browser to run your Applet
In this part you will instruct java to allow applets to run in your browser. For security
reasons you need to register the URL sites that contain secure Applets. To do this follow the
instructions given below.
For Windows 8: Right click the bottom left Windows icon and select Control Panel and
search for the java panel
The Lab0.html file that was created.
Note: Firefox is the default browser.
The applet can be tested by double-
clicking on this file or by pointing a
browser to the file.
Note that both files the Lab0.class
and the Lab0.html are in the LAB0
folder
20
20
For Windows 7: Look for the Control Panel in Windows. Using the MS bottom flag or start
and search for control panels as shown below:
and type the word java :
For Windows 7 or 8, click on the java icon and you will get the following control window,
Search for control panel.
21
21
select the Security tab
and there you have to register the following URL, this you do by editing the site list with the
following URL’s exactly as written here:
file:///
http://student.tcu.edu/
http://csfaculty.tcu.edu/10403/
Once this is done select Apply and OK and you are ready to test you applet
4. Step 3 – test you applet to insure that it is running properly
Start-up one of the Java-enabled web browsers. Graders will use Internet Explorer 6.0 (or
later) to grade the execution of your applet on the web - so ALWAYS make sure you view
your applets in Firefox, in Windows machines in the lab we are using the 32 bit version. On
your laptop machine make sure that the Firefox is the 32 bit version rather than the 64 bit,
this because the newer versions of Firefox do not allow for the java plugin. Do not use
Google Chrome. You may also use Microsoft InternetExplorer as long as it runs the java
plugin
Edit the Site List
22
22
If your account is set up correctly, you should be able to run your Lab0 applet by typing
http://student.tcu.edu/comer1/10403labs.html (where you have to replace comer1 with
whatever your assigned username is) for the URL and then clicking on the lab number you
want to view, Lab #0 in this case. When running the internet explorer browser you might
need to allow for ActiveX control, allowing the block content as follows:
Note: If you did not have a WWWPUB on your W: drive and have just created it for the
first time, you will probably get the error “The page cannot be found” (shown below) when
you try to access your 10403labs.html. This may be because the system has not yet run the
update program to register your WWWPUB. (At last check with Technology Resources this
update operation was being performed every hour, on the hour.)
Try to view it again in an hour or so! If you still cannot access your Lab0 see your instructor
immediately! You should not have this problem in future labs because the WWWPUB will
already be created and updated
Your applet
executing over the
web.
23
23
24
24
Once your WWWPUB directory is registered you should not ever see this
problem again. If you do get “The page cannot be found” message, it will
probably be because you did not correctly place a Labx.html file in the Labx
folder (x is the lab number).
IF THAT HAPPENS, GET IT FIXED IMMEDIATELY OR THE
GRADER WILL ASSIGN A GRADE OF 0 !! (The grader has nothing to
grade!!)
Note: If you get a dialog box asking for username and password, simply enter it
and view your web page. (This indicates that World read permissions have not been
given to your files yet. That should happen when Technical Resources runs a
program to update the file access privileges – currently every hour on the hour.)
However, if this continues to happen, tell your instructor AND go talk to the TCU
Help Desk personnel immediately!
Always make sure you view your applet on the Web using Internet
Explorer! Viewing the applet on the web is a major part of your lab
grade!!!
*** When you finish this operation, your W:/wwwpub/Lab0 directory should
have the contents shown above. If it does not, ask for assistance. ***
25
25
V. Submit your Java source code (.java file) for grading – TURNIN.
1. Run the TURNIN program to submit the lab for grading. TURNIN is accessible from the Start
menu or possibly from a desktop shortcut.
a. Enter your email address (e.g., j.doe@tcu.edu) in the box provided, select your course &
section number, and select the assignment to be turned in (in this case, Lab0) as illustrated
below. Click the Submit button.
b. If the information you entered was correct, AND your instructor has set up your Turnin
account, you should be presented with a window that will allow you to navigate and find your
.java file.
You will receive a caution that only your .java file is to be turned in. DO NOT SUBMIT
YOUR .html FILE OR YOUR .class FILE. You will get a grade of 0 (zero) if you do!!!
Note: For some future labs you will be submitting a .zip file that will encapsulate several .java
files together as a single file – we will discuss this later.
b. Navigate to the .java file you created for Lab0 (Lab0.java in the project’s src directory) and
click Open (or simply double-click the .java file). Be sure you only turn in the .java file with
Turnin.
26
26
c. If the submission went through correctly, you should be given a receipt number on the
submission screen. Be sure to keep the receipt number as this is your proof that you did
submit the lab, should any questions arise.
***************Important****************
You must run the TURNIN program to submit your lab for grading. Turnin only
the .Java file. Always keep your receipt as proof of submission.
Also, always verify the execution of your lab from the web using Internet Explorer
and make absolutely sure it is submitted as the correct lab number! (This means
going to http://stuwww.tcu.edu/username/10403labs.html and viewing the lab.)
Later in the semester, if you experience any difficulties with the submission
process, or viewing on the Web notify your instructor immediately!
***************************************
1. Your Lab 1 assignment has been posted on the 10403 Assignments web page. Go to
http://www.cs.tcu.edu/10403 and get your Lab 1 assignment now. You may begin work on it
today, while an instructor is available for assistance.
2. Be sure to ALWAYS logoff of your account on the PC by clicking the Start menu on the Task
bar, selecting ShutDown and Log off. If you do not do this, your account (and your labs) will
be open to the public!
27
27
VI. Running a JApplet as a Java Application (VERY IMPORTANT)
Once you have your Applet running it is also possible to run it as stand alone local Java application I
your computer. With this you gain the versatility of the Applet in your own machine, so if for any
reason you can not see the Applet running in the browser, ypou will be able to run it locally by a
simple double click application. Using Eclipse and Java, you will be able to do so as it is described
below. This is a very important part since Java Applets eventually will stop running over the
web, so in order to show your work you will have to run your programs as Applications
outside of Eclipse.
1. Add a main class: First you have to add a main method to your Applet class. Copy the
following code and add to your Applet class:
public static void main(String args[]) {
JFrame appletFrame = new JFrame("Applet as an application");
appletFrame.setLayout(new GridLayout(1,0));
appletFrame.setSize(200, 200);
// after the new put the name of your Applet Class
JApplet myApplet = new Lab1();
myApplet.init();
myApplet.start();
appletFrame.add(myApplet);
appletFrame.setVisible(true);
}
2. Run the applet as an application: If you read through the code of the main method you will see
that a frame is created, this frame is a pop up window that will serve as the browser call, to run
the applet now you will see in the Eclipse menu two options: to run as an Applet or as an
Application:
if you select Java Application a pop up window will appear (we call it a Frame) looking as
follows for the case we have been working on
28
28
Notice it runs under the title Running as application, rather than as it did before under the
browser as the host
3. Creating a clickable Jar file: Now we want to create this program as single clickable application,
Eclipse will help doing so, by selecting the option export if you click on the directory where you have
the applet class, as follows:
when you select the export function you will get a window asking the type of java jar you want, here
you select runnable jar as follows:
29
29
by selecting the next button a new will appear asking to select the launch configuration where you have
to select the directory and the java class you are working on, for this example in this case Lab1. Also
you will need to determine the export destination and the name of the jar file, for this example we have
place it on the Desktop with the name Lab1.jar, as shown below
After selecting Finish we will have the file Lab1.jar on the desktop. If just double click on it our
application will run on the frame as shown before.
The importance of this approach is that you can run this jar file in any machine that has java and it will
run perfectly.
3. Handling images in Applications: There is a very important consideration to make here when
dealing with images. While applets can read images rather simply from the bin folder, as you
will learn in class using the img = getImage( getCodeBase( ), "imgFile.jpg" ); command. The
case of Applications is different in this case you will have to change the previous line of code by
the following one:
img = Toolkit.getDefaultToolkit().getImage("imgFile.jpg");
30
30
Notice also that the location of the imgFile.jpg will have to be placed outside the bin folder
and not inside otherwise the image will not be found
4. Handling Sounds in Applications: Later in the course you will also learn to add sound to your
programs using the s = getAudioClip(getCodeBase(),"soundFile.wav"); Applet command. Again for the
case of Application programs this will have to be changed to a more obscure code as follows:
try{ File myFile = new File("soundFile.wav");
s = JApplet.newAudioClip(myFile.toURI().toURL());
//Or s = JApplet.newAudioClip(myFile.toURL());
}
catch (Exception e) {}
Again note also that the location of the soundFile.wav will have to be placed outside the bin folder and
not inside otherwise the image will not be found. BUT for the time being do not worry about this last
piece of code yet.
However, it is important to note that you may be required to turn both the jar file and the source
java later in the course using Turnin. This is done by compressing both file in a single .zip file.
31
31
VII. Working remotely on your own computers.
1. Get and install java from the site https://java.com/en/download/. Then install Eclipse version
MARS. Eclipse provides the development environment for writing and testing Java code. For
Windows users Eclipse use the new installer guide https://www.eclipse.org/downloads/installer-
instructions.php. For Mac user you can install directly fro the eclipse site
https://eclipse.org/downloads/
2. Get and install TURNIN (also see the Course Policies document). If you are connected to the
Internet (and have Java installed on your machine) you should be able to run the TURNIN program
and submit .java files from remote locations.
3. You should be able to access your W:\wwwpub remotely (to place applets on the web for viewing)
in one of two ways.
a. From a browser access your W: drive by going to https://stuftp.tcu.edu and login with your TCU
username and password. You should get a screen similar to the left window shown below.
Double-click on a directory to navigate though the directory structure (eg., into
WWWPUB\LABx). Use the Browse button to find the files (probably on your local C: drive)
that you wish to upload (classes directory and LABx.html file).
b. Or by using an FTP client that supports SSH, such as Core FTP Lite for Windows machines or
FETCH for Macs. (Go to www.coreftp.com for a free download of CoreFTP Lite.).
Run CoreFTP. From the Sites menu select Site Manager and setup as shown in the window
below. Provide your TCU username and password.
32
32
When you press Connect and a connection is established you should see a screen similar to the
window below. Navigate to find the files or directories you want to move (Source) in the left
pane and the location where you want to place them (Destination) in the right pane (shown
below). Then simply press the appropriate blue arrow to start the transfer.
4. ******* ALWAYS verify the transfer. **********
From Internet Explorer access http://stuwww.tcu.edu/username/10403labs.html (provide your
username) and click on the lab you just transferred. You should see your applet run.
33
33
VIII. Using Eclipse
So far we have only used a small portion of the facilities of the Eclipse IDE. Gain more familiarity with
the IDE by using the various facilities available. Here we explore some of them. Remember always to
BACK UP YOUR SOURCE FILES outside of Eclipse to make sure you always have a way to get
back if for some unexpected reason your lose some files while testing the IDE.
1. Refreshing and cleaning a project.
Although most of the time you will be working within the IDE it may be possible that you
modify a file of your workspace using a different editor. If this is the case when you go back to
Eclipse be sure to select the project folder and using the right mouse button select the option
Refresh, in this way the IDE will read the latest version in the workspace. See the following
example:
When a project has been modify externally in its source or object code it is a good practice to
clean the object code and rebuild it. This is done by selecting the option Project… Clean of the
menu bar as shown below
By using the Right Mouse Button
you have access to various
functions
Among them the Refresh updates
the selected folder for any external
modifications
Using the Right Mouse Button
you have access to various
functions
You can run your program,
selecting the Run option
34
34
2. Exporting a project using a Jar.
In Java it is customary to wrap all the object class into one single compressed file, similar to
a zip file. In Java these files are called JAR files (Java Archived Resources). Any project
can be turned into a Jar using the export facilities available in Eclipse. Use the Right Mouse
Button to select the option Export and follow the sequence of windows, as presented below:
From the Main Menu select
The Clean option of Project
Note that the Build
Automatically
Is checked so that the
program is recompiled after
the clean or any later
modification is performed
35
35
Continue selecting Next until you get to the Finish and Eclipse will generate a JAR file that
will be located in the project’s workspace. The JAR file contains all the necessary file to run
your program, for the case of Applets, you will change the html call to specify the location
on the desired applet, For example in this case the new html code will have the following
applet clause:
Note that in this case the file is located in the archive named Lab0Project.jar
Select the export Java folder
And then the Jar file
Name the jar file,
in this case as an
example
Lab0Project.jar
36
36
3. Documenting your code using JavaDoc
In any programming language it is important to document your work. For the case of Java we
use the double slash ( e.g. // ) for inline comments and the /** */ for paragraph
documentation. Assume we have a program that determines the Julian Number for a given date,
the method to compute the number requires as parameters the day, the month and the year and
returns a unique sequential number that identifies that date. A possible documentation for the
method would look as follows:
/**
* This method computes the Julian Number
* @param day (e.g. 1 to 31 )
* @param month ( e.g. 1 to 12 )
* @param year ( e.g. an positive integer )
* @return Julian Number
*/
public int calcJDN( int day, int month, int year) {
// here the code for the Julian Number method
// to do instructions …….
}
But Java goes further in the documentation because it allows us to generate an standardized
webpage documentation named the (Application User Interface) that we can use to learn how to
use the specific Java class.
For the sake of practice using Eclipse using the Right Button select the export option of a given
project as we did before; however this time select the option export as Javadoc in the
corresponding window:
Follow the wizard windows by selecting Next, you will need to provide a name for the folder
where you want the documentation to be placed or simple accept the default doc folder.
Continue until you get to the Finish option.
Exporting using the Javadoc option of the Java folder
37
37
Once you have finished you will find that you have a new folder with the documentation of the
program. This folder contains the API documentation of your class and you can select the
index.html file to start the navigation using any browser as follows:
In order to get meaningful and useful information in the API it is your responsibility to
document your source code using the /** */ standards described before. As your get a better
understanding of the Java language you will notice that only the public methods are documented
in the API. Look for the Fahrenheit Applet in the webpage of the course for a complete
example of proper documentation of Java Programs.
Select the index.html file of the doc folder to
review the documentation of the Lab0 class
38
38
4. Converting an Applet into a stand alone Application.
Another way to run an JApplet is to fully convert it into an Application, this is similar to what we
did in section VI, but a bit more complete since the JApplet is transformed into an application So far
we have been working with applets that run using a web browser to start the application. Here we
provide a short introduction to this process.
Java also allows to generate stand alone programs that run as an application triggered from a local
machine. Furthermore using the concept of a Jar we can create clickable applications that run by a
simple click. This might not sound of little relevance, however bear in mind that we can generate
the program in any machine (e.g. a Mac) and run the program flawlessly in a different machine (e.g.
a WinXP) with no reprogramming or recompilation. This is an important benefit of Java.
4.1 To show how we do this in Eclipse let us convert the Lab1Project by select “New Project”
from the File menu. Select General/Java Application and press “Next”. Let us call the project
Lab1App.
4.2 Create a new Class for this project and named it Lab1main. The Eclipse IDE will show
you a set of windows to do so.
39
39
a. Let us now edit this class with the following code that resembles the code we used for the
Lab1Applet, with some important changes we discuss below.
import java.awt.*;
public class Lab1main extends Frame
{ public Button btnStop = new Button("STOP");
public Lab1main() {
add(btnStop);
this.pack();
this.setVisible(true);
}
public static void main(String[ ] args)
{ new Lab1main();
} // trigger class
}
40
40
This code changes the Applet into a Java Application. And so rather than using an Applet we
will use a Frame to pop up a window. Also rather than the method init() we have a new method
named as the class itself, Lab1main. Note that we have added two lines of code:
this.pack();
this.setVisible(true);
they are needed in order make a visible pop up window in the case of Java Applications,
Applets are displayed directly by the web browser and that is why we did not added these lines
in the previous case.
Finally we need to create a method that will trigger the Application, this is the second method in
the code and must be named main(). We will discuss in class all the details of it when we
discuss applications.
In any case, provided that you have copied verbatim the code the IDE should look as follows:
and you will be able to run this method, by selecting the option as shown above. The outcome of
this is shown below:
Extra lines of code needed
Main method required to trigger the application
Run the program as a Java Application
41
41
Note that the performance of this applet is similar to the performance when we run the
Lab1Applet. However we did not require the web browser to trigger the program.
We can now convert this program into a Jar file in order to run it outside the IDE. For this you
should follow the steps presented in section VII.2 of this guide.
As we gain more familiarity with Java we will introduce other important features required in a
Frame to be fully compatible with an Applet. Such as adding more classes and controlling the
event of the window using a WindowListener, adding extra imports and many more.
5. Running the Debugger using JApplets
By now you have realized that tracing the execution of a program is a rather important part of
your work as a programmer. This task is generally known as debugging a program, i.e. getting
rid of the logical bugs you might have made. A simple solution to it is the use of messages sent
to the console by means of the System.out.println() method. However a more complete help is
to use the tools provided by the Eclipse Debugger. In this section we provide a brief
introduction to let you get acquainted with the tool available so that you might learn to use it
when you have a more complex programming task.
In this guide we will use the following code, but we invite you to use any code of your previous
labs to one feel more familiar with.
Pop up frame displaying the Button
42
42
To start the debugging we must select the instructions we want to trace, this is done by adding
breakpoints in the blue left side of source window where we want the program to pause. A
breakpoint is a location in your source code that will halt execution so that you can view the
information about a program's state. To do this right-click along the left side of the Java editor
on the line of code you wish to break on, (Also, double-clicking on the left side of the editor will
create a breakpoint.). See in the following figure how to toggle a breakpoint.
After you do it you will see the breakpoint (a blue dot) and if you pass the mouse over it you
43
43
get the following message. To turn it off double click it again.
We are now ready to run the debugger. So far you have use the command Run As .. Java
Applet; but now we will use the command below in the options Debug As .. Java Applet, as
shown in the figure
When
you
run
the
debugger
your
window
perspective
will
change
from
that
of
java
running
to
debugging,
this
in
order
to
provide
you
with
other
useful
windows
in
the
process.
You
may
get
a
message
from
Eclipse
telling
you
about
it,
as
shown
below
In
any
case
the
new
look
of
the
perspective
is
similar
to
the
one
shown
here:
44
44
Let
us
pay
attention
to
the
debugging
buttons
located
at
the
right
side
of
the
menu
of
the
debug
window:
This
window
shows
the
live
stack
trace
of
methods.
When
working
with
JApplet
we
are
at
the
highest
stack
level
object
so
in
order
to
get
down
to
a
breakpoint
select
couple
times
the
Resume
button
until
the
code
of
your
program
is
displayed
in
the
source
window
showing
the
temporary
halt
at
the
selected
breakpoint.
Looking
at
the
Variables
Window
you
will
see
the
values
currently
assigned
to
them.
In
our
example
we
see
that
num1
=
9
and
myS
=
“Hello”
as
shown
below:
45
45
If
you
keep
selecting
the
resume
icon
you
will
move
to
the
next
breakpoint.
Note
that
if
the
breakpoint
is
located
in
another
class
i.e.
another
program,
the
debugger
will
jump
into
it
and
deliver
the
values
of
the
objects
associated.
In
the
case
of
the
example
we
present
here
a
for
loop
and
the
values
associated
with
it
as
it
the
debugger
program
runs
through
it
The
debugger
also
has
two
stepping
buttons:
Step
Over
and
Step
Into
.
Step
over
takes
you
to
the
next
instruction
within
the
code
and
Step
Into
jumps
into
the
code
when
a
method
is
called.
Again
you
can
see
assigned
value
for
the
variables
changing
accordingly.
As
you
can
imagine,
knowing
the
values
of
your
variables
eliminates
the
need
for
using
System.out.println()
statements
throughout
your
code.
Finally
the
Stop
button
stops
the
execution
of
the
debugging.
You
can
debug
again
or
go
back
to
the
regular
java
execution
by
using
the
Run
As
command.
Note
that
you
can
always
go
back
to
the
Java
perspective
by
selecting
from
the
Window
Menu
in
eclipse
the
desired
perspective.
46
46
A
useful
aspect
during
debugging
is
the
fact
that
you
can
change
the
internal
assigned
values
of
your
program,
for
example
assume
we
change
the
value
of
variable
num2
in
the
variables
window.
Initially
assigned
to
4.0
and
we
change
it
to
6.0
if
we
step
over
the
code
using
we
will
get
to
the
line
if (num2>5) System.out.println( " Value changed for num2 " + num2 );
this line should not have not been executed, but since we have changed its value
we will get in the console the message: value changed for num2 6.0. There are many
other features available for debugging such as establishing watchpoints. We have provided
only an introduction. We recommend you look at the following informative links:
http://agile.csc.ncsu.edu/SEMaterials/tutorials/eclipse-
debugger/http://www.vogella.com/articles/EclipseDebugging/article.html