Cloud Zone is brought to you in partnership with:

Eric is living in Chapel Hill, NC. By night, he writes and edits science fiction. On weekends, he spends too much time making plumbers hop on things. Eric has posted 249 posts at DZone. You can read more from them at their website. View Full User Profile

Creating a Hello World Application for Windows Azure in Eclipse

10.16.2012
| 4363 views |
  • submit to reddit

Curator's note: This tutorial originally appeared at the Windows Azure Java Developer Center.

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.

For a video of how to create this application, see Java Applications in Windows Azure Cloud Services using Eclipse.

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.

  1. 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.

    Within the Windows Azure Deployment Project dialog:

    1. Click Include 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. Click Include 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.)
    3. In the Applications selection, you will already see MyHelloWorld.war listed if you started this wizard from your MyHelloWorld project’s context menu Package for Windows Azure command.
    4. Your Windows Azure Deployment Project dialog will now look similar to the following.

      New Windows Azure Deployment Project dialog
    5. 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.

    Hh690944.Important(en-us,Azure.100).gifImportant
    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, right-click Windows Azure, and click Deploy to Windows Azure Cloud.)

  3. In the Publish Windows Azure Project dialog:

    1. For Subscription, click Download. 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.
    2. Click the Import button for Subscription, select the publish settings file that you saved locally in the previous step, and click Open.
    3. For Subscription, select the subscription that you want use for your deployment.
    4. For Storage account, select the storage account that you want to use, or click New to create a new storage account.
    5. For Access key, select Primary.
    6. For Service name, select the cloud service that you want to use, or click New to create a new cloud service.
    7. For Target environment, for purposes of this tutorial, select Staging. (When you’re ready to deploy to your production sight, you’ll change this to Production.)

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

      Publish Windows Azure Project dialog
    8. Click Next.
  4. The Remote Desktop Settings dialog is displayed. The default settings configured for your project will be shown. You can choose to enable or disable remote desktop access to the virtual machine hosting your deployment.

    • If you want to disable remote desktop access, uncheck Enable all roles to accept Remote Desktop Connections with these login credentials.
    • If you want to enable remote desktop access (this is the default):

      1. Check Enable all roles to accept Remote Desktop Connections with these login credentials.
      2. For user name, specify the name of the Remote Desktop user that you are creating.
      3. Specify and confirm a password for the Remote Desktop user. There is no default password for the user – you must specify a value.
      4. Accept the automatically provided default value, or enter an expiration date for the Remote Desktop user, either through the text box or by selecting a date through the Expiration date control.
      5. For the public and private certificates used to encrypt the user credentials, use the available options to do one of the following:

        • Select the certificates from your workspace.
        • Select the certificates from your file system.
        • Create new certificates.
        For development purposes, a default set of sample certificates are provided. The password for the default private certificate is Password1. Note this is a publicly known password, do not rely on the sample certificates for a production environment.
      6. [Optional] If you want the Remote Desktop session to start as soon as your package is deployed, check Start Remote Desktop on deploy.
      Your Remote Desktop Settings dialog will appear similar to the following if you choose to enable remote desktop access and the Start Remote Desktop on deploy option.

      Remote Desktop Settings dialog

    You can change your Remote Desktop settings for future deployments. For more information about Remote Desktop settings, see Enabling Remote Access for Windows Azure Deployments in Eclipse.

  5. In the Remote Desktop Settings dialog, click Publish.

    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://windows.azure.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 Ready (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.

Hh690944.Warning(en-us,Azure.100).gifWarning
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).

 

 

 

 

Published at DZone with permission of its author, Eric Gregory. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)