introduction to java 2 programming lecture 4 writing java applications, java development tools
TRANSCRIPT
Introduction to Java 2 Programming
Lecture 4
Writing Java Applications, Java Development Tools
Overview
• Java Programming Tools
• Command-Line Applications
• Practical Exercises
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
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
Sample Directory Structure
E.g:package com.ldodds.intro2java;
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
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
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; }}
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
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)
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
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
CLASSPATH based tools
• Example:
• E.g. CLASSPATH=c:\src
java com.ldodds.intro2java.Robot
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)
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
Overview
• Java Programming Tools
• Command-line Applications
• Practical Exercises
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
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.
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”);
Overview
• More Syntax– Constants, Strings, Arrays
• The Object Lifecycle
• Java Programming Tools
• Practical Exercises
The Calculator