• No results found

Tutorial 5: Developing Java applications

N/A
N/A
Protected

Academic year: 2021

Share "Tutorial 5: Developing Java applications"

Copied!
35
0
0

Loading.... (view fulltext now)

Full text

(1)

Tutorial 5: Developing Java

applications

Georgios Gousios

[email protected]

Department of Management Science and Technology Athens University of Economics and Business

(2)

Contents

The Java Development Environment

The classpath

Invoking the compiler and the VM

The Eclipse

IDE

(3)

The Java approach to software

• The compiler is relatively simple

• The source code is transformed to an intermediate representation (bytecode) suitable for transfer among different platforms

• Java Virtual Machine ◦ Interpreter

◦ Just-in-Time (JIT) compiler

Verifier l o a d e r C l a s s JIT Intpr Javac

(4)

Main concepts of the Java platform

• Write once, run everywhere

• Classloader

• Security

◦ Bytecode verifier

◦ Array bounds checks, no pointers

◦ Security manager - Sandbox

• Internet centric

• Rich API’s

(5)

The Classpath

• The classpath is a string that helps the Java language to find its libraries.

• Syntax

CLASSPATH=path1:path2, e.g.

CLASSPATH=$HOME/jarpeb.jar:$HOME/test/:. • An environment variable in Unix and Windows or a

command line argument.

• A classpath can contain both paths to .jar files and to directories containing .class files

(6)

The Classpath

• The package path is added to the classpath when searching for classses

Examples:

CLASSPATH=$HOME/java/:$HOME/jarpeb.jar:. java gr.aueb.dds.Exercise1

CLASSPATH=:

(7)

Important programs

• java: The execution environment

• javac: The source to bytecode compiler

• javadoc: Tool that generates sourcecode

documentation from special comments in the

source code

• jar: Tool that creates and manipulates Java

archives

(8)

javac

• Use the -classpath switch to specify where the compiler can find user libraries and source files

• Use the -verbose switch to see what classes are loaded by the compiler during compilation

• Use the -g switch to generate debugging information • Ant task: <javac>

javac -d classes Hello.java

javac -classpath ./src/java:./lib/ \ src/java/net/sf/jftp/*.java

(9)

javadoc

javadoc [options] [packagenames] [sourcefiles] • -d dir Output files to dir

• -classpath Where to find source or class files to process

• -sourcepath Where to find source files to process • -docletpath Which style to use for the output

• Ant task: <javadoc>

(10)

The jar program distribution format

• A jar file contains class files and auxiliary resources

• The jar file can contain a META-INF/Manifest.mf file to: ◦ Define the default class to be executed

◦ Include cryptographic hashes of the jar to verify its integrity

◦ Version information

• The contents of a jar archive are compressed with the zip algorithm

(11)

The jar program distribution format

• The jar program is similar to the Unix tar program

• To create a JAR file:

jar cf jar-file input-file(s) • To view the contents of a JAR file:

jar tf jar-file

• To extract the contents of a JAR file: jar xf jar-file

• Running jar-Packaged Software: java -jar jar-file • Ant task: <jar>

(12)

The ant build system

• Used to manage the build process for large (>5 source files) programs

• Uses an XML description file for information on what to do • Basic concepts:

◦ Project: A collection of resources to be processed ◦ Target: A set of processing actions

◦ Task: A single action

• Common targets: compile, build, clean, test, run, javadoc

ant [options] targets or

(13)

Example build.xml file

<project name="J-FTP" default="jars" basedir="."> <property name="compile.debug" value="true"/>

<property name="compile.deprecation" value="false"/> [...]

<target name="compile" depends="prepare" description="Compile components"> <javac srcdir="src/java" destdir="build/classes" debug="${compile.debug}" deprecation="${compile.deprecation}" optimize="${compile.optimize}" nowarn="true"> <classpath refid="compile.classpath"/> </javac> </target> </project>

(14)

Java

IDE

s

• IDE: Integrated Development Environment

• Usually a very competent editor along with an integrated build system

• Facilities offered by modern IDEs

◦ Editor with syntax highlighting, auto indentation and code folding

◦ Auto-completion of object and method names with integrated help system

◦ Visual editors for GUIs and debuggers ◦ Interfaces to servers and databases ◦ Automatic creation of a files

(15)

The Eclipse

IDE

• Open source version of the proprietary Sun Java Studio

IDE

• Available at http://www.eclipse.org

• Should also download the Javadoc Java documentation zip file

http://java.sun.com/javase/6/docs/api/

• To run

◦ On Linux: From a console execute the eclipse command

(16)

The Eclipse

IDE

- Basic principles

• Basic environment only supports Java, but plug-ins exist for all known

languages (including PHP)

• Plug-ins are updated from central repositories

• Everything is organised around projects - several types of projects depending on the language

• Projects are automatically compiled each time you save

• Projects can be version controlled to support team development • Each language contains editors with features such as auto

completion, syntax highlighting and auto formatting • Most languages also include a graphical debugger

• Projects can be viewed from different perspectives (ways of organising/presenting files)

(17)

Initial configuration

• Install necessary plug-ins (Subversion and PHP)

• From the Tools menu, choose the Software Updates option and click on Find and Install option

• Choose “Search new features...”

• From the dialog that opens, choose new remote site • Add the following for PHP and SVN

◦ SVN http://subclipse.tigris.org/update_1.2.x/

◦ PHP http://download.eclipse.org/tools/pdt/updates/ • Some plug-ins might have dependencies, ask for help!

(18)

Checking out projects from a repository

• File->New->Project->SVN->Check out projects from SVN

• Create new repository and set the URL to your SVN URL • Select your project directory from the list

• Click finish – you will be presented with the New Project Wizard • Select either Java Project or the corresponding project type if

you are not using Java

• Enter a project name and click finish • The checkout process begins

(19)

Configuring the project

• Menu Project->Properties

• Select the Java Build Path from the list on the left

• Set the directories where the source code in the Source tab dialogue

• In the same tab, click on “Allow output folders for source folders” • Add the libraries (jar files) to the build path at the Libraries tab If everything is OK, Eclipse should compile the project cleanly (no red markers in the Package Explorer)

(20)

Running and Debugging

• Before running the project, you must create a Run configuration • Menu Run->Run

• Double click on “Java application”

• Choose a name for the configuration and set the project main class in the Main tab

• If your application is working in the command line, set the appropriate arguments in the Arguments tab

• Click on Apply and then on Run. Your program will start

• You can create as much Run configurations as you like, eg to test various command line options

(21)

Running and Debugging

Debugging

• Set a breakpoint - Eclipse is smart, it won’t allow you to set breakpoints to places not participating in execution, e.g. class member declarations

• Click on Run->Debug and in the dialogue that appears select the Run configuration you ’ve created before

• Eclipse will switch to the debug perspective, you will then able to see windows displaying local variables and executing

threads.

• The program will execute up to the breakpoint. You can then execute the program line by line (F5 to go to next line)

(22)

The team synchronising perspective

• Menu Window->Open perspective->Team Synchronizing. Once

again, you have to create a synchronisation configuration

• Click on the synchronise button and select SVN from the sync types. Then, select your project and click finish

• Before committing or updating, you can inspect the changes that will be made per file by double-clicking on each file

• All actions are accessible by right clicking on files and directories

• Be sure not to commit the binary output file

• New files must be added to the repository before being committed to it

(23)

The Netbeans

IDE

• Open source version of the proprietary Sun Java Studio

IDE

• Available at http://www.netbeans.org

• Should also download the Javadoc Java documentation zip file

• To run

◦ On Linux: From a console execute the netbeans command

(24)

Initial configuration

• Add the Javadoc archive

◦ From the Tools menu open the Java Platform Manager ◦ Select the Javadoc tab and click the Add ZIP/Folder

button

• Choose a nice font for the editor

◦ From the Tools menu select Options

◦ Font can be changed under "Editing", "Java Editor", "Fonts and Colors"

(25)

Importing a project

If the project is ant-based (it ’d better be!)

• Open "New Project" from the "File" menu

• Choose "General" and "Java project with Existing Ant Script" and click next

• Open the project’s top level directory in the "Location" field

• Unless the build.xml file is not in the project’s top dir, everything should be auto-filled. Else, select the

build.xml file

(26)

Importing a project

• At the next screen, select the directories that contain

source files. Care should be taken to correctly import the package hierarchies. Open a random source file and use the following heuristic to find the top-level dir to include: package x.y.z --> dir hierarchy x.y.z

Import the dir containing x

• If the project has a test directory, import it in the "Test Package Folders" field

• At the next screen, import the jar files present in the lib directory and other possible depedencies

(27)

Importing a project

If the project is not ant-based:

• Open "New Project" from the "File" menu and choose "General" and "Java project with Existing sources"

• Select the input source and library directories

• Netbeans builds a simple ant build file automatically, which can then be edited. Knowledge of ant is a

(28)

Practice

Import your projects

(Ask the demonstrator for help)

In case you don’t have a project yet:

Try

Jalopy

(29)

Running the project

• If the project contains a run ant target, then F6 will execute the project

• If not, write a custom run target:

◦ Open the build.xml file from the project hierarchy ◦ Use the following template

◦ Add the new target to the project configuration (Project properties->Run and Build)

<target name="run" depends="build"> <java

dir="build" <!--Compilation output dir--> classname=""<!--Class to execute-->

jar="" <!--Application main jar--> fork="true" <!--Only when jar is set--> classpath=""<!--Where to find libraries--> />

(30)

Browsing the project

• The project classes are displayed organised in packages • Classes with main methods are specially marked

• By clicking on a variable we can view its declaration

(Ctrl+O), its source implementation (Ctrl+Shift+G) and its usage (Shift+F7)

• We can search either by text or by type names in both the currently open file or the whole project

• Auto-completion can help to view both functions and documentation for a class

(31)

Debugging

• Breakpoints: Used to stop a program at a certain line of code

• Watch: A variable whose value is constantly monitored • The debugger runs the project until the first breakpoint is

encountered

• Whenever the debugger stops, all local variable values are displayed along with running threads and watches • Step over/into/out: Go over/into the declaration/out of the

declaration of a function

• Run to cursor: Set the cursor to a line in code. The debugger stops execution when the line is reached

(32)

Writting Javadoc comments

• Netbeans features Javadoc integration

• To add Javadoc to a class: Tools -> Autocomment

• Netbeans automatically parses Javadoc comments and adds them to popup class descriptions during

autocompletion

• To search for Javadoc help: Todo -> Javadoc Index Search

• To generate Javadoc for project: Build -> Generate Javadoc

(33)
(34)

Writting tests

• Netbeans features JUnit integration • To create a test suite for the project

◦ Unless it exists, create a test directory: Go to Project properties and add an empty folder to Test package folders

◦ Go to Tools -> JUnit tests -> Create tests to generate a test case for the currenlty open class.

• To run a test: An ant task must be present to run a test case

(35)

References

Related documents

Provide continuity of medical record information for care and advice when office is closed.. Provide timely advice by phone when office is

Select the MySQL JDBC Driver with the matching System Version (January 2015: 5.1) and select the «Jar List»-Tab.. Remove the current Jar in the list and click on «Add JAR/Zip…».

This code is provided via email (or message to a smart phone) after entering the username and password for the site.. After entering my username and password, the site sends me an

A computer file is a collection of information which is stored (saved) on computer storage media such as a hard disk drive or flash drive.. Since a hard drive usually

Word template free planner templates for folder tabs, end tab folders with these items marked as files become available click on your front and crucial portion.. Administered

• Basic sharing and advanced sharing allow you to control who may access specific files and folders located in your libraries.. • Permissions allow you to restrict the actions other

In the left pane, click the type of file you want to search for, or click All files and folders.. Based on the type of file you chose to search for, specify

assign - A method in Group Policy of installing software and making the application available to all users or computers in a designated site, domain, organizational unit (OU), or to