Vous êtes sur la page 1sur 15

© 2010 Marty Hall

Ja a with
Java t Eclipse:
c pse
Setup and Getting
St t d
Started
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 your location.

© 2010 Marty Hall

For live Java EE training, please see training courses


at http://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, g
Hibernate/JPA, and customized combinations of topics.
Taught by the author of Core Servlets and JSP, More
Servlets and JSP, JSP and this tutorial.tutorial Available at public
venues,Customized
or customized versions
Java EE Training: can be held on-site at your
http://courses.coreservlets.com/
organization. Contact hall@coreservlets.com for details.
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 your location.
Topics in This Section
• Installing Java
• Installing 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 your location.
Java Versions
• Java SE (Standard Edition)
– “Core” Java
• Java version used in this class
– Latest version is Java 6,
6 which should be used for almost
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 Web
apps andd other
h enterprise
i tasks
k
• Most real-life deployments start with Java SE and then get
server bundled with Java EE libraries

Which Java SE Version Should


You Use?
• Server-Side Applications
– Use the latest Java version that your app server supports
• JDK 1.4 – 1.6, depending how old your server is
– If you can choose,
choose use JDK 11.6
6 (but 11.5
5 still popular)
• Desktop Applications
– For best p
power and speed,
p , use Java 6 (aka
( 1.6))
• If you install, use JDK 1.6
– For best portability to existing deployments, use JDK 1.5
• Applets
A l t
– Separate Java Plug-In is required in all recent browsers
• For intranet apps,
apps use Java 6
• On internet, most Java applet sites (e.g., Yahoo! Games)
use JDK 1.4 or 1.5, but all require end-user installation
Installing Java
• Install Java 6
– http://java.sun.com/javase/downloads/index.jsp

Use this version. The “JDK – Java


Development Kit” includes compiler for This tutorial uses Eclipse, but if you prefer the
NetBeans environment, it is very easy to adapt
.java files, whereas the “JRE – Java the instructions to that development
Runtime Environment” is only for environment. So, if you prefer NetBeans or
executing prebuilt .class files. your organization has standardized on it, use
this download instead of (not in addition to) the
one on the left.

• Bookmark the Java API (“JavaDocs”)


– http://java.sun.com/javase/6/docs/api/
http://java sun com/javase/6/docs/api/
• 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 your location.
Eclipse IDE (Integrated
Development Environment)
• Overview
– E
Eclipse
li is
i a a free
f open source IDE for
f
Java. Support for Java, HTML, CSS,
JavaScript, C++, PHP, and more.
• Download from http://eclipse.org/
http://eclipse org/
• Choose “Enterprise Java” version
– As of 2/2010, version 3.5 called “Galileo”
• Features
– Checks your syntax as you type
– Automatically compiles every
time you save file
– Many tools: refactoring,
debugging, server integration,
templates for common tasks, etc.
• Low learning curve: beginners can use
Eclipse without knowing these tools

Running Eclipse
• Double click eclipse.exe
– From installDir/bin
• Click on “Workbench” icon
–N
Next time
i you bbring
i up Eclipse,
E li it
i will
ill come up in
i
workbench automaticallly
• Shortcut
– Many developers make shortcut to Eclipse on their
desktop
• R-click eclipse.exe, Copy, then go to desktop, R-click, and
Paste Shortcut
Eclipse: Running Programs
• Executing program from existing project
– Open existing
project
– Double click
Java file to
bring it up in editor
– R-click
li k anywhere
h
in code
– Select Run As 
Java Application
– Output goes in
C
Consolel att bottom
b tt
• Note: Class must
have a “main” method

Eclipse: Making Projects


• Creating new project
– File  New  Project 
Java  Java Project
• Pick any name
• To simplify applets later,
choose Sources/Classes
in same folder
• Creating new class
– R-click  New  Class
• You can choose to have
Eclipse make “main”
automatically
– You can also copy/paste existing
class, then give it new name
© 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 your location.

Creating and Running Program

• Create the File


– Write and save a file (say Test.java) that defines
public class Test
• Other than “real”
real Java IDEs (e
(e.g.,
g Eclipse
Eclipse, NetBeans
NetBeans,
Intellij IDEA), there are a number of text editors (e.g.,
TextPad, UltraEdit, Brief) with good Java syntax support.
– File and class names are case sensitive
• Compile the program
– Compile
p Test.java
j
> javac Test.java
• This step creates a file called Test.class
• Run the program
> java Test
• This step assumes your class has “main” method
© 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 your location.

Installing Sample Projects


• Code from all tutorial sections is available
online
li
– http://courses.coreservlets.com/
Course-Materials/java5 html
Course-Materials/java5.html
• Import project into Eclipse
– Click on appropriate
pp p tutorial section
– Download ZIP file
• The one for this section is called “intro”
– St
Startt E
Eclipse
li andd go to
t Workbench
W kb h
– File  Import  General  Existing Projects into
Workspace
p  Select archive file ((not “Select root
directory”).
• Then browse to ZIP file you downloaded, OK, Finish
Basic Hello World Application
• File HelloWorld.java:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello,
System.out.println( Hello, world.
world.");
);
}
}

• Notes
– “A
“Application”
li i ” isi lingo
li for
f a stand-alone
d l Java program
– An application is a Java class that contains “main”
• Most Java classes do not contain “main”,
main , but only those
that do can be directly executed

Executing HelloWorld
• In Eclipse
– Compiling
• Done automatically whenever you save a file
– Executingg
• R-click inside window, then Run As  Java Application
– Output (see “Console” tab at bottom)
• Hello,
Hello World
• Manually
– Compiling
p g
• javac HelloWorld.java
– Executing
• java HelloWorld
– Output
• Hello, World
Command-line Arguments
• Useful for learning and testing
– Command-line args are useful for practice
– But, programs given to end users should never use
command-line arguments
• They should pop up a GUI to collect input.
• Eclipse has poor support
– Entering command-line args via Eclipse is more trouble
than it is worth
– So,
So to test with command-line
command line args:
• Save the file in Eclipse (causing it to be compiled)
• Navigate to folder on desktop (not within Eclipse)
• Open command window (Run  cmd)
• Type “java Classname arg1 arg2 …”

Command Line Arguments


• File ShowArgs.java:
public class ShowArgs {
public static void main(String[] args) {
for(int i=0; i<args.length; i++) {
System out println("Arg
System.out.println( Arg " + i + " is "
+ args[i]);
}
}
}
• Differences from C
– In Java,
Java String is a real type
– Java arrays have an associated length
– The filename is not part of the command line arguments
• Eclipse
– To assign command line args: R-click, Run As, Run
Configurations, Arguments tab
Command Line Arguments:
Results
• Compiling
– Save file in Eclipse (or “javac ShowArgs.java” to do it
manually)
• Running
– Navigate to folder containing ShowArgs.class

> java ShowArgs fee fie foe fum


Arg 0 is fee
Arg 1 is fie
Arg 2 is foe
Arg 3 is fum

Command Line Arguments:


the for/each Loop
• File ShowArgs2.java:
public class ShowArgs2 {
public static void main(String[] args) {
for(String arg: args) {
System.out.println("Command line arg: "
+ arg);
}
}
}
• Differences from C
– New loop construct
Command Line Arguments,
Results
• Compiling
– Save file in Eclipse (or “javac ShowArgs2.java” to do it
manually)
• Running
– Navigate to folder containing ShowArgs2.class

> java ShowArgs2 fee fie foe fum


Command line arg: fee
Command line arg: fie
Command line arg: foe
Command line arg: fum

Basic Hello WWW Applet


• File HelloWWW.java:
import java.applet.Applet;
import java.awt.*;

public class HelloWWW extends Applet {


public void init()
p () {
setBackground(Color.gray);
setForeground(Color.white);
setFont(new
( Font("SansSerif",
( , Font.BOLD,
, 30));
));
}

public void paint(Graphics g) {


g.drawString("Hello, World Wide Web.", 5, 35);
}
}
Basic Hello WWW Applet
(Continued)
• File HelloWWW.html:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0
Transitional//EN">
<HTML>
<HEAD>
<TITLE>HelloWWW: Simple Applet Test.</TITLE>
</HEAD>

<BODY>
<H1>HelloWWW: Simple Applet Test.</H1>

<APPLET CODE="HelloWWW.class" WIDTH=400 HEIGHT=40>


<B>Error! You must use a Java enabled browser.</B>
</APPLET>

</BODY>
</HTML>

Basic Hello WWW Applet


(Continued)
• Compiling
– Eclipse: save the file
– Manual: javac HelloWWW.java
• Running:
Load HelloWWW.html in a Java-enabled browser
With EEclipse,
li navigate
i t tot folder
f ld on file
fil system,
t then
th double
d bl
click HTML file or drag it onto browser
© 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 your location.

A Few Eclipse Tricks


• Making a new project
Fil  New
– File N  Project
P j t  Java
J  Java
J P
Project
j t
• Choose Source/Classes in Same Folder
• Making a new class
– R-click project, New  Class
• Autocompletion
– Type
yp part
p of a class or method name,, Control-Space
p
• Inserting main method
– Type the word “main”, then Control-Space
• Inserting System.out.println
System out println
– Type the word “sysout”, then Control-Space
• Renaming
g a variable or method
– Select method or variable, R-click, Refactor  Rename
• Will also change all places that refer to it
Summary
• Downloading Java
– http://java.sun.com/javase/downloads/index.jsp
• Downloading Eclipse
– http://eclipse.org
h // li
• Downloading sample projects
– http://courses
http://courses.coreservlets.com/
coreservlets com/
Course-Materials/java5.html
– Import with File  Import  Existing Projects …
• Executing a class that has “main”
– R-click in code, Run As  Java Application

© 2010 Marty Hall

Q
Questions?
ti ?
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 your location.

Vous aimerez peut-être aussi