INCLUDEPICTURE "../PowerPnt/JAVAMIG_07/Slide_275.jpg" \* MERGEFORMAT
Objectives
Migrate a Java application to J#
Migrate a Java applet to J#
Note
Note
Prerequisites
Experience compiling and running Java applications.
Experience with the JDK 1.1.
Estimated time to complete this lab: 60 minutes
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.
Log in to the Hosted Experience
Open Internet Explorer.
Browse to http://msdn.microsoft.com/virtuallab.
Click on the lab group titled “Java to .NET Framework Migration Workshop.”
Click on the lab titled “Compiling Java Applications with J#”
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.
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
Create a virtual directory using Internet Information Server (IIS)
In this step you will create an IIS virtual directory in preparation for deploying and running the Java and J# applications.
Using Windows Explorer, create the following folder:
C:\JavaMigration\Labs\Lab07\Deploy
On the Start menu, point to Administrative Tools, and click Internet Information Services (IIS) Manager.
Expand computername (local computer) where computername is the name of your computer, and then expand Web Sites.
Right-click Default Web Site, point to New, and then click Virtual Directory.
In the Virtual Directory Creation Wizard, click Next.
In the Alias text box, type Lab07, and then click Next.
In the Path text box, type C:\JavaMigration\Labs\Lab07\Deploy, and then click Next.
Click Next.
Click Finish. The virtual directory has been created with the following URL: http://localhost/Lab07.
Close the Internet Information Services (IIS) Manager.
Deploy and test the Java solution
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.
Open a command prompt window, and move to the following directory:
C:\JavaMigration\Labs\Lab07\Starter
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.
cd StockServer
dir
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.
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…
In the original command prompt window, move to the following directory:
C:\JavaMigration\Labs\Lab07\Starter\Client
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.
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.
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.
Copy the following files to the C:\JavaMigration\Labs\Lab07\Deploy folder:
javaticker.htm
stockticker.jar
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.
Close Internet Explorer.
Close the command prompt window running the stock server.
Rebuild the Stock Server as a J# project
Start Microsoft Visual Studio .NET 2003.
On the File menu, point to New, and then click Project.
In the New Project dialog box, in the Project Types pane, click Visual J# Projects, and in the Templates pane, click Console Application.
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:
In Solution Explorer, right-click Class1.jsl, click Delete, and then click OK.
In Solution Explorer, right-click the StockServer project, point to Add, and then click Add Existing Item.
In the Add Existing Item dialog box, browse to the following location:
C:\JavaMigration\Labs\Lab07\Starter\StockServer.
Change Files of type: to All Files (*.*).
Click StockServer.java, press CONTROL, and click fictitious_stocks.xml, and then click Open. Both files are added to the project as shown below:
In the Add Reference dialog box, click Browse, and browse to the following directory:
C:\JavaMigration\Labs\Lab07\Starter
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.
Click OK. The reference is added to the StockServer project.
Build the solution. It will compile without any errors or warnings. Notice that all business logic has been migrated intact.
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#.
Add the Client files to the J# solution
In Solution Explorer, right-click 'Solution StockServer', point to Add, and click New Project.
In the New Project dialog box, in the Project Types pane, click Visual J# Project, and in the Templates pane, click Class Library.
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:
In Solution Explorer, right-click Class1.jsl, click Delete, and then click OK.
In Solution Explorer, right-click the Client project, point to Add, and then click Add Existing Item.
In the Add Existing Item dialog box, browse to the following location:
C:\JavaMigration\Labs\Lab07\Starter\Client.
The Java classes are added to the solution as shown below:
Under Client, right-click References, and click Add Reference.
In the Add Reference dialog box, click Browse, and browse to the following directory:
C:\JavaMigration\Labs\Lab07\Starter.
Select xerces.net.dll, and then click Open.
Click OK. The reference is added to the Client project.
Build the solution.
Close Visual Studio .NET 2003.
Deploy and run the J# StockServer application
Open a Visual Studio .NET 2003 Command Prompt window, and move to the following folder:
C:\JavaMigration\Labs\Lab07\StockServer\bin\Debug.
Copy the XML file containing the stock information into the same directory as the assembly as follows:
copy ..\..\fictitious_stocks.xml
start StockServer
The server displays the following message:
Loaded stocks and awaiting clients…
Deploy and run the J# Client assembly
In the original Visual Studio .NET 2003 Command Prompt window, move to the following directory:
C:\JavaMigration\Labs\Lab07\Client\bin\Debug
Copy the Client.dll and xerces.net.dll assemblies into the deployment folder for the Web site:
copy *.dll C:\JavaMigration\Labs\Lab07\Deploy
Move to the deployment folder:
cd C:\JavaMigration\Labs\Lab07\Deploy
Note that this folder now contains the following files:
Client.dll
javaticker.htm
stockticker.jar
xerces.net.dll
The HTML page must be converted to reference the Client.dll assembly rather than the applet in stockticker.jar.
Create a copy of the original javaticker.htm file as follows:
copy javaticker.htm jsharpticker.htm
Type the following commands:
tagconvert jsharpticker.htm
dir
Notice that tagconvert has backed up the original HTML file to jsharpticker.htm.vjsbak.
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.
VJSCODEBASE="Client.dll#StockTicker"
Change the <H1> tag as follows:
<H1>Test Page for J# Stock Ticker</H1>
Save the file and close Notepad.
Open Internet Explorer, and browse to the following URL:
http://localhost/Lab07/jsharpticker.htm
The stock ticker is appears as shown below:
Close Internet Explorer.
Stop the StockServer by closing the command prompt window.