create new tag
view all tags

Learning Java

This wiki page will show a series of exercises in regards to Java. Over time will cover many aspects of Java. Please try out the exercises and give feedback to Kevin Benson kmb@mssl.ucl.ac.uk. All exercises tend to have detailed Example source code to help you. The examples tend to be quite detailed in the early exercies e.g. A and B almost showing line for line exactly what you need. As your getting more comfortable with Java the later exercises will show less detail and concentrate on certain aspects of the Exercise. Exercise A particularly has a lot of information below since this is the first exercise to get set up.


Each exercise will give you all the information you need and Exercise A will go into more detail the first time as you see below for setting up Eclipse if you desire to use Eclipse. You also need to use Java API Javadocs as a Reference which is found here: http://java.sun.com/javase/6/docs/api/index.html (Note Java 5 or 6 should be fine for all exercises). Also JUnit Javadocs are here: http://junit.sourceforge.net/javadoc_40/index.html. All 3rd party libraries tend to have an API javadoc for reference on how to use along with other various documentation. Better to search/google for the library and read about the library if there is something you don't understand or e-mail me and I will try to help.

Exercise A (consists of 5 small exercises):

This is the first exercise and for the most part small and concentrates on areas you will come in contact more with Java. If you use Eclipse please create your own projects and add classes and other resources to your own project; please do not use the MSSLExercises Project given to you, MSSLExercises Project is a guide to help you but you should create all projects and classes yourself. Areas covered in this Exercise:
  • Using Junit Tests (Notice there is no main used everything done is via Junit tests)
  • 3rd Party Libraries.
  • Use Logging. Don't do System.out anymore.
  • Basic Java Bean (setters&getters class)
  • Reading and Writing XML.
  • Database interaction.

More Info: The above list seems alot but actually there is not to much code. All the things you will learn above you will need to learn to write a Java Program. Probably could guarantee any Java Project to encompass the first four points and probably one or both of the other points will happen.

The zip file example below can be imported into Eclipse or can simply run the 'run.sh' (Please run it at the same location as run.sh hence MSSLExercises directory) command to compile and run the tests via commandline. Instructions below on setting up in Eclipse. Just some advise don't feel you have to use Eclipse, Ant, or Maven to do builds if you have a small program that is useful feel free to distribute it with a .sh and/or .bat file to build and run and write it with your favorite text editor, I only recommend you make a README to explain how to run the program and never use hard coded path names try to use an environment variable or give instructions of exactly how to run it to not set any variables. As your program(s) gets bigger though you will find Eclipse and other build tools a major help.

  • Packages used in Java API Reference
    • java.lang
    • java.util
    • org.w3c.dom
    • java.sql
    • javax.xml.transform and javax.xml.parsers (a little)
    • java.io (a little)

  • Setting up Examples in Eclipse (after you unzipped your Examples)
    • File->Import Choose (Import Existing Project into Workspace) hit Next
    • Now browse to where you unzipped the Examples and choose the directory
    • MSSLExercises should be found as the only project and automatically chosen. Advise to check the box 'Copy into Workspace' to copy it into your eclipse work area otherwise it will remain at the location you unzipped it.
    • Hit Finish and you should see your project and browse the various source code and Junit tests.

  • When creating a New Project
    • Here are the things done in the Examples that you would need (or advise to do) in your New Project. Feel Free to look at Project->Properties for the MSSLExercises project to see what was done.
    • After creating a new Java Project go to its Properties (Project->Properties).
    • In the left tab click on 'Java Build Path'
    • Now select 'Libraries' from the right side near the top (sort of a tab button)
    • Here click 'Add Jar' and select the jars that you use in your project you are almost certain to use Log4j as noted in Examples. Be sure these jars are in your workspace such as a 'lib' directory. Don't use an external jar outside your workspace. (for exercise A you will need both log4j and mysql libraries)
    • Now also click 'Add Library' and select JUnit
    • Hit Next and choose Junit 4 then click Finish.
    • Now click on the 'Source' from the right side near the top
    • And in the 'Default Output Folder' there should already be your {project name} and a build directory to it such as '{project name}/target'
    • Hit OK.

  • Running Junit via Eclipse: Originally I tried to just click on a Junit java file in mssl.exercises.test and in the menu did Run->Run As->Junit test but unfortunately did not see anything happen in the console. So setting up a Junit configuration solved the problem.
    • In the menu do Run->Run
    • In the left area click on Junit New Configuration.
    • Now give the Configuration a name. Select 'Run all tests ....' and in the text box have 'mssl.java/exercises.test'
    • You can try to hit OK and run tests most should run but if you get an error saying it cannot find a 'java_training.properties' file then go back to this configuration and click on the 'Source' tab.
    • And hit the 'Add' button choose 'Workspace Folder' and choose the MSSLExercises folder this should correct the problem for it to find the properties file used in some of the tests.
    • After you run it once then you should be able to click Run or the green arrow to run it over and over again.

  • Eclipse Views and Windows: People sometimes ask what Tabs and Views are setup on the Eclipse. Arrange them how you like. But typically I have on the Left side tabs of 'Navigator, Package, CVS Repo, SVN Repo' and at the bottom I have tabs of: 'Problems, Javadoc, Console, Synchronize, Junit' Console by the way will show you all the stuff printed out from log4j and Junit will show your test information.

  • Running via Shell script
    • If you decide to go with text editor and a shell script no problem just follow the Example Guide shell script.
    • You will need to add mysql.jar and the ...TestsDB.java to your junit run. It was excluded in the Guide because it requires being inside the mssl network for connecting to the database.

Exercise B (Not written yet):

Will cover:
  • More on XML with Validation. XML has a lot of power in its ability to describe information and to give rules and restrictions on the content and how the content is formed. Because of this will dive a little deeper and write a small schema, read and write some xml based on that schema and to validate against the schema. Note this exercise will not go into Schema structures just reference links.
  • Calling a Soap Web Service probably Simbad.
  • Using interfaces.

Exercise C (Not written yet):

Will cover:
  • Data Binding --

Exercise D (Not written yet):

Will Cover:
  • Installing a Servlet Container (Tomcat)
  • Writing basic Servlet and JSP's pages to serve to the web.

-- KevinBenson - 28 Aug 2007

Edit | Attach | Watch | Print version | History: r3 < r2 < r1 | Backlinks | Raw View | More topic actions
Topic revision: r3 - 2007-08-28 - KevinBenson
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2020 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback