Getting your first program running
Applet, Application, MIDlet, Servlet, JSP
cit.KarlGrabe.com
updated 14 Sep 2011
Lab Excercises to do but first get started with your first program:
Links on this page:
Other Links:
How to get
your firsh Android App running
Check you have an AVD set up correctly in Eclipse
- AVD = Android Virtual Device, e.g. an Android phone emulator. Its needed to test your running application.
- Window | Android SDK and AVD manager
- Select Virtual Devices (probably already selected)
- you should see one or more devices listed with info on AVD name, Target Name (e.g. = Adroid 2.1...)
No AVD? So let's create one:
- Select Virtual Devices (probably already selected)
- New
- Create Andoid Virtual Device dialog
- Give the AVD a name, can be anything but I'd choose somethign like: AVD2.1Lev7 if you wan to create a device runnign Android OS 2.1.
- For Target select the OS e.g. Android 2.1
- Give the AVD a virtual SD Card and it's size e.g. for 10MiB enter the number 10
- Click Create AVD
- Your new AVD should now appear in the list.
- Test it by selecting it and clicking Start... Click Launch when asked. NB: It takes a verl long time to launch!!
- Try out some of the applications. No, you can't make free phone calls...
If you have an AVD you can now create a new App project in Eclipse:
- File | New | Android Project (you might have to look in Other... option at the bottom of the list)
- Next
In New Project Dialog
- Name your project e.g. citApp - this will be the name of the project folder if you look for it in the workspace folder
- Select a target Android emulator device e.g. Android 2.1-update1. Make a note of the level chosen e.g. for Android 2.1-update1 it's 7 - this is used later. If you want your app to run on ANY android device then select the earlies OS release: Android 1.5.
- Levave the Applicaton name as is e.g. citApp
- Enter a package name e.g. ie.cit.mobile (doesn't have to be a web address just names with dots inbetween. minimun 2 names)
- Check create Activity and give it a name. This will the name of your main class.
- Optional enter an Minimum SDK version - typically the same as the level chosen in step 2 above
- Finish
To Run your App
- Select the project folder e.g. in our case it's citApp
- Right-click and choose Run As...
- Select Android Application
How to get
your first Applet running with BlueJ
Start BlueJ
- Close previous projects
- Project | New Project
- Click New Class button
- Name class. myApplet
- Double click on the myApplet rectangle that was just created
- Delete all of the code and replace with the following:
import java.awt.*;
import java.applet.*;
public class myApplet extends Applet {
public void paint (Graphics g) {
g.drawString ("first CIT java applet", 10,10);
}
}
- Check that the class name "myApplet" matches the name you created
in step 4. If not change the class name in the code you pasted in step 6.
- Click Compile button to check for any errors. Close window.
- Right-Click on myApplet rectange and select Run Applet.
- Select Run Applet in Applet Viewer
- You should see an applet view window open and have the text "first
CIT java applet" displayed.
How to get
your first Application running with BlueJ
Start BlueJ
- Close previous projects
- Project | New Project
- Click New Class button
- Name class e.g. myAplication
- Double click on the myApplication rectangle that was just created
- Code your application or delete everything and paste in an example from
the noted.
- Make sure the class name and file name match (class must be called myApplication)
- Click Compile button to check for any errors. Close window.
- Right-Click on myApplicatin rectange and select void
main ( )
- Click OK at the next dialog (no need to enter anything)
- Your application should run, if you can't see it check it is not hidden
behind some other windows.
How to get
your first Applet running with Eclipse
File - New - Project...
Java project - Next
Enter Project Name
Finish
New - Class
Enter class name in text field labellled Name:
Do not check public static void main ( )
Finish
Add this to the top of the file (before the class definition
import java.awt.*;
import java.applet.*;
Now add a paint method in the class body and extend class from Applet:
public class myApplet2Class extends Applet {
public void paint (Graphics g){
g.drawString("hello there", 10, 10);
}
}
Click green Run and select Run as
Select Java Applet -
How to get
your first Application running with Eclipse
File - New - Project...
Java project - Next
Enter Project Name
Finish
Now add a class file and code a main method
New - Class
Enter class name in text field labellled Name:
Create main mehtod should be checked
Finish
In main method enter code:
System.out.println("hello there");
click green run button
Select Java Application, right click select new
Click Run
Save & Launch dialog - click OK
How to get
your first Applet running using NetBeans 7
Create a new Project
- Close all open projects
- File | New Project
- Choose Project:
- in Java folder select Java Applicatoin folder (even though it's an applet we want)
- Next
In the Name and Location dialog:
- Name your project (this will be the name of the project folder) e.g. HelloCITapplet
- Browse to the location where you would like your project file kept
- Uncheck main class <-- NB this not default behaviour so you must change it
- Check Save as main project
- Finish
Now Create an Applet class
- File | New File
- Java folder | Applet (w'ell use JApplet later)
- Next
Name and Location
- Name the class e.g. myApplet (this will create a java source file called myApplet.java)
- leave package blank (normally not recommended but easier for trying examples)
- Finish
To run
- in the projects pane find your source file e.g. myApplet.java
- Right clidk on it and select Run File
- It should compile/run and the Applet Viewer window opens and showing your program running (it doesn't do anything)
Some code to try
- Paste in the following code (erasing the existing code) and re-run your applet:
import java.awt.*;
import java.applet.*;
public class myApplet extends Applet {
public void paint (Graphics g) {
g.drawString ("first CIT java applet", 10,10);
}
}
you can now add more lines after g.drawStrin... above - look up the Graphics class in the API
How to get
your first Application running using NetBeans 7
Create a new Project
- Close all open projects
- File | New Project
- Choose Project:
- Java folder and select Java Application
- Next
In the Name and Location dialog:
- Name your project (this will be the name of the project folder) e.g. HelloCITapplication
- Browse to the location where you would like your project file kept
- decete the text opposite Create Main Class
- Check Save as main project
- Check main class
- Finish
Create an application class
- File | New File...
- Java folder | Java Main Class
- Next
- Name the class e.g. myApplication
- finish
To run
- Click on the green 'play' button
- if asked - choose your class e.g. myApplication
Some code to try
- Paste in the following code in the main method and re-run
System.out.println ("hello");
Run again - "hello" will appear in the Output window
How to get
your a GUI Application running using NetBeans 7
- File | New Project
- Java folder and Java Desktop Application
- Next
Name and Location
- Name the Project
- Select Basic Application
- uncheck Use Deditcated...
- Check Set as Main Project
- Finish
- Now Click Play
How to get
your first MIDlet (moble phone application) running using NetBeans 5.5/6
Create a new Project
- File | New Project
- Choose Project:
- Select the Mobile folder and then select MIDlet (Mobile Application)
- Next
In the Name and Location dialog:
- Name your project (this will be the name of the project folder) e.g. HelloCITproject
- Browse to the location where you would like your project file kept
- Check Save as main project
- Uncheck Create HelloMidlet
(otherwise you can't edit all of the code)
- Next
Select Default (Mobile) platform:
- Default ColorPhone
- CLDC
1.1 (if you want floating point support)
- MIDP 2.0 or higher
- Next
More Configurations
- Uncheck Everything
- Finish
Create a source MIDlet java file:
Name and Location Dialog
- Name your midlet e.g. HelloCIT
- Class can be the same e.g. HelloCIT
- Leave package blank
- Finish
You should see the HelloCIT default code, but it does nothing so...
- Paste in your java MIDlet code: e.g. copy this HelloCIT
example source code and paste over the default code
- Make sure there is no package statement at the top of your code
- In your source code change the name of the class and constructor if needed
to match the name of the java file. In this case the class is HelloCIT
(note capitals) and the constructor should be public HelloCIT ( ).
Run the MIDlet in the phone emulator
- Click the green leftmost Run button - you Midlet should compile, and open
the phone emulator.
- No, you can't make free phone calls - duuhh.
- Select the MIDlet listed on the phone emulator and launch it.
- You should see the text Hello, "Hello CIT" displayed. The 'Hello'
is the label associated with the StringItem,
the 'Hello CIT' is the contents of that StringItem object.
Make some changes and run it again
- Close the emulator first
- find the line that prints out "Hello CIT", change it and re-run
your program.
- it's this line:
- helloStringItem = new StringItem("Hello", ", Hello
CIT!");
How to get
a Netbeans MIDlet up and running on a YOUR (real) phone
- You need to locate the compiled .JAR file for example here is a typical
location:
- E:\myDocuments\NB Projects\HelloCITproject\dist
- Look for HelloCITproject.jar
- Transfer this file to your phone from your PC/Laptop via bloothooth, InfraRed
or a cable. Alternative you could upload it to a web server and then create
a WML file to link to it. Browse with your phone to the WML file and it possbile
to download and install it on your phone.
- Here's an exampl of a WML file with some MIDlets to try. Browse to it with
your phone's wap browser:
- http://cit.karlgrabe.com/mobile/index.wml
- (cit.karlgrabe.com/m will take you there from a phone or PC)
- Some phones may also require the HelloCITproject.jad file, it in the same
location as the JAR file. BTW - You can double click the JAD file and the
NetBeans phone emulator shoudl run it.
How to get
your first MIDlet (moble phone application) running using Eclipse
- File |
New | New | Project...
- J2me | J2ME MIDlet Suite
- Next
- Enter Project Name
- Next
- Next
- Finish
- J2ME | J2ME Midlet
- Next
- Name your class
- Next
- Finish
- Paste or
write your source code
- Right-Click
on source
- Run As | Emulated J2ME MIDlet
- Modify
Code
- re-run -> click on green button,
select from the drop down list
*** The remainder of this page is for Jbuilder 2006 only and is out of date
****
How to get
your first Applet running with JBuilder 2006.
1. Run JBuilder from Start menu.
3. Close any existing projects
that might be open (e.g. Right click Welcome.jpx for 1st time running JBuilder)
2. Here's the quickest way to build an applet in JBuilder
- Select New... (note NOT New Project)
- In the Object Gallery
select web tab and then select applet
-
The Project Wizard opens automatically at step 1/3
- In Project Wizard Step 1/3 - give the project a name (e.g. test) and
click Finish (skipping steps 2 and 3)
- The Applet wizard automatically
comes up and in step 1/3.
-
In the Applet wizard step 1/3 Delete the package name.
- Click Finish in step 1/3
(skipping steps 2 and 3)
- You'll see the applet default source
code.
- Now compile and run the applet with
menu Run|Run Project (or click green play button) and you'll see it
running:
Your applet doesn't actually do anything, but then you didn't write any
Java code!
To Get your first Application running with JBuilder.
- Run JBuilder from Start menu.
- Close any existing projects
that might be open (e.g. Right click Welcome.jpx for 1st time running JBuilder).
- Did You do step 2 above?? Why not?, So Close any existing projects. Select
New... (note NOT New Project)
- In the Object Gallery select General and then select Application
- The Project Wizard opens automatically
- In Project Wizard Step 1- give the project a name (e.g. test) and click
Finish (skipping remaining steps). You can choose an alternative
folder to save the project in if you want (e.g. your F drive).
- The Application wizard automatically comes up in step 1.
- In the Application wizard step 1 Delete the
package name. You can give the application class a name if
you want.
- Click Next to go to step 2 - give the JFrame class a name and a title for
the JFrame's title bar.
- Click Finish in step 2 (skipping remaining steps)
- You'll see the application default source code.
- Now compile and run the application with menu Run | Run Project
(or click green play button) and you'll see it running:

It does nothing, only shows a blank JFrame.
To Get your first MlDlet Running with JBuilder
- Open a New project: File | New Project
(or File| New,select Project from Object gallery and click
Project and OK)
- Project Wizard step 1 - give the project a name and click Next
- Project Wizard Step 2: VERY IMPORTANT - change JDK to J2ME
Wireles Tookit.
- Click Finish in Step 2, skipping remaining steps. You now
have a blank JME project, no code files yet.
- Now Select File/New (NOT
new project) and select Micro and then MIDP
Midlet from the object Gallery, click OK.
- In Midlet Wizard Step l, name the class and delete
package name. Click Finish in Step l, skipping
remaining steps.
-
Now run the project (click
play button)
and the phone emulator will run as follows:
- On the emulator - Click Launch to run Midlet (it does nothing,
we didn't write any code, it only has an Exit button).

- Click Exit on the emulator to exit the MIDlet program.
- Click on the emulator's close window icon to exit the emulator, then you
are back in JBuilder
To Get your first Servlet Running with JBuilder
(2006)
File|new project
in project wizard:
name project file, select directory, click next
next, next, finish
file|new
in Object gallery:
Web select Standard Servlet, click ok
In Select Server Supporting... select Tomcat from drop down list, click ok
in Servlet Wizard give class a name (e.g testServlet), delete package, click new web module:
in Web module wizard select "create empty web Module", click next
in web module wizard step 2 use defaults and click next
in web module wizard setp 3 use defaults and click finish
continue in Servler Wizard Step 1 and click next#
in servlet wizard step 2 select doGet( ) and doPost( ), click next
in servlet wizard step 3 click next
in servlet wizard step 4 click next
in servlet wizard step 5 click finish
to run servlet click green play button.
When you have your servlet debugged you can upload to a server.
For Apache/Tomcat place the compiled class file(s) (e.g. testServlet.class) into the following direcory on the server:
root/WEB-INF/classes
to invoke the servlet (e.g. compiled to testServlet.class), directly or from a form inside a html document use the following URL:
www.myWebSite.com/servlet/testServlet
where "myWebSite" is the domain name of your web site - you upload the testServlet.class file(s) normally using ftp.
To Get your first Java Server Page (JSP) Running with JBuilder
(2006)
File|new project
in project wizard:
name project file, select directory, click next
next, next, finish
file|new
in Object gallery:
Web select JSP, click ok
In Select Server Supporting... select Tomcat from drop down list, click ok
in JSP wizard step 1 select new web module
in Web module wizard select "create empty web Module", click next
in web module wizard step 2 use defaults and click next
in web module wizard setp 3 use defaults and click finish
continue in JSP Wizard Step 1 and rename jsp if needed, (e.g. exampleJSP) click next
in JSP wizard step 2, no need to check "generate submit form" click next
in JSP wizard step 3 click next
in JSP wizard step 4 click finish
to run JSP click green play button - it doesn't do anything but you can paste in JSP code already written.
e.g. paste in the following (use the name exampleJSP in step 1 of JSP Wizard above) here it is on cit.karlgrabe.com
<! krg jsp Vector + Enumeration Example feb 2007
<%@page language="java" import="java.util.*"%>
<HTML>
<HEAD>
<TITLE>Simple Vector Example</TITLE>
</HEAD>
<BODY>
<P>Vector Example</P>
<FORM METHOD="GET" ACTION="exampleJSP.jsp">
<INPUT SIZE="15" NAME="userinput">
<INPUT TYPE="SUBMIT" value="Add Text to Vector" name="VectorAdd">
</FORM>
<P>
<%!
// Declaration of class wide instance variables
Vector myVector;
%>
<%
String input;
if (myVector == null) {
myVector = new Vector(5, 10);
myVector.add("this is vector's 1st item");
}
input = (request.getParameter("userinput"));
myVector.add(input);
%>
<%= "You just typed: " + input%> <BR>
<%="Vector contents output follows"%> <BR>
<%
Enumeration vEnum = myVector.elements();
while (vEnum.hasMoreElements()) {
%>
<%= "Vector item: " + vEnum.nextElement() %> <BR>
<%} %>
</P>
</BODY>
</HTML>
To deploy your JSP just copy the file (exampleJSP.jsp) into any directory on your server (it must have a servlet container installed, e.g. Apache/Tomcat) e.g.
root/myProject/
then to invoke it as follows:
www.mySite.com/myProject/exampleJSP.jsp
cit.KarlGrabe.com