Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439

This exercise will guide you through preparing some software packages that are required for future lab exercises and assignments.

In COMP9322, we will create two separate environments: development and deployment. In development, you may use your own computer (Mac, Windows or Linux, your laptop, PC, or CSE lab machines) to develop the code/project. In deployment, we will define 'COMP9322 standard deployment environment' to run your lab/assignment solutions in. The deployment environment will be mostly based on the development environment we setup in this lab.

In all lab exercises from here on, we assume that you will be working in your development environment.

Part 1: Preparation (what you already have from COMP9321)

To make the instructions more readable, the ~ characterwill be used todenote your CSE home directory. When you are asked to issue commands, you should do so from a Terminal session. The $ symbol will be used to mean the prompt you see in the Terminal.

You should be able to apply these instructions to your own OS environment. All lab instructions we provide here can be easily translated to Windows, Mac and Linux.

NOTE - For Java, Eclipse IDE and Tomcat Server, we are not going to give you detailed instructions here, as you are already familiar with them from COMP9321. However, if you have any problem following these instructions in your own computer, just come to the lab consultations or post a question on the messageboard. There will be help available.

Create ~/cs9322-Prac

To avoid compatibility issues, what I recommend is for everybody to use 9322 standard software tool set - AND to install the tools under the directory called cs9322-Prac under your home directory.

For example, mine is: /Users/hpaik/cs9322-Prac/ (under a Mac).

Let's call this (~/cs9322-Prac) $9322_PRAC$.

If you do not have copies of Eclipse or Tomcat on your machine, install them under $9322_PRAC$.

You may have Tomcat, Eclipse, etc. running in your environment already, then I'd recommend re-installing them under $9322_PRAC$, using the exact versions recommended by the lab instructions, even if that means you are duplicating the installation. If you are absolutely against this, make sure that the versions you keep are identical to what we are using here.

The starting list included in the 9322 standard software tool set are:

  1. Apache Tomcat 8.0.32
  2. Maven 3.2.9
  3. Eclipse IDE - Luna

Java Version

We use Java 1.7 (no Java 1.8 please). If you have multiple versions of Java installed on your machine, make sure that you have JAVA_HOME set to Java 1.7 distribution. How to do this will be different depending on your environment. If you are not sure, either post a question on the messageboard or come to me.

If you are using Mac, the following little trick will allow you to switch JAVA_HOME easily. Put this in your ~/.profile.

#set JAVA_HOME (-v get the version you want)export JAVA_HOME=$(/usr/libexec/java_home -v 1.7)

Eclipse IDE (Luna)

Eclipse Luna is one of the 9322 standard software tool set. We use J2EE developer package of Eclipse. Luna is not the latest version, but it is compatible with Java 1.7. CSE Lab recently has been upgraded to Eclipse Luna.

Eclipse Luna, you can find the link here.

  1. On the page, look for Eclipse IDE for Java EE Developers and suitable distribution for your environment.

Download the file and install Luna under $9322_PRAC$.

Quick configuration of Eclipse Luna.

  1. Create a directory called workspace under $9322_PRAC$ (i.e., $9322_PRAC$/workspace).
  2. When the Eclipse starts, set the above workspace as your default workspace for this Eclipse.
  3. Go to Menu, Preferences, then Java.
    • Set compiler level to 1.7.
    • Set Installed JRE to JDK 1.7 in your own environment. Choose 1.7 as default (i.e., ticked).

Tomcat Server (v8.0.32)

Apache Tomcat version 8.0.32 is one of the 9322 standard software tool set. If you have setup Tomcat from the COMP9321 course. You can leave the setup as-is and create a new installation for this course.

You can find the Tomcat 8 binary distributions here.

Download Tomcat 8 and install it under $9322_PRAC$. Just make sure that you have the important environment variables: CATALINA_BASE and CATALINA_HOME set as expected. For example, mine is set to (using my own $9322_PRAC$):

export CATALINA_BASE=/Users/hpaik/cs9322-Prac/apache-tomcat-8.0.32export CATALINA_HOME=/Users/hpaik/cs9322-Prac/apache-tomcat-8.0.32

Set these values to your own environment. Again, how to set these values is different depending on which computing environment you are using. If you are not sure, post a message or come to me.

After installing, start and stop the server to make sure that everything is OK.

Part 2: Installing Apache Maven and Eclipse Maven Plug-in

For the Web service development exercises, we will use Apache Maven and Apache CXF as main tools. In this part of the lab, we will first setup Mavan, as Apache CXF uses it heavily!

Installing and setting up Maven

Maven version 3.3.9 is one of the 9322 standard software tool set. Maven is a popular build/project management tool. We rely on Maven to (i) generate project templates, (ii) manage dependencies/classpath (iii) compile and package project artifacts.

We do not need to understand the functionality of Maven comprehensively. We can start with the parts that are relevant to our given tasks and, of course, learn more about the tool as we go.

NOTE: Again, you may have Maven installed already. If you do not want to duplicate Maven installation, then make sure that you do have the same version available and MAVEN_HOME is properly set.

Otherwise, we will download Maven 3.3.9 and install it under $9322_PRAC$.

  1. Download Maven binary distribution


    Look for Maven 3.3.9 (current stable version). Choose either tar.gz or zip binary distributions to download.

  2. Extract the downloaded package (e.g., apache-maven-3.3.9-bin.tar.gz) to $9322_PRAC$. For the sake of illustration, you'd do:

    $ cd ~/cs9322-Prac
    $ tar -zxvf apache-maven-3.3.9-bin.tar.gz
    $ rm apache-maven-3.3.9-bin.tar.gz


    This will extract the contents to a new directory called apache-maven-3.3.9 and delete the downloaded file to save disk space.

    The all important directory here is apache-maven-3.3.9/bin and you want the directory to be in your PATH for convenient access.

  3. Now you want to setup:

    MAVEN_HOME environment variable (pointing to $9322_PRAC$/apache-maven-3.3.9, e.g., /Users/hpaik/cs9322-Prac/apache-maven-3.3.9)
    PATH to include the Maven bin directory (e.g., /Users/hpaik/cs9322-Prac/apache-maven-3.3.9/bin)

    Depending on your environment, how this is done would be different. In Mac OSX, for example, I'd edit my own ~/.profile and add:

    export MAVEN_HOME=/Users/hpaik/cs9322-Prac/apache-maven-3.3.9
    export PATH="$PATH:/Users/hpaik/cs9322-Prac/apache-maven-3.3.9"

    You could do the same for your own path and variables.

  4. Now run Maven command to make sure that it is setup correctly. You can do this by:

    $ mvn --version

    You should see Maven reporting back the version of Maven and JDK.

Integrating Maven with Eclipse

We will use Maven both from a command line as well as Eclipse IDE. From Eclipse, we can access Maven through a plug-in. You can install the plug-in from Eclipse by the following steps:

  1. Open Eclipse IDE

  2. Click Help -> Install New Software...

  3. Click the Add button at top right corner

  4. At pop up, fill in:

    • Name as "M2Eclipse"
    • Location as "http://download.eclipse.org/technology/m2e/releases"
  5. In the following step, you should see something like this:

  6. Tick 'Maven Integration for Eclipse' and follow through with the installation process. Eventually, Eclipse may tell you that instead of fresh installation, it will do upgrade of the existing plug-ins. This is fine.

After the installation, you may be asked to re-start Eclipse.

Now we want the Maven plug-in to point to our own installation of Maven (rather than the internal/embedded one).

  1. Go to Preference -> Maven -> Installation.
  2. Click 'Add' button. Add the location of your Maven installation (i.e., your MAVEN_HOME). For illustration purpose, in the screenshot below, it shows it to be '/usr/dev/apache-maven-3.2.3'.

  3. Click OK.

A Little Exercise with Maven

To check that your Maven and Eclipse plug-in are setup, let us do a little 'HelloWorld' exercise.

  1. Open a terminal window, and 'cd' into Eclipse's workspace. This should be $9322_PRAC$/workspace. For example, in my machine, it is: '/Users/hpaik/cs9322-Prac/workspace'. Let's call this $YOUR_WORKSPACE$.

  2. Now under $YOUR_WORKSPACE$, type in the following (all in one line):

    • $ mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

    • Don't be alarmed if this takes a while. Maven should be downloading necessary libraries to your local repository (normally located in a directory called '.m2' under your home).
    • Eventually, you should see the 'BUILD SUCCESS' message. See the INFO lines ... it should tell you that a project is created under $YOUR_WORKSPACE$/my-app. This matches '-DartifactId' parameter (think of it as the name of your application/project).

    • Open Eclipse. Go to File -> Import -> Maven -> Existing Maven Projects.

    • Navigate to $YOUR_WORKSPACE$/my-app and click 'Finish'

    • Open up 'my-app' project imported and briefly examine the files/structure generated by Maven (e.g., App.java). You should see something similar to this:

    • Note pom.xml file. It is the configuration file for Maven. We will not do much with the file here, but from the following labs, you will be working with this file closely. As I said earlier, we will learn the elements of POM as we go.

    • Now, do right click on the project name 'my-app' -> Run As -> maven install.

    • By Maven convention, running 'mvn install' will prompt Maven to compile all Java files under src/main/java, include all dependent libraries in classpath, package the classes into a 'jar' file and place it into /target directory, install it in your local repository $your_home$/.m2

    • As a result, you will find my-app-1.0-SNAPSHOT.jar in the target directory.

    • Now from a command line, go to $YOUR_WORKSPACE$/my-app

    • Type in: java -cp target/my-app-1.0-SNAPSHOT.jar com.mycompany.app.App

    • You should see 'Hello World!' in return.

Conclusion

At the end of Lab00, see if you can tick the following boxes: