Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations

Presentation on theme: "Introduction to Java 2 Programming Lecture 4 Writing Java Applications, Java Development Tools."— Presentation transcript:

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

2 Overview Java Programming Tools Command-Line Applications Practical Exercises

3 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

4 Javac – the Java Compiler Generates bytecode (.class files) from source code (.java files) –One class file per source file –Usage example: javac javac com\ldodds\intro2java\ Javac expects source code to be organised into directories according to package structure –One directory per dotted portion of package

5 Sample Directory Structure E.g: package com.ldodds.intro2java;

6 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 –Dont indicate the class file, but the class name Examples: java MyApplication java com.ldodds.intro2java.Robot

7 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\ 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

8 Javadoc Example /** * A simple 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; }

9 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

10 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)

11 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

12 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

13 CLASSPATH based tools Example: E.g. CLASSPATH=c:\src java com.ldodds.intro2java.Robot

14 Mixed mode tools javac Accept parameters referring to files BUT, Read the CLASSPATH to find related classes javac c:\intro2java\src\intro2java\*.java Results in file errors (relating to parameters), and cannot resolve symbol errors (relating to missing classes)

15 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:\classes javac –d c:\classes *.java

16 Overview Java Programming Tools Command-line Applications Practical Exercises

17 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

18 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 cant 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.

19 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 line java –DpropertyName=propertyValue MyApplication //In the code String value = System.getProperty(propertyValue);

20 Overview More Syntax –Constants, Strings, Arrays The Object Lifecycle Java Programming Tools Practical Exercises

21 The Calculator

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

Similar presentations

Ads by Google