Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Tutorial 5: Developing Java applications Georgios Gousios [email protected] Department of Management Science and Technology Athens University of Economics and Business Revision : 1117 Tutorial 5: Developing Java applications – p. 1 Contents The Java Development Environment The classpath Invoking the compiler and the VM The Eclipse IDE Tutorial 5: Developing Java applications – p. 2 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 Java C l a s s Bytecode l o a d e r Verifier Intpr JIT VMTutorial 5: Developing Java applications – p. 3 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. 4 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. 5 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. 6 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. 7 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 Tutorial 5: Developing Java applications – p. 8 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> javadoc -d doc -classpath src/java/ net.sf.jftp Tutorial 5: Developing Java applications – p. 9 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 Tutorial 5: Developing Java applications – p. 10 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> Tutorial 5: Developing Java applications – p. 11 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 ant [options] (executes the default target) Tutorial 5: Developing Java applications – p. 12 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> Tutorial 5: Developing Java applications – p. 13 Java IDEs • IDE : Integrated Development Environment • Usually a very competent editor along with an integrated build system • Facilities offered by modern IDE s ◦ Editor with syntax highlighting, auto indentation and code folding ◦ Auto-completion of object and method names with integrated help system ◦ Visual editors for GUI s and debuggers ◦ Interfaces to servers and databases ◦ Automatic creation of a files ◦ Interfacing to version control systems Tutorial 5: Developing Java applications – p. 14 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. 15 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. 16 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. 17 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 Tutorial 5: Developing Java applications – p. 18 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) Tutorial 5: Developing Java applications – p. 19 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. 20 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) Tutorial 5: Developing Java applications – p. 21 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. 22 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. 23 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" Tutorial 5: Developing Java applications – p. 24 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 • At the next screen, select from the dropdown menus the targets that are closer to the descriptions Tutorial 5: Developing Java applications – p. 25 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 Tutorial 5: Developing Java applications – p. 26 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 prerequisite Tutorial 5: Developing Java applications – p. 27 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. 28 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--> /> </target> Tutorial 5: Developing Java applications – p. 29 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 Tutorial 5: Developing Java applications – p. 30 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 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 Tutorial 5: Developing Java applications – p. 32 32-1 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 Tutorial 5: Developing Java applications – p. 33 33-1