Getting your first program running
 Applet, Application, MIDlet, Servlet, JSP
  
    cit.KarlGrabe.com 
    updated 27 Jan 2014 
  
Lab Excercises to do but first get started with your first program:
Links on this page:
          
Other Links:
  
 
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
 
cit.KarlGrabe.com