Introduction to Java 2 Programming Lecture 4 Writing Java Applications, Java Development Tools.

21

Click here to load reader

Transcript of Introduction to Java 2 Programming Lecture 4 Writing Java Applications, Java Development Tools.

Page 1: Introduction to Java 2 Programming Lecture 4 Writing Java Applications, Java Development Tools.

Introduction to Java 2 Programming

Lecture 4

Writing Java Applications, Java Development Tools

Page 2: Introduction to Java 2 Programming Lecture 4 Writing Java Applications, Java Development Tools.

Overview

• Java Programming Tools

• Command-Line Applications

• Practical Exercises

Page 3: Introduction to Java 2 Programming Lecture 4 Writing Java Applications, Java Development Tools.

Java Programming Tools

• Javac – the Java Compiler

• Java – the Java Executable (virtual machine)

• Javadoc – Java Documentation generator

• Jar – Java ARchive creator• All found in %JAVA_HOME%\bin

Page 4: Introduction to Java 2 Programming Lecture 4 Writing Java Applications, Java Development Tools.

Javac – the Java Compiler

• Generates bytecode (.class files) from source code (.java files)– One class file per source file

– Usage example:javac MyApplication.java

javac com\ldodds\intro2java\Robot.java

• Javac expects source code to be organised into directories according to package structure– One directory per “dotted” portion of package

Page 5: Introduction to Java 2 Programming Lecture 4 Writing Java Applications, Java Development Tools.

Sample Directory Structure

E.g:package com.ldodds.intro2java;

Page 6: Introduction to Java 2 Programming Lecture 4 Writing Java Applications, Java Development Tools.

Java – the Virtual Machine

• Executes the virtual machine and loads one or more classes into it

• Specify the class name (including packages) as a parameter– Don’t indicate the class file, but the class name

Examples:java MyApplication

java com.ldodds.intro2java.Robot

Page 7: Introduction to Java 2 Programming Lecture 4 Writing Java Applications, Java Development Tools.

Javadoc – the Documentation Generator

• Automatically generates HTML documentation from Java source code– Very efficient way to produce development

documentation for your application.– E.g:javadoc com\ldodds\intro2java\Robot.java

• Output can be customised in a number of different ways, – usually by adding special “tags” to the source code– List of useful tags in the Tools Reference handout

Page 8: Introduction to Java 2 Programming Lecture 4 Writing Java Applications, Java Development Tools.

Javadoc Example/** * A <i>simple</i> Calculator * * @author Leigh Dodds */public class Calculator{ /** * Adds two numbers together and returns the result */ public int plus(int x, int y) { return x + y; }}

Page 9: Introduction to Java 2 Programming Lecture 4 Writing Java Applications, Java Development Tools.

Jar Tool

• Java ARchive– Basically a zip file, used to package java classes– E.g. for delivery as an applet or application

• Usually contain a Manifest– An index of the contents of a jar file– Major benefit is indicating which class holds the

“main” method– Allows an application to be launched automatically

from a jar file• E.g. by double-clicking the archive

Page 10: Introduction to Java 2 Programming Lecture 4 Writing Java Applications, Java Development Tools.

The CLASSPATH

• The CLASSPATH is – How Java finds compiled classes– A system property– A list of directories and/or jar files (similar to PATH)– A common source of frustration!– E.g:CLASSPATH=c:\classes;c:\applications\app.jar

• Classify Java tools into three groups:– File based– CLASSPATH based– And “mixed mode” (I.e. use both)

Page 11: Introduction to Java 2 Programming Lecture 4 Writing Java Applications, Java Development Tools.

File based tools

• Javadoc, jar

• Accept command-line parameters referring to files.

• Read the directory structure to find related files

javadoc c:\intro2java\src\intro2java\*.java

• Result in file-based errors

Page 12: Introduction to Java 2 Programming Lecture 4 Writing Java Applications, Java Development Tools.

CLASSPATH based tools

• java, javap• Refer to classes and not files• Ignore the file-system, except for those directories

mentioned in the CLASSPATH– Starts at those directories and walks down to find the

correct .class files

– Can look inside JAR files to do the same

• Result in exceptions or errors, e.g. ClassNotFoundException; NoClassDefFoundErrors

Page 13: Introduction to Java 2 Programming Lecture 4 Writing Java Applications, Java Development Tools.

CLASSPATH based tools

• Example:

• E.g. CLASSPATH=c:\src

java com.ldodds.intro2java.Robot

Page 14: Introduction to Java 2 Programming Lecture 4 Writing Java Applications, Java Development Tools.

Mixed mode tools

• javac• Accept parameters referring to files• BUT, Read the CLASSPATH to find

related classesjavac c:\intro2java\src\intro2java\*.java

• Results in file errors (relating to parameters), and “cannot resolve symbol” errors (relating to missing classes)

Page 15: Introduction to Java 2 Programming Lecture 4 Writing Java Applications, Java Development Tools.

CLASSPATH Tips

• Always add “the current working directory” to the CLASSPATH

SET CLASSPATH=%CLASSPATH%;.

• Good idea keep classes and source separate (e.g. bin and src directories)– But need to be careful when compiling

• Use a “global” classes directory, e.g. c:\classes– Add this to the CLASSPATH– Always compile into that directory

SET CLASSPATH=%CLASSPATH%;c:\classesjavac –d c:\classes *.java

Page 16: Introduction to Java 2 Programming Lecture 4 Writing Java Applications, Java Development Tools.

Overview

• Java Programming Tools

• Command-line Applications

• Practical Exercises

Page 17: Introduction to Java 2 Programming Lecture 4 Writing Java Applications, Java Development Tools.

Command Line Applications

• Java has no notion of executable– Just loads classes into the virtual machine

• Starting a Java application involves calling a method– Which may then create objects, call other

methods, etc, etc– There needs to be a starting point to trigger the

application to run

Page 18: Introduction to Java 2 Programming Lecture 4 Writing Java Applications, Java Development Tools.

The main method

• To turn a class into an application, give it a “main” method:public static void main(String[] args)

• Must be of this format (otherwise Java can’t find it)

• Can then be invoked from the command-line– Do all the work to initialise the app (create objects, etc)

in this method– Minimise the amount of code in there: just create an

object or two, and call their methods.

Page 19: Introduction to Java 2 Programming Lecture 4 Writing Java Applications, Java Development Tools.

Command Line Parameters

• Parameters are passed in as an array• E.g.:java MyApplication param1 param2 param3

• Can also use “System Properties”– Parameters global to the whole virtual machine– Get them with the System.getProperty() method

//the command linejava –DpropertyName=propertyValue MyApplication

//In the codeString value = System.getProperty(“propertyValue”);

Page 20: Introduction to Java 2 Programming Lecture 4 Writing Java Applications, Java Development Tools.

Overview

• More Syntax– Constants, Strings, Arrays

• The Object Lifecycle

• Java Programming Tools

• Practical Exercises

Page 21: Introduction to Java 2 Programming Lecture 4 Writing Java Applications, Java Development Tools.

The Calculator