8



Java to .NET Framework Migration Workshop
Compiling Java Applications with J#

INCLUDEPICTURE "../PowerPnt/JAVAMIG_07/Slide_275.jpg" \* MERGEFORMAT



Objectives

After completing this lab, you will be able to:


Note

This lab focuses on the concepts in this module and as a result may not comply with Microsoft security recommendations.




Note

The line numbers specified in this lab are provided as a guide only. You may find that some of the line numbers vary depending on how you edit the code.



Prerequisites

Before working on this lab, you must have:

Estimated time to complete this lab: 60 minutes


Log in to the
Hosted Experience

The Visual Studio Hosted Experience is pre-configured to allow you to run the following lab in a hosted environment. A broadband connection is required.

      1. Open Internet Explorer.

      2. Browse to http://msdn.microsoft.com/virtuallab.

      3. Click on the lab group titled “Java to .NET Framework Migration Workshop.”

      4. Click on the lab titled “Compiling Java Applications with J#”

      5. Follow the remaining on-screen instructions to start the Virtual Lab experience. Once you have logged in and started the Virtual Lab session you may begin with Exercise 1.

Exercise 1
Migrating a Java Application to J#

In this exercise, you will take an existing Java application and import the code into a new J# project. You will then compile the code using the J# compiler, and then build and run the application. It is important to stress that all business logic is migrated intact, and requires no further modification.

The Java application is a stock ticker, consisting of client and server components which communicate across the network passing XML documents over a socket. The client is an applet, which can be viewed using Internet Explorer.


Tip

Completed code for the J# classes in this exercise are available in the C:\JavaMigration\Labs\Lab07\Solution folder.



In this step you will create an IIS virtual directory in preparation for deploying and running the Java and J# applications.

      1. Using Windows Explorer, create the following folder:

C:\JavaMigration\Labs\Lab07\Deploy

      1. On the Start menu, point to Administrative Tools, and click Internet Information Services (IIS) Manager.

      2. Expand computername (local computer) where computername is the name of your computer, and then expand Web Sites.

      3. Right-click Default Web Site, point to New, and then click Virtual Directory.

      4. In the Virtual Directory Creation Wizard, click Next.

      5. In the Alias text box, type Lab07, and then click Next.

      6. In the Path text box, type C:\JavaMigration\Labs\Lab07\Deploy, and then click Next.

      7. Click Next.

      8. Click Finish. The virtual directory has been created with the following URL: http://localhost/Lab07.

      9. Close the Internet Information Services (IIS) Manager.

      1. Due to licensing restrictions, Java software has not been installed in the Hosted Experience environment – hence, the steps in this section (“Deploy and test the Java solution”) will not function. Instead, you should read the steps in this section to familiarize yourself with how the original Java application behaves.

      2. Open a command prompt window, and move to the following directory:

C:\JavaMigration\Labs\Lab07\Starter

      1. This folder contains two folders, Client and StockServer. The stock server is a multi-threaded server that streams stock information across a socket as XML. The client application is a multi-threaded applet that displays the stock information as a simple stock ticker.


      2. Type the following commands:

The StockServer folder contains the following files:

        • fictitious_stocks.xml. An XML file containing fictitious stock information.

        • StockServer.java. A multithreaded XML socket server.


Note

If you have time, examine the StockServer.java file using Notepad.

      1. Execute the following command to start the server in a new command prompt window:

start java StockServer

The server displays the following message:

Loaded stocks and awaiting clients…

      1. In the original command prompt window, move to the following directory:

C:\JavaMigration\Labs\Lab07\Starter\Client

      1. Examine the contents of this directory. Notice that the directory contains javaticker.htm and stockticker.jar. The file stockticker.jar contains the applet and supporting code, the source of which is in the various Java files in this folder.

      2. Examine the javaticker.htm file use Notepad. The file contains the following HTML code:

<html>

<body>

<H1>Test Page for Java Stock Ticker</H1>

<APPLET CODE=StockTicker.class ARCHIVE=stockticker.jar WIDTH=700 HEIGHT=100>

</APPLET>

</body>

</html>


The applet class is named StockTicker, and is found in the stockticker.jar archive.

      1. Close Notepad.


Note

If you have time, examine the StockTicker.java source code file. The remaining source code files in this folder contain classes that are used to parse the XML stream transmitted by the server, and convert it into StockBean objects which are displayed by the applet.

      1. Copy the following files to the C:\JavaMigration\Labs\Lab07\Deploy folder:


      1. Open Internet Explorer, and navigate to the following URL:

http://localhost/lab07/javaticker.htm

Once the applet is loaded, the ticker is shown as follows:

Note that stock prices are generated using a simple algorithm and do not attempt to reflect realistic stock movement.

      1. Close Internet Explorer.

      2. Close the command prompt window running the stock server.

      1. Start Microsoft Visual Studio .NET 2003.

      2. On the File menu, point to New, and then click Project.

      3. In the New Project dialog box, in the Project Types pane, click Visual J# Projects, and in the Templates pane, click Console Application.

      4. In the Name text box, type StockServer, in the Location text box, type C:\JavaMigration\Labs\Lab07, and click OK.

The solution is created as shown below:

      1. In Solution Explorer, right-click Class1.jsl, click Delete, and then click OK.

      2. In Solution Explorer, right-click the StockServer project, point to Add, and then click Add Existing Item.

      3. In the Add Existing Item dialog box, browse to the following location:

C:\JavaMigration\Labs\Lab07\Starter\StockServer.

      1. Change Files of type: to All Files (*.*).

      2. Click StockServer.java, press CONTROL, and click fictitious_stocks.xml, and then click Open. Both files are added to the project as shown below:


      1. Right-click References, and click Add Reference.

      2. In the Add Reference dialog box, click Browse, and browse to the following directory:

C:\JavaMigration\Labs\Lab07\Starter

      1. Select xerces.net.dll, and then click Open.


Note

The original Java application used the Xerces parser to parse the XML stream. Xerces.NET is an open source implementation of the Xerces parser for the .NET Framework, which can be freely downloaded from http://www.dotnetit.org/xerces.net/. The NETiT site (www.dotnetit.org) is a useful repository for several open source projects that have been ported to the .NET Framework.

      1. Click OK. The reference is added to the StockServer project.

      2. Build the solution. It will compile without any errors or warnings. Notice that all business logic has been migrated intact.


Exercise 2
Migrating a Java applet to J#

In this exercise, you will migrate the Java applet to J# as a J# Browser Control, deploy it, and test it. It is important to note that as before, the business logic remains intact following the migration of the applet to J#.

      1. In Solution Explorer, right-click 'Solution StockServer', point to Add, and click New Project.

      2. In the New Project dialog box, in the Project Types pane, click Visual J# Project, and in the Templates pane, click Class Library.

      3. In the Name text box, type Client, in the Location text box, type C:\JavaMigration\Labs\Lab07, and click OK.

The Client project is added to the solution as shown below:

      1. In Solution Explorer, right-click Class1.jsl, click Delete, and then click OK.

      2. In Solution Explorer, right-click the Client project, point to Add, and then click Add Existing Item.

      3. In the Add Existing Item dialog box, browse to the following location:

C:\JavaMigration\Labs\Lab07\Starter\Client.


      1. Select all the Java source files in this folder (StockBean.java, StockBeanStack.java, StockConsumer.java, StockSAXParser.java, and StockTicker.java), and then click Open.

The Java classes are added to the solution as shown below:

      1. Under Client, right-click References, and click Add Reference.

      2. In the Add Reference dialog box, click Browse, and browse to the following directory:

C:\JavaMigration\Labs\Lab07\Starter.

      1. Select xerces.net.dll, and then click Open.

      2. Click OK. The reference is added to the Client project.

      3. Build the solution.

      4. Close Visual Studio .NET 2003.

      1. Open a Visual Studio .NET 2003 Command Prompt window, and move to the following folder:

C:\JavaMigration\Labs\Lab07\StockServer\bin\Debug.

      1. Copy the XML file containing the stock information into the same directory as the assembly as follows:

copy ..\..\fictitious_stocks.xml


      1. Run the J# StockServer application by executing the following command:

start StockServer

The server displays the following message:

Loaded stocks and awaiting clients…

      1. In the original Visual Studio .NET 2003 Command Prompt window, move to the following directory:

C:\JavaMigration\Labs\Lab07\Client\bin\Debug

      1. Copy the Client.dll and xerces.net.dll assemblies into the deployment folder for the Web site:

copy *.dll C:\JavaMigration\Labs\Lab07\Deploy

      1. Move to the deployment folder:

cd C:\JavaMigration\Labs\Lab07\Deploy

Note that this folder now contains the following files:

The HTML page must be converted to reference the Client.dll assembly rather than the applet in stockticker.jar.

      1. Create a copy of the original javaticker.htm file as follows:

copy javaticker.htm jsharpticker.htm

      1. Type the following commands:

tagconvert jsharpticker.htm

dir

Notice that tagconvert has backed up the original HTML file to jsharpticker.htm.vjsbak.

      1. Using Notepad, open jsharpticker.htm. Notice that the tagconvert utility has changed the applet tag to an object tag, and added a CLASSID attribute as follows:

<html>

<body>

<H1>Test Page for Java Stock Ticker</H1>

<object WIDTH=700 HEIGHT=100 CLASSID="clsid:a399591c-0fd0-41f8-9d25-bd76f632415f" VJSCODEBASE="StockTicker.dll#StockTicker" >

</object>

</body>

</html>


The tagconvert utility assumes that the assembly containing the migrated applet has the same name as the original jar file. You will need to change the assembly referenced in the VJSCODEBASE attribute.


      1. Change the VJSCODEBASE attribute as follows:

VJSCODEBASE="Client.dll#StockTicker"


      1. Change the <H1> tag as follows:

<H1>Test Page for J# Stock Ticker</H1>


      1. Save the file and close Notepad.

      2. Open Internet Explorer, and browse to the following URL:

http://localhost/Lab07/jsharpticker.htm

The stock ticker is appears as shown below:

      1. Close Internet Explorer.

      2. Stop the StockServer by closing the command prompt window.