java with eclipse - setup and getting started

Upload: srimathi-amala-pushpika

Post on 03-Apr-2018

234 views

Category:

Documents


1 download

TRANSCRIPT

  • 7/28/2019 Java With Eclipse - Setup and Getting Started

    1/14

    2010 Marty Hall

    Java with Ecli se:Setup and Getting

    ar e

    Customized Java EE Training: http://courses.coreservlets.com/Servlets, JSP, JSF 2.0, Struts, Ajax, GWT 2.0, Spring, Hibernate, SOAP & RESTful Web Services, Java 6.

    Developed and taught by well-known author and developer. At public venues or onsite at yourlocation.

    2010 Marty Hall

    For live Java EE training, please see training courses

    at htt ://courses.coreservlets.com/.Servlets, JSP, Struts, JSF 1.x, JSF 2.0, Ajax (with jQuery, Dojo,Prototype, Ext-JS, Google Closure, etc.), GWT 2.0 (with GXT),

    Java 5, Java 6, SOAP-based and RESTful Web Services, Spring,Hibernate/JPA, and customized combinations of topics.

    Taught by the author ofCore Servlets and JSP, More

    Customized Java EE Training: http://courses.coreservlets.com/Servlets, JSP, JSF 2.0, Struts, Ajax, GWT 2.0, Spring, Hibernate, SOAP & RESTful Web Services, Java 6.

    Developed and taught by well-known author and developer. At public venues or onsite at yourlocation.

    , .venues, or customized versions can be held on-site at your

    organization. Contact [email protected] for details.

  • 7/28/2019 Java With Eclipse - Setup and Getting Started

    2/14

    Topics in This Section

    Installing Java

    Installing and configuring Eclipse

    Importing sample projects

    Executing simple desktop programs

    Executing applets

    2010 Marty Hall

    Installing Java

    Customized Java EE Training: http://courses.coreservlets.com/Servlets, JSP, JSF 2.0, Struts, Ajax, GWT 2.0, Spring, Hibernate, SOAP & RESTful Web Services, Java 6.

    Developed and taught by well-known author and developer. At public venues or onsite at yourlocation.

  • 7/28/2019 Java With Eclipse - Setup and Getting Started

    3/14

    Java Versions

    Java SE (Standard Edition) Core Java

    Java version used in this class ,

    all new projects

    Naming conventions are confusing Java 6 == Java 1.6 == JDK 1.6

    Java 5 == Java 1.5 == JDK 1.5

    Java 2, version 1.4 = Java 1.4 == JDK 1.4

    Java EE (Enterprise Edition)

    Same core language, but adds in many libraries for Webapps an ot er enterpr se tas s

    Many real-life deployments start with Java SE and thenget server that is bundled with Java EE libraries

    Which Java SE Version Should

    Server-Side ApplicationsUse the latest Java version that your app server supports

    JDK 1.4 1.6, depending how old your server is

    , . .

    Desktop Applications

    For best ower and s eed use Java 6 aka 1.6 If you install, use JDK 1.6

    Use 1.5 only if project is old (but consider upgrading)

    pp e sSeparate Java Plug-In is required in all recent browsers

    ,

    On internet, most Java applet sites (e.g., Yahoo! Games)use JDK 1.4 or 1.5, but allrequire end-user installation

  • 7/28/2019 Java With Eclipse - Setup and Getting Started

    4/14

    Installing Java

    Install Java 6http://java.sun.com/javase/downloads/index.jsp

    Use this version. The JDK Java

    Development Kit includes compiler for.java files, whereas the JRE Java

    Runtime Environment is only for

    executing prebuilt .class files.

    This tutorial uses Eclipse, but if you prefer the

    NetBeans environment, it is very easy to adapt

    the instructions to that development

    environment. So, if you prefer NetBeans or

    your organization has standardized on it, use

    s own oa ns ea o no n a on o e

    one on the left.

    Bookmark the Java API (JavaDocs) p: ava.sun.com avase ocs ap This is the most important Java reference for developers.

    Eclipse integrates this API, but a separate link is still good

    2010 Marty Hall

    Installing Eclipse

    Customized Java EE Training: http://courses.coreservlets.com/Servlets, JSP, JSF 2.0, Struts, Ajax, GWT 2.0, Spring, Hibernate, SOAP & RESTful Web Services, Java 6.

    Developed and taught by well-known author and developer. At public venues or onsite at yourlocation.

  • 7/28/2019 Java With Eclipse - Setup and Getting Started

    5/14

    Installing Eclipse

    Overview

    for Java. Support for Java, HTML,

    CSS, JavaScript, C++, PHP, andmore. . Choose Eclipse IDE for Java EE

    Developers Latest version as of 8/2010 is

    .

    Features Checks your syntax as you type Automaticall com iles ever

    time you save file Many tools: refactoring,

    debugging, server integration,emp a es or common as s, e c. Low learning curve: beginners can

    use Eclipse without knowing thesetools

    Eclipse Memory Bug with Java. . _

    ProblemWith JDK 1.6.0_21, Eclipse does not properly pass

    memory parameters to Java. . . _ ,

    to Eclipse 3.4, 3.5, and 3.6

    SolutionFind the Eclipse installation folder, edit eclipse.ini, andadd the following line to the bottom of the file

    -XX:MaxPermSize=256m

    More details and a downloadable patched eclipse.ini http://www.coreservlets.com/

    - -eclipse-jdk1.6.0_21-permgen-bug.html

  • 7/28/2019 Java With Eclipse - Setup and Getting Started

    6/14

    Running Eclipse

    Unzip the downloaded fileCall the folder you unzip into installDir

    Double click eclipse.exe rom nsta r n

    Click on Next time you bring

    up Eclipse, it willcome up in workbench automatically

    Shortcut any eve opers pu c pse n on e r es op

    R-click eclipse.exe, Copy, then go to desktop, R-click, andPaste Shortcut (not just Paste!)

    Configuring Eclipse

    For desktop apps, only one step neededTell Eclipse where you installed Java

    Process n ow re erences ava nsta e s

    Press Add, choose Standard VM, navigate to JDK

    folder (not bin subdirectory) E.g., C:\Program Files\Java\jdk1.6.0_20

    Check checkbox

    Java versionand OK

  • 7/28/2019 Java With Eclipse - Setup and Getting Started

    7/14

    Eclipse: Running Programs

    Executing program from existing projectOpen existing

    project

    Java file tobring it up in editor

    R-click anywherein code

    Java Application

    Output goes inonso e a o om Note: Class must

    have a main method

    Eclipse: Making Projects

    Creating new projectFileNew Project

    Java Java Project

    To simplify applets later,choose Sources/Classes

    in same folder

    Creating new classR-clickNew Class

    You can choose to haveEclipse make mainautomatically

    You can also copy/paste existingclass, then give it new name

  • 7/28/2019 Java With Eclipse - Setup and Getting Started

    8/14

    2010 Marty Hall

    Building Java Manually

    Customized Java EE Training: http://courses.coreservlets.com/Servlets, JSP, JSF 2.0, Struts, Ajax, GWT 2.0, Spring, Hibernate, SOAP & RESTful Web Services, Java 6.

    Developed and taught by well-known author and developer. At public venues or onsite at yourlocation.

    Creating and Running Program

    Create the FileWrite and save a file (say Test.java) that definespublic class Test

    . ., , ,Intellij IDEA), there are a number of text editors (e.g.,TextPad, UltraEdit, Brief) with good Java syntax support.

    Compile the programCom ile Test. ava

    > javac Test.java This step creates a file called Test.class

    > java Test

    This ste assumes our class has main method

  • 7/28/2019 Java With Eclipse - Setup and Getting Started

    9/14

    2010 Marty Hall

    Simple Examples

    Customized Java EE Training: http://courses.coreservlets.com/Servlets, JSP, JSF 2.0, Struts, Ajax, GWT 2.0, Spring, Hibernate, SOAP & RESTful Web Services, Java 6.

    Developed and taught by well-known author and developer. At public venues or onsite at yourlocation.

    Installing Sample Projects

    Code from all tutorial sections is availableonlinehttp://courses.coreservlets.com/

    Import project into Eclipse

    Click on a ro riate tutorial sectionDownload ZIP file

    The one for this section is called intro

    ar c pse an go o or enc

    File Import General Existing Projects intoWorks ace Select archive file not Select rootdirectory).

    Then browse to ZIP file you downloaded, OK, Finish

  • 7/28/2019 Java With Eclipse - Setup and Getting Started

    10/14

    Basic Hello World Application

    File HelloWorld.java:public class HelloWorld {

    public static void main(String[] args) {" "

    }

    }

    Notes App cat on s ngo or a stan -a one Java program

    An application is a Java class that contains main ,that do can be directlyexecuted

    Executing HelloWorld

    In Eclipse (recommended)Compiling

    Done automatically whenever you save a file

    Executin R-click inside window, then Run As Java Application

    Output (see Console tab at bottom),

    ManuallyCom ilin

    javac HelloWorld.java

    Executing

    Output Hello, World

  • 7/28/2019 Java With Eclipse - Setup and Getting Started

    11/14

    Basic Hello WWW Applet

    File HelloWWW.java:

    import java.applet.Applet;import java.awt.*;

    public class HelloWWW extends Applet {

    ublic void init() {

    setBackground(Color.BLACK);

    setForeground(Color.WHITE);

    setFont new Font "SansSerif" Font.BOLD 30

    }

    g.drawString("Hello, World Wide Web.", 5, 35);

    }

    Basic Hello WWW Applet

    File HelloWWW.html:

    HTML>HelloWWW: Simple Applet Test.

    HelloWWW: Simple Applet Test.

    Error! You must use a Java enabled browser.

  • 7/28/2019 Java With Eclipse - Setup and Getting Started

    12/14

    Basic Hello WWW Applet

    CompilingEclipse: save the file

    Manual: javac HelloWWW.java

    Running:Load HelloWWW.html in a Java-enabled browser

    c pse, us rag e e on o rowser

    2010 Marty Hall

    Wrap-Up

    Customized Java EE Training: http://courses.coreservlets.com/Servlets, JSP, JSF 2.0, Struts, Ajax, GWT 2.0, Spring, Hibernate, SOAP & RESTful Web Services, Java 6.

    Developed and taught by well-known author and developer. At public venues or onsite at yourlocation.

  • 7/28/2019 Java With Eclipse - Setup and Getting Started

    13/14

    A Few Eclipse Tricks

    Making a new project e ew ro ec ava ava ro ec

    Choose Source/Classes in Same Folder

    Making a new class R-click project, New Class

    Autocompletion T e art of a class or method name Control-S ace

    Inserting main method Type the word main, then Control-Space

    . . Type the word sysout, then Control-Space

    Renaming a variable or method Select method or variable, R-click, Refactor Rename

    Will also change all places that refer to it

    Summary

    Downloading Javahttp://java.sun.com/javase/downloads/index.jsp

    Downloading Eclipse ttp: ec pse.org

    Downloading sample projects . .

    Click on Java 6 Programming tutorial on left side

    Import with File Import Existing Projects

    Executing a class that has mainR-click in code, Run As Java Application

  • 7/28/2019 Java With Eclipse - Setup and Getting Started

    14/14

    2010 Marty Hall

    ues ons

    Customized Java EE Training: http://courses.coreservlets.com/Servlets, JSP, JSF 2.0, Struts, Ajax, GWT 2.0, Spring, Hibernate, SOAP & RESTful Web Services, Java 6.

    Developed and taught by well-known author and developer. At public venues or onsite at yourlocation.