Tutorial 5: Developing Java applications p. 1 Tutorial 5: Developing Java applications Georgios Gousios gousiosg@aueb.gr Department of Management Science and Technology Athens University of Economics and Business Revision : 1117
Tutorial 5: Developing Java applications p. 2 Contents The Java Development Environment The classpath Invoking the compiler and the VM The Eclipse IDE
Java Bytecode VM Tutorial 5: Developing Java applications p. 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 Javac C l a ss l o a d e r Verifier Intpr JIT
Tutorial 5: Developing Java applications p. 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
Tutorial 5: Developing Java applications p. 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 Default libraries in $JAVA_HOME/lib
Tutorial 5: Developing Java applications p. 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=: java -cp $HOME/jarpeb.jar gr.aueb.dds.exercise1
Tutorial 5: Developing Java applications p. 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 ant: A Java oriented build system
Tutorial 5: Developing Java applications p. 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> avac -d classes Hello.java avac -classpath./src/java:./lib/ \ src/java/net/sf/jftp/*.java
Tutorial 5: Developing Java applications p. 9 javadoc avadoc [options] [packagenames] [sourcefiles] -d dir Output files todir -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> avadoc -d doc -classpath src/java/ net.sf.jftp
Tutorial 5: Developing Java applications p. 10 hejar 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
Tutorial 5: Developing Java applications p. 11 hejar program distribution format The jar program is similar to the Unixtar 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>
Theant 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 ant [options] (executes the default target) Tutorial 5: Developing Java applications p. 12
Tutorial 5: Developing Java applications p. 13 Examplebuild.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>
Java IDEs 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 Interfacing to version control systems Tutorial 5: Developing Java applications p. 14
Tutorial 5: Developing Java applications p. 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 On Windows: Double-click the Eclipse icon
Tutorial 5: Developing Java applications p. 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)
Tutorial 5: Developing Java applications p. 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!
Tutorial 5: Developing Java applications p. 18 ecking 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
Tutorial 5: Developing Java applications p. 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 everything is OK, Eclipse should compile the project cleanly (no d markers in the Package Explorer)
Tutorial 5: Developing Java applications p. 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
Tutorial 5: Developing Java applications p. 21 Running and Debugging ebugging 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)
Tutorial 5: Developing Java applications p. 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
Tutorial 5: Developing Java applications p. 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 On Windows: Double-click the Netbeans icon
Tutorial 5: Developing Java applications p. 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"
Importing a project the project isant-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 thebuild.xml file is not in the project s top dir, everything should be auto-filled. Else, select the build.xml file At the next screen, select from the dropdown menus the targets that are closer to the descriptions Tutorial 5: Developing Java applications p. 25
Tutorial 5: Developing Java applications p. 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 atest directory, import it in the "Test Package Folders" field At the next screen, import the jar files present in thelib directory and other possible depedencies
Tutorial 5: Developing Java applications p. 27 Importing a project the project is notant-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 simpleant build file automatically, which can then be edited. Knowledge ofant is a prerequisite
Tutorial 5: Developing Java applications p. 28 Practice Import your projects (Ask the demonstrator for help) In case you don t have a project yet: Try Jalopy
Tutorial 5: Developing Java applications p. 29 Running the project If the project contains a runant 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) arget 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--> /> target>
Tutorial 5: Developing Java applications p. 30 Browsing the project The project classes are displayed organised in packages Classes withmain 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
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 Tutorial 5: Developing Java applications p. 31
Tutorial 5: Developing Java applications p. 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
32-1
Tutorial 5: Developing Java applications p. 33 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: Anant task must be present to run a test case
33-1