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

  1. AVD = Android Virtual Device, e.g. an Android phone emulator. Its needed to test your running application.
  2. Window | Android SDK and AVD manager
  3. Select Virtual Devices (probably already selected)
  4. 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:

  1. Select Virtual Devices (probably already selected)
  2. New
  3. Create Andoid Virtual Device dialog
  4. 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.
  5. For Target select the OS e.g. Android 2.1
  6. Give the AVD a virtual SD Card and it's size e.g. for 10MiB enter the number 10
  7. Click Create AVD
  8. Your new AVD should now appear in the list.
  9. Test it by selecting it and clicking Start... Click Launch when asked. NB: It takes a verl long time to launch!!
  10. 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:

  1. File | New | Android Project (you might have to look in Other... option at the bottom of the list)
  2. Next

In New Project Dialog

  1. Name your project e.g. citApp - this will be the name of the project folder if you look for it in the workspace folder
  2. 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.
  3. Levave the Applicaton name as is e.g. citApp
  4. 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)
  5. Check create Activity and give it a name. This will the name of your main class.
  6. Optional enter an Minimum SDK version - typically the same as the level chosen in step 2 above
  7. Finish

To Run your App

  1. Select the project folder e.g. in our case it's citApp
  2. Right-click and choose Run As...
  3. Select Android Application

How to get your first Applet running with BlueJ

Start BlueJ

  1. Close previous projects
  2. Project | New Project
  3. Click New Class button
  4. Name class. myApplet
  5. Double click on the myApplet rectangle that was just created
  6. 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);
    }
    }

  7. 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.
  8. Click Compile button to check for any errors. Close window.
  9. Right-Click on myApplet rectange and select Run Applet.
  10. Select Run Applet in Applet Viewer
  11. 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

  1. Close previous projects
  2. Project | New Project
  3. Click New Class button
  4. Name class e.g. myAplication
  5. Double click on the myApplication rectangle that was just created
  6. Code your application or delete everything and paste in an example from the noted.
  7. Make sure the class name and file name match (class must be called myApplication)
  8. Click Compile button to check for any errors. Close window.
  9. Right-Click on myApplicatin rectange and select void main ( )
  10. Click OK at the next dialog (no need to enter anything)
  11. 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

 

In the Name and Location dialog:

Now Create an Applet class

Name and Location

To run

Some code to try

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

 

In the Name and Location dialog:

Create an application class


To run
Some code to try
System.out.println ("hello");

Run again - "hello" will appear in the Output window



How to get your a GUI Application running using NetBeans 7

Name and Location


How to get your first MIDlet (moble phone application) running using NetBeans 5.5/6

Create a new Project

 

In the Name and Location dialog:

Select Default (Mobile) platform:

More Configurations

Create a source MIDlet java file:

 

Name and Location Dialog

 

You should see the HelloCIT default code, but it does nothing so...

 

Run the MIDlet in the phone emulator

Make some changes and run it again


How to get a Netbeans MIDlet up and running on a YOUR (real) phone


How to get your first MIDlet (moble phone application) running using Eclipse
  
  1.             J2ME | J2ME Midlet
  2.             Next
  3.             Name your class
  4.             Next
  5.             Finish
   

 

 

*** 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.


  1. Run JBuilder from Start menu.
  2. Close any existing projects that might be open (e.g. Right click Welcome.jpx for 1st time running JBuilder).
  3. Did You do step 2 above?? Why not?, So Close any existing projects. Select New... (note NOT New Project)
  4. In the Object Gallery select General and then select Application
  5. The Project Wizard opens automatically
  6. 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).
  7. The Application wizard automatically comes up in step 1.
  8. In the Application wizard step 1 Delete the package name. You can give the application class a name if you want.
  9. Click Next to go to step 2 - give the JFrame class a name and a title for the JFrame's title bar.
  10. Click Finish in step 2 (skipping remaining steps)
  11. You'll see the application default source code.
  12. 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


  1. Open a New project: File | New Project (or File| New,select Project from Object gallery and click Project and OK)
  2. Project Wizard step 1 - give the project a name and click Next
  3. Project Wizard Step 2: VERY IMPORTANT - change JDK to J2ME Wireles Tookit.
  4. Click Finish in Step 2, skipping remaining steps. You now have a blank JME project, no code files yet.
  5. Now Select File/New (NOT new project) and select Micro and then MIDP Midlet from the object Gallery, click OK.
  6. In Midlet Wizard Step l, name the class and delete package name. Click Finish in Step l, skipping remaining steps.
  7. Now run the project (click play button) and the phone emulator will run as follows:




 

 

 

 


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