29 out of 46 rated this helpful - Rate this topic

Creating a Hello World Application for Windows Azure in Eclipse

Updated: May 1, 2013

The following steps show you how to create and deploy a basic JSP application to Windows Azure using the Windows Azure Plugin for Eclipse with Java (by Microsoft Open Technologies). A JSP example is shown for simplicity, but highly similar steps would be appropriate for a Java servlet, as far as Windows Azure deployment is concerned.

The application will look similar to the following:

Deployed Hello World

Prerequisites

To create a Hello World application

  1. First, we’ll start off with creating a Java project. Start Eclipse. Within Eclipse, at the menu click File, click New, and then click Dynamic Web Project. (If you don’t see Dynamic Web Project listed as an available project after clicking File, New, then do the following: click File, click New, click Project…, expand Web, click Dynamic Web Project, and click Next.) For purposes of this tutorial, name the project MyHelloWorld. (Ensure you use this name, subsequent steps in this tutorial expect your WAR file to be named MyHelloWorld). Your screen will appear similar to the following:

    Create Web Project

    Click Finish.

  2. Within Eclipse’s Project Explorer view, expand MyHelloWorld. Right-click WebContent, click New, and then click JSP File.

  3. In the New JSP File dialog, name the file index.jsp. Keep the parent folder as MyHelloWorld/WebContent, as shown in the following:

    Create JSP File

    Click Next.

  4. In the Select JSP Template dialog, for purposes of this tutorial select New JSP File (html) and click Finish.

  5. When the index.jsp file opens in Eclipse, add in text to display Hello World! within the existing <body> element. Your updated <body> content should appear as the following:

    <body>
      <b><% out.println("Hello World!"); %></b>
    </body>
    

    Save index.jsp.

  6. Now we’ll create a Windows Azure deployment project. As a shortcut, in Eclipse’s Project Explorer, right-click the MyHelloWorld project, click Windows Azure, and then click Package for Windows Azure.

    (As an alternative, you can click the New Windows Azure Deployment Project button in the toolbar, or you can use the menu: Within Eclipse, at the menu click File, click New, and then click Windows Azure Deployment Project. If you don’t see Windows Azure Deployment Project listed as an available project after clicking File, New, then do the following: click File, click New, click Project…, expand Windows Azure Deployment Project, click Windows Azure Deployment Project, and click Next. In this case, you will also need to add a reference to your application as part of the new project wizard.)

    For purposes of this tutorial, name the project MyAzureProject, and click Next.

  7. Within the Windows Azure Deployment Project dialog:

    1. Click the JDK tab. Click Use a JDK from this location. If the JDK installation location that you want to use is not selected automatically, click the Browse button, and using the browse control, select the desired directory location of the JDK to include in your deployment.

    2. Under the Cloud deployment section of the dialog, select the Automatically upload the local JDK to cloud storage and deploy from there option. (Note that you must have already created a Windows Azure storage account before you can use it with your Eclipse projects; to create a storage account, see How to use blob storage from Java.)

    3. From the Storage account drop-down list, select the Windows Azure storage account that you want use to upload the JDK.

    4. If the storage account you want to use is not in the list, you can add it by clicking on the Accounts link. The Accounts link opens a Storage Accounts dialog that provides two options for adding existing storage accounts to the list of storage accounts tracked by Eclipse in your current workspace. (New storage accounts can be created at the Windows Azure Management Portal.)

      Take one of the following actions:

      1. For Eclipse to automatically discover your available storage accounts, click the Import from PUBLISH-SETTINGS file button and either download or browse to a publish settings file for your Windows Azure account.

      2. If instead you prefer to manually specify an existing storage account that you want to use, click the Add button and enter the name and access key of that storage account. This information can be obtained from the Storage section of the Windows Azure Management Portal.

    5. Close the Storage Accounts dialog after you have added your storage account(s), and select the storage account that you want to use.

      Your populated JDK dialog will now look similar to the following.

      New Windows Azure Deployment Project dialog
      noteNote
      The URL of the uploaded JDK is generated automatically based on the name of the JDK’s installation directory. As long as a file at that exact URL location exists, it will not be automatically uploaded again, regardless of whether its contents have changed. If you change the local JDK setting to point to a different local directory, then a new ZIP file generated from that directory will be uploaded.

    6. Click the Server tab. Click Use a server from this location and select the installation directory on your computer of an application server of your choice to include in your deployment. If you use Tomcat 6 or 7, Glassfish OSE 3, JBoss 6 or 7, or Jetty 7 or 8, the server type will automatically be detected and shown in the dropdown shown below. (If the server type is not recognized, do not select a server here but instead manually enter your server and application deployment logic in your startup.cmd file.)

    7. Similar to as done for the JDK, under the Cloud deployment section of the dialog, select the Automatically upload the local server to cloud storage and deploy from there option and specify the storage account that you want to use to upload the application server.

      Your Server dialog will now look similar to the following.

      Server tab
    8. Click the Applications tab. You will see MyHelloWorld.war already listed if you started this wizard from your MyHelloWorld project’s context menu Package for Windows Azure command.

    9. Your Applications dialog will now look similar to the following.

      Applications tab
    10. [Optional] Click Next to set options for Session Affinity, Caching, or Remote Debugging, as shown in the following.

      Windows Azure deployment project optional features
    11. Click Finish to close the Windows Azure Deployment Project dialog.

To deploy your application to the compute emulator

  1. In Eclipse’s Project Explorer, click MyAzureProject.

  2. In the Eclipse toolbar, click the Run in Windows Azure Emulator button, Run in Windows Azure emulator. If the Run in Windows Azure Emulator button is not enabled, ensure that MyAzureProject is selected in Eclipse’s Project Explorer, and ensure that Eclipse’s Project Explorer has focus as the current window.

    This will first start a full build of your project and then launch your Java web application in the compute emulator. (Note that depending on your computer’s performance characteristics, the first build may take between a few seconds to a few minutes, but subsequent builds will get faster.) After the first build step has been completed, you will be prompted by Windows User Account Control (UAC) to allow this command to make changes to your computer. Click Yes.

    ImportantImportant
    If you do not see the UAC prompt, check the Windows taskbar for the UAC icon and click it first. Sometimes the UAC prompt does not show up as a topmost window, but is visible only as a taskbar icon.

    Examine the output of the compute emulator UI to determine if there are any issues with your project. Depending on the contents of your deployment, it may take a couple minutes for your application to be fully started within the compute emulator.

  3. Start your browser and use the URL http://localhost:8080/MyHelloWorld as the address (the MyHelloWorld portion of the URL is case-sensitive). You should see your MyHelloWorld application (the output of index.jsp), similar to the following image:

    Hello World in Compute Emulator

When you are ready to stop your application from running in the compute emulator, in the Eclipse toolbar, click the Reset Windows Azure Emulator button, Reset Windows Azure emulator.

To deploy your application to Windows Azure

  1. In Eclipse’s Project Explorer, click MyAzureProject.

  2. In the Eclipse toolbar, click the Publish to Windows Azure Cloud button, Publish to Windows Azure Cloud. If the Publish to Windows Azure Cloud button is not enabled, ensure that MyAzureProject is selected in Eclipse’s Project Explorer, and ensure that Eclipse’s Project Explorer has focus as the current window. (As an alternative to using the Publish to Windows Azure Cloud button, you can use the following menu choices: In Eclipse’s Project Explorer, right-click MyAzureProject, click Windows Azure, and click Deploy to Windows Azure Cloud.)

  3. In the Publish to Windows Azure dialog:

    1. If there are no subscriptions to select in the Subscription list yet, follow these steps to import your subscription information:

      1. Click Import from PUBLISH-SETTINGS file.

      2. In the Import Subscription Information dialog, click Download PUBLISH-SETTINGS File. If you are not yet logged into your Windows Azure account, you will be prompted to log in. Then you’ll be prompted to save a Windows Azure publish settings file. (You can ignore the resulting instructions shown on the logon pages – they are provided by the Windows Azure portal and are intended for Visual Studio users.) Save it to your local machine.

      3. Still in the Import Subscription Information dialog, click the Browse button, select the publish settings file that you saved locally in the previous step, and then click Open. Your screen should look similar to the following.

        Import publish settings
      4. Click OK.

    2. For Subscription, select the subscription that you want use for your deployment.

    3. For Storage account, select the storage account that you want to use, or click New to create a new storage account.

    4. For Service name, select the cloud service that you want to use, or click New to create a new cloud service.

    5. For Target environment, for purposes of this tutorial, select Staging. (When you’re ready to deploy to your production site, you’ll change this to Production.)

      Note that the Publish to Windows Azure dialog contains a section for Remote Access. By default, Remote Access is not enabled and we will not enable it for this example. To enable Remote Access, you would enter a user name and password to use when remotely logging in. For more information about Remote Access, see Enabling Remote Access for Windows Azure Deployments in Eclipse.

      Your Publish to Windows Azure dialog will appear similar to the following:

      Publish Windows Azure Project dialog
  4. Click Publish to publish to the Staging environment.

    When prompted to perform a full build, click Yes. This may take several minutes for the first build.

    A Windows Azure Activity Log will display in your Eclipse tabbed views section.

    Windows Azure Activity Log

    You can use this log, as well as the Console view, to see the progress of your deployment. An alternative is to log in to the Windows Azure Management Portal, http://manage.windowsazure.com, and use the Cloud Services section to monitor the status.

Because this was a deployment to a staging environment, the DNS will be of the form http://<guid>.cloudapp.net. You can see the DNS name if you click the deployment name in the Windows Azure Platform Management Portal (you may need to expand the Hosted Service node to see the deployment name); the DNS name is in the right hand pane of the portal.

Once your deployment has a status of Running (as indicated by the Windows Azure Platform Management Portal), you can enter the URL for your deployed application in your browser to see that your application is deployed to the cloud. The URL for an application deployed to the staging environment will be of the form http://<guid>.cloudapp.net/. For example, http://72d5eb5875234b7ca8c7f74c80a2a1f1.cloudapp.net. Remember to append MyHelloWorld (case-sensitive) to the end of the URL, so you’ll be using a URL similar to the following in your browser (use the GUID assigned to your URL instead of the GUID listed here): http://72d5eb5875234b7ca8c7f74c80a2a1f1.cloudapp.net/MyHelloWorld.

Although this walk-through was for a deployment to the staging environment, a deployment to production follows the same steps, except within the Publish Azure Project dialog, select Production instead of Staging for the Target environment. A deployment to production results in a URL based on the DNS name of your choice, instead of a GUID as used for staging.

WarningWarning
At this point you have deployed your Windows Azure application to the cloud. However, before proceeding, realize that a deployed application, even if it is not running, will continue to accrue billable time for your subscription. Therefore, it is extremely important that you delete unwanted deployments from your Windows Azure subscription.

To delete your deployment

To delete your deployment within the Windows Azure Plugin for Eclipse with Java, ensure that MyAzureProject is selected in Eclipse’s Project Explorer, ensure the Eclipse Project Explorer has the current window focus, and then click the Unpublish button, Unpublish, in the Eclipse toolbar. (You could do the same operation by right-clicking MyAzureProject in Eclipse’s Project Explorer, clicking Windows Azure and then clicking Undeploy from Windows Azure Cloud.) This will display the Unpublish Windows Azure Project dialog.

Unpublish Windows Azure Project dialog

Select the subscription and cloud service that contains your deployment, select the deployment that you want to delete, and then click Unpublish.

(An alternative to using the plugin to delete the deployment is to use the Cloud Services section of the Windows Azure Management Portal: Navigate to your deployment, select it, and then click the Delete button. This will stop, and then delete, the deployment. If you only want to stop the deployment and not delete it, click the Stop button instead of the Delete button, but as mentioned above, if you do not delete the deployment, billable charges will continue to accrue for your deployment even if it is stopped).

To make your deployment more efficient

The Eclipse plugin enables several approaches to deploying the JDK, the application server, and arbitrary user-defined components. One approach is for these items to be packaged together with your deployment, which would happen when you use the Include the local JDK in the deployment package and Include the local server in the deployment package options in the JDK and Server dialogs, respectively. This is the simplest way to get started, but doing so results in greater package size and longer deployment times. It may also make the maintenance and repetitive updates of your applications more difficult in the long term.

The alternative is to configure your project such that the JDK, application server, and/or arbitrary user-defined components are deployed from a download location when they are deployed to the cloud. Ideally the download location would be a private Windows Azure blob. By using a download location, the results are a much smaller package size, faster deployment to the cloud, and easier maintenance.

There are two ways to deploy from a download location. The first way to deploy from a download location is to manually upload your JDK or application server to a download location prior to deploying to Windows Azure, and then specifying that download location through the Deploy a JDK from the selected download location and Deploy a server from the selected download location options of the JDK and Server dialogs, respectively.

The second way to deploy from a download location is to automatically upload the JDK and application server to cloud storage and deploy from there. This latter approach is the easiest and was used in this sample when you selected the Automatically upload the local JDK to cloud storage and deploy from there and Automatically upload the local server to cloud storage and deploy from there options in the JDK and Server dialogs, respectively.

When you use the Automatically upload the local JDK to cloud storage and deploy from there or Automatically upload the local server to cloud storage and deploy from there options, you specify a directory as the source location, not a zip file. During the first deployment, Windows Azure will create the zip file for you and store it at the Windows Azure storage account that you specified. On subsequent deployments, unless you point to a new local directory for your JDK or server, Windows Azure will reuse what you already uploaded.

Although you cannot use an Automatically upload to cloud storage and deploy from there option for arbitrary user-defined components in the current release of the plugin, you can deploy them from download. For more information, see Components properties.

If you choose not to use the Automatically upload to cloud storage and deploy from there options, but still want to deploy from a download location, use the Deploy a JDK from the selected download location or Deploy a server from the selected download location options. When using these options, you specify a URL to a zip file as the source location. You can either use Windows Azure storage as the download location, or some other URL-based location. The following steps describe the process. This example is for deploying the JDK from a download location, although the steps would be very similar for the application server and arbitrary user-defined components.

  1. Create a ZIP of your JDK installation directory, ensuring that the directory node itself is the child of the ZIP structure, and not its contents. Take note of the name of the directory, as you’ll need it later.

  2. Upload the ZIP into either your Windows Azure storage account as a blob or another URL-hosted location. For Windows Azure, you can do this using an externally available tool for uploading blobs to Windows Azure storage. It is recommended to use a private blob. Take note of the URL of the ZIP contents.

  3. In the JDK tab of the New Windows Azure Project wizard (or the role’s Server Configuration property page if you’re updating an existing project), enable the Deploy from download feature:

    1. Check the checkbox named Deploy a JDK from the selected download location.

    2. Enter the full URL to your JDK ZIP file download. If using Windows Azure, blob names must be lowercase.

    3. If you’re using Windows Azure storage, select the account that you want to use from the Storage account drop-down list (or add an account using the Accounts link) and enter the access key for your Windows Azure storage account, if this is a private blob (recommended). If you’re not using Windows Azure storage, select (none) from the Storage account drop-down list.

    4. Ensure that the JAVA_HOME textbox refers to the correct directory name. By default, it will reference the same JDK directory name as the value you chose for your local use. But if the directory contained in the ZIP has a different name (for example, due to using a different version), update the directory name in the JAVA_HOME textbox accordingly, since this setting will be used in the cloud (not in the compute emulator).

  4. Click Finish to save your changes.

That’s it. You can follow a similar set of steps if you want to enable your application server to be deployed from a download location: Just use the Server tab of the New Windows Azure Project dialog (or the role’s Server Configuration property page if you’re updating an existing project), and you’ll have similar controls and logic as used in the JDK tab. Likewise, you can deploy arbitrary user-defined components from download. For more information, see Components properties.

Note that the Deploy from download and Automatically upload the local [JDK or server] to cloud storage and deploy from there options are in effect only when your application is deployed in the cloud. Those options have no effect on your compute emulator experience; the local version of the JDK, server, and components will still be used when you deploy to the compute emulator.

See Also

Did you find this helpful?
(1500 characters remaining)
© 2013 Microsoft. All rights reserved.
facebook page visit twitter rss feed newsletter